Tested on Docker 1.9+
~ > docker version
Client:
Version: 1.9.0
API version: 1.21
Go version: go1.5.1
Git commit: 76d6bc9
Built: Tue Nov 3 21:17:46 UTC 2015
OS/Arch: darwin/amd64
Server:
Version: 1.9.0
API version: 1.21
Go version: go1.4.3
Git commit: 76d6bc9
Built: Tue Nov 3 19:20:09 UTC 2015
OS/Arch: linux/amd64
docker run -it --name test1 alpine sh
docker run -it --name test2 alpine sh
Note: they cannot communicate
In test1
:
/ # ping -c 1 -W 1 test2
ping: bad address 'test2'
/ # ping -c 1 -W 1 test2.testbed
ping: bad address 'test2.testbed'
The containers cannot communicate since they are not on the same network.
/ # cat /etc/hosts
172.17.0.2 c51a702ba714
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
This is the default /etc/hosts
for a container. There is no reference to other running containers.
~ > docker network create testnet
341381b9251974509a83760276b9de3c1afd9d6445b6a6bfedf76e14e0e04605
~ > docker network connect testnet test1
~ > docker network connect testnet test2
In test1
:
/ # ping test2
PING test2 (172.18.0.3): 56 data bytes
64 bytes from 172.18.0.3: seq=0 ttl=64 time=0.080 ms
64 bytes from 172.18.0.3: seq=1 ttl=64 time=0.085 ms
--- test2 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.080/0.082/0.085 ms
/ # ping test2.testbed
PING test2.testbed (172.18.0.3): 56 data bytes
64 bytes from 172.18.0.3: seq=0 ttl=64 time=0.140 ms
64 bytes from 172.18.0.3: seq=1 ttl=64 time=0.070 ms
64 bytes from 172.18.0.3: seq=2 ttl=64 time=0.069 ms
--- test2.testbed ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.069/0.093/0.140 ms
/ # nslookup test2
Server: (null)
Address 1: ::1 localhost
Address 2: 127.0.0.1 localhost
Name: test2
Address 1: 172.18.0.3 test2
/ # nslookup test2.testbed
Server: (null)
Address 1: ::1 localhost
Address 2: 127.0.0.1 localhost
Name: test2.testbed
Address 1: 172.18.0.3 test2
/ # cat /etc/hosts
172.17.0.2 c51a702ba714
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.18.0.3 test2
172.18.0.3 test2.testnet
Note that in the /etc/hosts
file there are 2 entries for test2, one is the network name that was created earlier.
Containers cannot communicate if they are not on the same network, even if you know the ip address of the container
~ > docker run -it --name test3 alpine sh
/ # ping test2
ping: bad address 'test2'
/ # ping test2.testnet
ping: bad address 'test2.testnet'
/ # ping -c 1 -W 1 172.18.0.3
PING 172.18.0.3 (172.18.0.3): 56 data bytes
--- 172.18.0.3 ping statistics ---
1 packets transmitted, 0 packets received, 100% packet loss