After upgrading to Docker engine 1.10 (from 1.08) I've noticed that my reverse proxy configuration is not working anymore.
All my apps (including Nginx for reverse proxies) are containerized and were communicating via container names. Here's an example for virtual hosts part in Nginx:
server {
server_name jobs;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://jenkins:8080;
}
}
Now, I can ping Jenkins container from Nginx container only via IP but not anymore via container name. As IPs are constantly changed due to updates, redeployments, etc. is there a better networking way of avoiding defining IPs in reverse proxy configuration?
Legacy --link
is not an option as there are lots of containers.
Basic idea is to use docker inspect to obtain the pid of the container, then enter the uts namespace of the container via nsenter . Running hostname inside that namespace will change the hostname for the docker instance that shares that namespace. Save this answer.
In the same way, a container's hostname defaults to be the container's ID in Docker. You can override the hostname using --hostname . When connecting to an existing network using docker network connect , you can use the --alias flag to specify an additional network alias for the container on that network.
We can do this by running docker exec -it <CONTAINER ID> /bin/bash . Install the ping command and ping the service task running on the second node where it had a IP address of 10.0. 0.3 from the docker network inspect overnet command. Now, lets ping 10.0.
You can check the network-scope alias which comes with docker network connect
and docker run
.
Starting a container with an alias allows your NGinx to reverse proxy to that alias in its config.
At runtime, that alias will resolve to the container that you started later.
See an example in "Docker Networking: Auto-discovering host names in a bridge network".
Note that you will need a key-value store to manage your container in a docker 1.10+ network.
Note (July 2016) with docker 1.12 and its swarm mode, it becomes even simpler.
See for instance "The beautiful networking stack in Docker Swarm mode"
The docker swarm will define an overlay network and a key-value store for you! The containers will see each others.
Another concrete example: "NGINX as a Reverse Proxy for Docker Swarm Clusters"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With