简体   繁体   中英

Problems getting docker containers to see (ping) each other by name

I have three docker containers,

  1. java container (JC): for my java application (spring boot)
  2. elasticsearch container (EC): for ElasticSearch
  3. test container (TC): testing container to troubleshoot with ping test

Currently, the JC cannot see the EC by "name". And when I say "see" I mean if I do a ping on the JC to EC, I get a ping: unknown host . Interestingly, if I do a ping on the TC to EC, I do get a response.

Here is how I start the containers.

  1. docker run -dit --name JC myapp-image
  2. docker run -d --name EC elasticsearch:1.5.2 elasticsearch -Des.cluster.name=es
  3. docker run --rm --name TC -it busybox:latest

Then, to ping EC from JC, I issue the following commands.

docker exec JC ping -c 2 EC

I get a ping: unknown host

With the TC, since I am already at the shell, I can just do a ping -c 2 EC and I get 2 replies.

I thought maybe this had something to do with my Java application, but I doubt it because I modified my Dockerfile to just stand up the container. The Dockerfile looks like the following.

FROM java:8
VOLUME /tmp

Note that you can create the above docker image by docker build -no-cache -t myapp-image . .

Also note that I have Docker Weave Net installed, and this does not seem to help getting the JC to see the EC by name. On the other hand, I tried to find the IP address of each container as follows.

  1. docker inspect -f '{{ .NetworkSettings.IPAddress }}' JC --> 172.17.0.4
  2. docker inspect -f '{{ .NetworkSettings.IPAddress }}' EC --> 172.17.0.2
  3. docker inspect -f '{{ .NetworkSettings.IPAddress }}' TC --> 172.17.0.3

I can certainly ping EC from JC by IP address: docker exec JC ping -c 2 172.17.0.2 . But getting the containers to see each other by IP address does not help as my Java application needs a hostname reference as a part of its configuration.

Any ideas on what's going on? Is it the container images themselves? Why would the busybox container image be able to ping the ElasticSearch container by name but the java container not?

Some more information.

  • VirtualBox 5.0.10
  • Docker 1.9.1
  • Weave 1.4.0
  • CentOS 7.1.1503
  • I am running docker inside a CentOS VM on a Windows 10 desktop as a staging environment before deployment to AWS

Any help is appreciated.

Within the same docker daemon, use the old --link option in order to update the /etc/hosts of each component and make sure one can ping the other:

docker run -d --name EC elasticsearch:1.5.2 elasticsearch -Des.cluster.name=es
docker run -dit --name JC --link ED myapp-image
docker run --rm --name TC -it busybox:latest

Then, a docker exec JC ping -c 2 EC should work.

If it does not, check if this isn't because of the base image and a security issue: see " Addressing Problems with Ping in Containers on Atomic Hosts ".
JC is based on docker/_java:8 , itself based on jessie-curl , jessie .

Containers in this default network are able to communicate with each other using IP addresses. Docker does not support automatic service discovery on the default bridge network. If you want to communicate with container names in this default bridge network, you must connect the containers via the legacy docker run --link option. docs.docker.org .

It should also work using the new networking.

docker network create -d bridge non-default
docker run --net non-default ...

There isn't a specific option which applies this behavior to the default network (AFAICT from looking at docker network inspect ). I guess it's just triggered by the option "com.docker.network.bridge.default_bridge".

In the first part of another question, it's suggested this was changed in Docker 1.9. Note that Docker 1.9 was when they turned on the new networking system in the stable release. The section of the userguide that I quoted from above, did not exist in version 1.8. Docker 1.9.0 "bridge" versus a custom bridge network results in difference in hosts file and SSH_CLIENT env variable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM