dingoglotz
dingoglotz

Reputation: 2833

Docker ping container on other nodes

I have 2 virtual machines (VM1 with IP 192.168.56.101 and VM2 with IP 192.16.56.102 which can ping each other) and these are the steps I'm doing:

- Create consul container on VM1 with 'docker run -d -p 8500:8500 --name=consul progrium/consul -server -bootstrap'
- Create swarm manager on VM1 with 'docker run -d -p 3376:3376 swarm manage -H 0.0.0.0:3376 --advertise 192.168.56.101:3376 consul://192.168.56.101:8500
- Create swarm agents on each VM with 'docker run -d swarm join --advertise <VM-IP>:2376 consul://192.168.56.101:8500

If i run docker -H 0.0.0.0:3376 info I can see both nodes connected to the swarm and they are both healthy. I can also run container and they are scheduled to the nodes. However, If I create a network and assign a few nodes to this network and then SSH into one node and try to ping every other node I can only reach the nodes which are running on the same virtual machine.

Both Virtual Machines have these DOCKER_OPTS:

DOCKER_OPTS = DOCKER_OPTS="--cluster-store=consul://192.168.56.101:8500 --cluster-advertise=<VM-IP>:0 -H tcp://0.0.0.0:2376 -H unix:///var/run/docker.sock"

Upvotes: 4

Views: 4065

Answers (2)

abronan
abronan

Reputation: 3439

You can only ping containers on the same node because you attach them to a local scope network.

As suggested in the comments, if you want to ping containers across hosts (meaning from a container on VM1 to a container on VM2) using docker swarm (or docker swarm mode) without explicitly opening ports, you need to create an overlay network (or globally scoped network) and assign/start containers on that network.

To create an overlay network:

docker network create -d overlay mynet

Then start the containers using that network:

  • For Docker Swarm mode:

      docker service create --replicas 2 --network mynet --name web nginx
    
  • For Docker Swarm (legacy):

      docker run -itd --network=mynet busybox
    

For example, if we create two containers (on legacy Swarm):

docker run -itd --network=mynet --name=test1 busybox
docker run -itd --network=mynet --name=test2 busybox

You should be able to docker attach on test2 to ping test1 and vice-versa.

For more details you can refer to the networking documentation.

Note: If containers still can't ping each other after the creation of an overlay network and attaching containers to it, check the firewall configurations of the VMs and make sure that these ports are open:

  • data plane / vxlan: UDP 4789
  • control plane / gossip: TCP/UDP 7946

Upvotes: 2

Vanuan
Vanuan

Reputation: 33442

I don't have a direct quote, but from what I've read on Docker GitHub issue tracker, ICMP packets (ping) are never routed between containers on different nodes.

TCP connection to explicitly opened ports should work. But as of Docker 1.12.1 it is buggy.

Docker 1.12.2 has some bug fixes wrt establishing a connection to containers on other hosts. But ping is not going to work across hosts.

Upvotes: 2

Related Questions