Adding a reverse proxy to the test swarm - tooltwist/documentation GitHub Wiki
This reverse proxy uses the services of two Docker containers:
- nginx is a high speed reverse proxy that can route to server based on rules in a configuration file.
- docker-gen watches for changes in the swarm, and rewrites the nginx configuration file when a container starts or stops. The steps below prepare containers for these two technologies.
$ cd ~/Deploy/Swarm/<<SWARM_NAME>>
$ git clone https://github.com/JustAdam/docker-gen-swarm.git
...
$ cd docker-gen-swarm
$ mv nginx/ nginx-myproxy
Copy the certificates so they can be used by docker-gen to access the swarm master.
$ mkdir docker-gen/certs
$ cp ~/.docker/machine/certs/*.pem docker-gen/certs
Change the Dockerfile to install docker-gen into the image: ... RUN apt-get update && apt-get upgrade -y && apt-get clean RUN apt-get install -y wget
# Install docker-gen
#ADD docker-gen /usr/bin/docker-gen
RUN (cd /usr/bin && wget https://github.com/jwilder/docker-gen/releases/download/0.4.1/docker-gen-linux-i386-0.4.1.tar.gz && tar xvzf docker-gen-linux-i386-0.4.1.tar.gz)
ADD certs/ /etc/nginx/certs/
...
Change the last line of the Dockerfile as shown:
CMD ["/usr/bin/docker-gen", "--tlscacert=/etc/nginx/certs/ca.pem", "--tlscert=/etc/nginx/certs/cert.pem", "--tlskey=/etc/nginx/certs/key.pem", "-watch", "-only-exposed", "-notify-sighup=nginx_reverseproxy", "nginx.tmpl", "/etc/nginx/sites-enabled/reverse-proxy"]
Replace the template with a more developed template from the author of nginx-proxy:
$ mv docker-gen/templates/nginx.tmpl docker-gen/templates/nginx.tmpl-safe
$ docker run --rm jwilder/nginx-proxy /bin/bash -c "cat /app/nginx.tmpl" > docker-gen/templates/nginx.tmpl
First find the IP address of the master server:
$ docker-machine env $(cat ../SWARM_NAME)-master | grep DOCKER_HOST
export DOCKER_HOST=tcp://***.***.***.***:3376
Take note of the IP address and use it to set the DOCKER_HOST
value in docker-compose.yml
.
Also save it as variable:
$ MASTER_IP=111.222.333.444
Build the containers:
$ docker $(docker-machine config $(cat ../SWARM_NAME)-master) build -t docker-gen ./docker-gen
...
$ docker $(docker-machine config $(cat ../SWARM_NAME)-master) build -t nginx-myproxy ./nginx-myproxy
...
Start the containers:
$ docker $(docker-machine config $(cat ../SWARM_NAME)-master) run -d --restart=always --name=docker-gen --env=affinity:image==docker-gen:latest --env=DOCKER_HOST=tcp://${MASTER_IP}:3376 docker-gen
$ docker $(docker-machine config $(cat ../SWARM_NAME)-master) run -d --restart=always --name=nginx_reverseproxy -p 80:80 --volumes-from=docker-gen nginx-myproxy
$ docker logs -f docker-gen
$ docker logs -f nginx_reverseproxy
$ eval `docker-machine env --swarm $(cat ../SWARM_NAME)-master`
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d2ff64887a29 nginx "nginx" Less than a second ago Up 2 seconds 0.0.0.0:80->80/tcp nginx_reverseproxy
c3b687a4c9a9 docker-gen "/usr/bin/docker-gen About a minute ago Up About a minute docker-gen
6e30c7e1f424 tooltwist/ttsite:30sep2015 "/sbin/my_init" 5 hours ago Up 5 hours 22/tcp, 39393/tcp, 0.0.0.0:32768->8080/tcp swarm2_ttdemo_1
1f97117847b8 swarm:latest "/swarm join --addr 8 hours ago Up 8 hours 2375/tcp swarm-agent
f5221c2ef67a swarm:latest "/swarm manage --tls 8 hours ago Up 8 hours 2375/tcp, 0.0.0.0:3376->3376/tcp swarm-agent-master
(optional) We'll start a simple "hello world" container.
$ eval `docker-machine env --swarm $(cat ../SWARM_NAME)-master`
$ docker run -d -e "VIRTUAL_HOST=myhello.com" -e "VIRTUAL_PORT=80" -p 80 tutum/hello-world
Add an entry to /etc/hosts for myhello.com, with the IP address of the master server, then point your browser at http://myhello.com. You will hopefully see a "hello world" message,
http://www.before.no/2015/04/using-docker-gen-with-a-swarm-cluster https://github.com/jwilder/docker-gen