1.What is the difference between --name
and --hostname
in docker run
command?
2.Why foo
can't reach bar
by its hostname
= barhost
?
create network and two containers connected to it:
docker network create test
docker run --rm -dit --name bar --network test --hostname barhost alpine:latest
docker run --rm -it --name foo --network test --hostname foohost alpine:latest
ping barhost
from foo
terminal
ping -c2 barhost
gives result:
bad address 'barhost'
but ping bar
from foo
ping -c2 bar
is successful:
PING bar (172.31.0.2): 56 data bytes 64 bytes from 172.31.0.2: seq=0 ttl=64 time=0.260 ms 64 bytes from 172.31.0.2: seq=1 ttl=64 time=0.155 ms
--- bar ping statistics --- 2 packets transmitted, 2 packets received, 0% packet loss
3.If you can't reach bar
by its hostname
from foo
why it is possible to do that from within bar
?
# assuming you've created network test from point 2.
docker run --rm -it --name bar --network test --hostname barhost alpine:latest
ping barhost
from bar
terminal
ping -c2 barhost
is successful:
PING barhost (172.31.0.2): 56 data bytes 64 bytes from 172.31.0.2: seq=0 ttl=64 time=0.135 ms 64 bytes from 172.31.0.2: seq=1 ttl=64 time=0.127 ms
--- barhost ping statistics --- 2 packets transmitted, 2 packets received, 0% packet loss