Docker Community Forums

Share and learn in the Docker community.

How do I launch conts each other pingable by hostname?

Hi!

How to “properly” launch two containers, if containers should be pingable each other by hostname? Base image is the same, if it plays a role.

The problem is:
1)
If second container do not exist, first container can not be launched with “–link=a1b2c3d4e5f6”, as there is no second container’s ID, as an image was not ever launched before.
2)
A try to use hostname of a future “neigbour” like “–link=client-host-name” do not works. May be as it is a bug, may be - not.

How I’m doing it now:
start container “server” without “–link”,
start “client” with "–link"
stop “server”,
start container “server” with “–link” to “client”.

Now they are both pingable by hostname in both directions.
But how can I do it without restrat?
Just only launch first, second container and it works?

Here are commands, how I’m doing it:

# Clean up all: sudo docker rm -f $( sudo docker ps --format={{.ID}} -a )

rm -f /tmp/srv-container-id
docker run --rm=false --detach --tty=true --interactive=false --hostname=srv --cidfile=/tmp/srv-container-id 9264aa2ebbdd
d2d7d21c6a4f

docker run --rm=false --detach --tty=true --interactive=false --hostname=clnt --cidfile=/tmp/clnt-container-id --link=d2d7d21c6a4f 9264aa2ebbdd
05e4ccf9080c

docker stop d2d7d21c6a4f
rm -f /tmp/srv-container-id

docker run --rm=false --detach --tty=true --interactive=false --hostname=srv --cidfile=/tmp/srv-container-id --link=05e4ccf9080c 9264aa2ebbdd
a8fdbfd95338