Automated nginx proxy for Docker containers using docker-gen, based on https://github.com/jwilder/nginx-proxy
Note the reverse proxy is configured as sticky using a session-cookie (route). More information here: https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng
git clone https://github.com/open-zimzam/Dockerfiles.git
docker build -t nginx Dockerfiles/nginx
docker build -t nginx-proxy Dockerfiles/nginx-proxy
docker run -d -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock --name nginx-proxy nginx-proxy
dockerhost in this example defined in /etc/hosts, points at IP of DOCKER_HOST
docker run -d -e VIRTUAL_HOST=dockerhost --name nginx-01 nginx
docker run -d -e VIRTUAL_HOST=dockerhost --name nginx-02 nginx
Example logging generated by nginx-proxy during container registration:
docker logs nginx-proxy
forego | starting nginx.1 on port 5000
forego | starting dockergen.1 on port 5100
dockergen.1 | 2015/02/20 13:24:00 Generated '/etc/nginx/conf.d/default.conf' from 1 containers
dockergen.1 | 2015/02/20 13:24:00 Running 'nginx -s reload'
dockergen.1 | 2015/02/20 13:24:00 Watching docker events
dockergen.1 | 2015/02/20 13:24:30 Received event start for container 155d347845d7
dockergen.1 | 2015/02/20 13:24:30 Generated '/etc/nginx/conf.d/default.conf' from 2 containers
dockergen.1 | 2015/02/20 13:24:30 Received event start for container cc661f5fb591
dockergen.1 | 2015/02/20 13:24:30 Generated '/etc/nginx/conf.d/default.conf' from 3 containers
dockergen.1 | 2015/02/20 13:24:30 Running 'nginx -s reload'
Generate two requests
curl dockerhost && curl dockerhost
Inspect both instances; requests are divided round-robin because we don't handle cookies
docker logs nginx-01
172.17.0.99 - - [20/Feb/2015:13:25:00 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.37.1" "192.168.59.3"
docker logs nginx-02
172.17.0.99 - - [20/Feb/2015:13:25:00 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.37.1" "192.168.59.3"
Kill one of the instances
docker stop nginx-01
Inspect nginx-proxy logging
docker logs nginx-proxy
...
dockergen.1 | 2015/02/20 13:25:30 Received event die for container 155d347845d7
dockergen.1 | 2015/02/20 13:25:30 Generated '/etc/nginx/conf.d/default.conf' from 2 containers
dockergen.1 | 2015/02/20 13:25:30 Received event stop for container 155d347845d7
dockergen.1 | 2015/02/20 13:25:30 Generated '/etc/nginx/conf.d/default.conf' from 2 containers
Generate two requests
curl dockerhost && curl dockerhost
Inspect remaining instance (all requests to nginx-02)
docker logs nginx-02
172.17.0.99 - - [20/Feb/2015:13:25:00 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.37.1" "192.168.59.3"
172.17.0.99 - - [20/Feb/2015:13:26:00 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.37.1" "192.168.59.3"
172.17.0.99 - - [20/Feb/2015:13:26:00 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.37.1" "192.168.59.3"
This works pretty much the same as the example with the NginX instances. Docker-gen detects the exposed ports and adds them to the pool. This example uses the Tomcat 8 image in this repository.
Suppose we're running two instances of tomcat-8:1.0
docker run -d -e VIRTUAL_HOST=dockerhost --name tomcat-01 tomcat-8:1.0
docker run -d -e VIRTUAL_HOST=dockerhost --name tomcat-02 tomcat-8:1.0
If we wish to upgrade to a newer version, say, tomcat-8:1.1, we start one of those first:
docker run -d -e VIRTUAL_HOST=dockerhost --name tomcat-03 tomcat-8:1.1
Let it take some hits; some call it a "canary", after the birds used in mines to test the air quality (something was off if the bird died). See if it performs as expected, before you ramp up additional instances.
docker run -d -e VIRTUAL_HOST=dockerhost --name tomcat-04 tomcat-8:1.1
docker run -d -e VIRTUAL_HOST=dockerhost --name tomcat-05 tomcat-8:1.1
Next, we shut down and remove the old instances, causing all existing sessions to switch to one of the new instances:
docker stop tomcat-01 && docker rm tomcat-01
docker stop tomcat-02 && docker rm tomcat-02
Content type
Image
Digest
sha256:04e6b6796…
Size
149.3 MB
Last updated
almost 10 years ago
docker pull dynamind/nginx-proxy