docker networking example

docker.network

1. List the docker network.

# docker network ls

NETWORK ID NAME DRIVER SCOPE

82f0e82e8a52 bridge bridge local

2f29f6c3dc07 host host local

0ee77fd0eaf6 none null local

2. List the bridge's address.

# ifconfig docker0 | grep "inet "

inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0

3. Create two containers.

# docker run -d -it --name=container1 net/centos

# docker run -d -it --name=container2 net/centos

4. List the address of container1.

# docker exec container1 ifconfig eth0 | grep "inet "

inet 172.17.0.2 netmask 255.255.0.0 broadcast 0.0.0.0

5. Create a new network.

# docker network create -d bridge --subnet 172.25.0.0/16 \ isolated_nw

6. Connect container2 to the new network.

# docker network connect isolated_nw container2

7. List the addresses of container2.

# docker exec container2 ifconfig | grep "inet "

inet 172.17.0.3 netmask 255.255.0.0 broadcast 0.0.0.0

inet 172.25.0.2 netmask 255.255.0.0 broadcast 0.0.0.0

inet 127.0.0.1 netmask 255.0.0.0

8. Create a third container and connect it to the new network.

# docker run --network=isolated_nw --ip=172.25.3.3 -itd \ --name=container3 net/centos

9. Check connectivity from container2 to container3.

 

# docker exec container2 ping -w 1 172.25.3.3

PING 172.25.3.3 (172.25.3.3) 56(84) bytes of data.

64 bytes from 172.25.3.3: icmp_seq=1 ttl=64 time=0.088 ms

64 bytes from 172.25.3.3: icmp_seq=2 ttl=64 time=0.072 ms

10 Check connectivity from container3 to container1.

# docker exec container3 ping -w 1 172.17.0.2

PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.

--- 172.17.0.2 ping statistics ---

2 packets transmitted, 0 received, 100% packet loss, time 999ms

11. Clean up.

# docker rm -f container1 container2 container3

container1

container2

container3

# docker network rm isolated_nw

isolated_nw

 

This entry was posted in Uncategorized. Bookmark the permalink.

Comments are closed.