Search code examples
dockerdocker-composeweave

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.


Solution

  • 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.