Dynamic reverse proxy for Docker containers
Hosting multiple sites on a single server is nothing new - it's as old as
the web itself. nginx already supports this use case, but an
file needs to be updated and nginx itself reloaded whenever a config change
(e.g. backend server IP address, listening domain name) happens.
This is problematic when using Docker containers as backend servers because
typically Docker containers have dynamically assigned IP addresses.
solves this by looking up backend server containers' IP addresses on-demand
inside the nginx process itself. This is achieved using OpenResty.
docker run -d \ --label com.glassechidna.doxyproxy.HttpHost=example.com \ --label com.glassechidna.doxyproxy.HttpPort=80 \ nginx docker run -d \ --label com.glassechidna.doxyproxy.HttpHost=example2.com \ --label com.glassechidna.doxyproxy.HttpPort=80 \ nginx docker run -d --net=host -v /var/run/docker.sock:/var/run/docker.sock -p 8080:8080 glassechidna/doxyproxy curl -H 'Host: example.com' http://localhost:8080/ # forwarded to the first container curl -H 'Host: example2.com' http://localhost:8080/ # forwarded to the second container
doxyproxy determines the container to forward traffic to by looking for a
container with a matching
then uses the
com.glassechidna.doxyproxy.HttpPort label to know which port
the web traffic should be forwarded to in that container.
Note: In the above example the
--net=host networking mode command is not
strictly necessary as the three containers are launched into the same default
Docker bridge network and hence the
doxyproxy container has connectivity to
the other two. Where the host network mode does become necessary is when you
wish to forward traffic to containers in other Docker networks.
This project is originally based on the great work done by