Search code examples
dockernetwork-programmingpinghostnamedocker-network

How to enable docker containers ping each other via their host names?


I'd like to be able to ping a docker hostname from another docker container. how can i do that ? My containers are on the same docker network.

Can someone guide me where should I look or search, I'm very newbie in networking and do not know about what should i search to fix this problem.


Solution

  • The link option is deprecated so I suggest you to use the --network-alias option in addition to the --name flag (see the documentation here and here for more info).

    For example, admiting you have a network called test, you can run these two commands (in two separate terminal)

    docker run -it --rm --name debian1 --network=test --network-alias=debian1 debian:jessie /bin/bash
    docker run -it --rm --name debian2 --network=test --network-alias=debian2 debian:jessie /bin/bash
    

    Then you will be able to ping containers according to their network-alias