Search code examples
network-programmingdockerportpinghost

Can docker host ping its containers?


I'm running docker on mac, my docker is running centos and ifconfig shows eth0 address is "172.17.0.2".

I tried to ping this 172.17.0.2 in my terminal but failed. So except using "docker run" command, is there a way to access to container by it's services like sshd?

I searched internet and found port mapping, so I added "-P" option to run it:

docker run -itP centos6.5 bash

It should assign a random port mapping right? But the list command doesn't show any "port" information, like below(have scroll a bit)

docker container list
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
136ab365eddd        centos6.8_dev       "bash"              11 seconds ago      Up 10 seconds                           practical_wilson

This is weird: why port mapping doesn't work? I tried tcping:

$tcping localhost 5000
localhost port 5000 closed.

Solution

  • Can docker host ping its containers?

    no.

    not on Mac, at least.

    and on linux, it only works incidentally and is not something you should do anyways.

    Docker isn't a virtual machine and shouldn't be treated like it is. You should be thinking of it as a virtual application, instead.

    if you need to get into the service via tcp/ip port, you need to map the port number from the container.

    docker run -p 1234:1234 my-image

    where 1234 is the tcp/ip port.

    this creates a service listening at localhost:1234 on your host machine.