This is a simple NGINX load balancer that automatically reconfigures itself according to running containers.
nginx-lb
search for running containers and group them by application name*- Configures an NGINX virtual host for every app load balancing between its Docker instances.
- Monitors Docker for events
- Repeat setup when an event is found
- An application name is detected by splitting containers name with _ and discarting the last bit. The application name is used as the app domain inside NGINX. A few examples of valid container names:
myapp.127.0.0.1.xip.io_1
myapp.127.0.0.1.xip.io_2
dev.myapp.com_1
. For those examples the app name would bemyapp.127.0.0.1.xip.io
,myapp.127.0.0.1.xip.io
anddev.myapp.com
, respectively. Note thatmyapp.127.0.0.1.xip.io
has two instances to be load balanced to.
nginx-lb
is an automated Docker image. Pull it with:
docker pull rochacon/nginx-lb
DOCKER_HOST
- HTTP address of the Docker host. e.ghttp://172.42.17.1:2375
PORTS
- Comma separated list of containers ports to monitor. e.g 80,8080
Launch nginx-lb
docker run -d -p 80:80 --name nginx-lb \
-v /var/run/docker.sock:/var/run/docker.sock:ro \
-e PORTS=80,8080 \
rochacon/nginx-lb
Use the DOCKER_HOST
environment to monitor a Docker daemon listening on TCP.
Launch some app instances
docker run -p 80 --name myapp.example.com_1 myimage
docker run -p 80 --name myapp.example.com_2 myimage
docker run -p 80 --name myapp.example.com_3 myimage
Hit application
curl myapp.example.com
docker exec nginx-lb cat /etc/nginx/nginx.conf
MIT