Skip to content

Instantly share code, notes, and snippets.

@hamiltont
Last active August 29, 2015 13:56
Show Gist options
  • Save hamiltont/8986911 to your computer and use it in GitHub Desktop.
Save hamiltont/8986911 to your computer and use it in GitHub Desktop.
Initial version of a micro platform for rapidly proxying docker containers
# Pdock rapidly sets up a tiny platform for you automatically discover and
# serve your docker services. It uses the hipache and skydock projects
# to enable rapidly connecting to your containers via subdomain names.
# Only port 80 has to be opened, and UDP,TCP,HTTP,WebSocket should
# work (mostly) out of the box
#
# Copyright 2014 Hamilton Turner <hamiltont@gmail.com>
#
#
# Examples:
# - Start pdock architecture, run wordpress at wordpress.basedomain.com
# # docker run -d tutum/wordpress
# # pdock_add_proxy_record wordpress 80
#
# - Make Ghost Available At ghost.basedomain.com
# # docker run -d -name ghost dockerfile/ghost
# # pdock_add_proxy_record ghost 2368
#
# - Run, then remove, a webserver at python-simplehttpserver.basedomain.com
# # docker run -d hamiltont/python-simplehttpserver
# # pdock_add_proxy_record python-simplehttpserver 8000
# # pdock_rm_proxy_record python-simplehttpserver
#
# Install
# source this file from your /root/.bashrc
#
# Limitations
# - Standard docker limitations on multicast or other
# service discovery-based applications
# - Uses skydock convention on repository names to
# assign subdomains
# - Cannot (yet) auto-discover service port. I'd like to
# automatically discover port if there is only one used
# by the container
DOMAIN=basedomain.com
pdock_print_dns_records() {
# docker run -e SKYDNS=http://172.17.0.2:8080 hamiltont/skydnsctl
# If you are guaranteed of service health, you could run
# SKYDNS=http://skydns.dev.docker:8080
SKY=`docker run -e SKYDNS=http://skydns.dev.docker:8080 hamiltont/skydnsctl`
echo "$SKY" | grep -E "Name|Host|Port"
}
pdock_print_proxy_records() {
docker run -rm -i -t crosbymichael/redis-cli -h hipache.dev.docker 'KEYS *'
}
pdock_add_proxy_record() {
NAME=$1
PORT=$2
docker run -rm crosbymichael/redis-cli -h hipache.dev.docker \
RPUSH frontend:${NAME}.media.protocollar.com ${NAME}
docker run -rm crosbymichael/redis-cli -h hipache.dev.docker \
RPUSH frontend:${NAME}.media.protocollar.com http://${NAME}.dev.docker:${PORT}
}
pdock_inspect_proxy_record() {
NAME=$1
docker run -rm crosbymichael/redis-cli -h hipache.dev.docker \
LRANGE frontend:${NAME}.${DOMAIN} 1 -1
}
pdock_list_proxy_records() {
docker run -rm crosbymichael/redis-cli -h hipache.dev.docker \
KEYS frontend:\*.${DOMAIN}
}
pdock_rm_proxy_record() {
NAME=$1
docker run -rm crosbymichael/redis-cli -h hipache.dev.docker \
DEL frontend:${NAME}.media.protocollar.com
}
pdock_stop() {
docker kill skydns > /dev/null
docker kill skydock > /dev/null
docker kill hipache > /dev/null
docker rm skydns > /dev/null
docker rm skydock > /dev/null
docker rm hipache > /dev/null
}
pdock_start() {
pdock_stop
docker run -d -p 172.17.42.1:53:53/udp -name skydns crosbymichael/skydns -nameserver 8.8.8.8:53 -domain docker
docker run -d -v /var/run/docker.sock:/docker.sock -name skydock -link skydns:skydns \
crosbymichael/skydock -ttl 30 -environment dev -s /docker.sock -domain docker
docker run -d -p 80:80 -name hipache hamiltont/hipache
}
@bketelsen
Copy link

couldn't you skip the basedomain var and name your containers with the full public url?

docker run hamiltont/www.mydomain.com ...

then when you're adding proxy records

RPUSH frontend:${NAME} ${NAME}
RPUSH frontend:${NAME} http://${NAME}.dev.docker:${PORT}

Then it would work for fqdn's instead of just subdomains. Still need the exposed port to make it complete, but it's a start.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment