https://docs.docker.com/network/network-tutorial-standalone/

alpine-net网络都能通过名称来访问,bridge默认网络只能通过ip访问

 


1. 创建alpine-net
docker network create --driver bridge alpine-net

2. 
docker network ls

3.  172.18.0.0/16  bridge默认是172.17.0.0/16 
docker network inspect alpine-net

4. 
docker run -dit --name alpine1 --network alpine-net alpine ash
docker run -dit --name alpine2 --network alpine-net alpine ash
docker run -dit --name alpine3 alpine ash
docker run -dit --name alpine4 --network alpine-net alpine ash
docker network connect bridge alpine4

5. docker ps -a

6. alpine3 alpine4
docker network inspect bridge

7. alpine1 alpine2 alpine4
docker network inspect alpine-net

 


# 进入alpine1
docker attach alpine1

# 能ping通1 2 4
ping -c 2 alpine1
ping -c 2 alpine2
ping -c 2 alpine4

#ping不通 alpine3
ping -c 2 alpine3

ping: bad address 'alpine3'

 ping不通alpine3,但能通过ip访问 ping -c 2 172.17.0.5    (默认bridge网络中是不支持容器名称通信的,但支持ip通信)

docker attach alpine4
/ # ping -c 2 alpine1
PING alpine1 (172.18.0.4): 56 data bytes
64 bytes from 172.18.0.4: seq=0 ttl=64 time=0.152 ms
64 bytes from 172.18.0.4: seq=1 ttl=64 time=0.111 ms

--- alpine1 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.111/0.131/0.152 ms
/ # ^C
/ # ping -c 2 alpine2
PING alpine2 (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.115 ms

--- alpine2 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.115/0.127/0.140 ms
/ # ping -c 2 alpine3
ping: bad address 'alpine3'
/ # ping -c 2 alpine4
PING alpine4 (172.18.0.2): 56 data bytes
64 bytes from 172.18.0.2: seq=0 ttl=64 time=0.063 ms
64 bytes from 172.18.0.2: seq=1 ttl=64 time=0.078 ms

ping -c 2 172.17.0.5
PING 172.17.0.5 (172.17.0.5): 56 data bytes
64 bytes from 172.17.0.5: seq=0 ttl=64 time=0.206 ms
64 bytes from 172.17.0.5: seq=1 ttl=64 time=0.112 ms

--- alpine4 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.063/0.070/0.078 ms


$ docker container stop alpine1 alpine2 alpine3 alpine4

$ docker container rm alpine1 alpine2 alpine3 alpine4

$ docker network rm alpine-net