Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't ping Docker containers via hostnames/names

Tags:

docker

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.

like image 757
Edgaras Avatar asked Mar 22 '16 22:03

Edgaras


People also ask

How do I assign a hostname to a Docker container?

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.

Do containers have hostnames?

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.

How do I ping a URL in a Docker container?

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.


1 Answers

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"

like image 184
VonC Avatar answered Oct 22 '22 09:10

VonC