Monday, August 2, 2021

Docker Container Networking

The --rm option means automatically remove the container when it exits.

docker run --rm <container_id>


To know the network properties of the 

docker inspect --format='{{json .NetworkSettings.Networks}}'  web


Below is to get the IP address of a container 


docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' web


Now to check if the container has access to the other container, can just do a ping by entering into the container 


docker container exec -it db bash


root@a205f0dd33b2:/# ping 172.17.0.2

ping 172.17.0.2

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

^C

--- 172.17.0.2 ping statistics ---

44 packets transmitted, 0 received, 100% packet loss, time 43185ms



In this above case, the db container does not have access to the other container, which is web. To provide access, below command can be run 

docker network connect my_bridge web


Docker networking allows you to attach a container to as many networks as you like. You can also attach an already running container. Go ahead and attach your running web app to the my_bridge.


$ docker container exec -it db bash


root@a205f0dd33b2:/# ping web

PING web (10.0.0.2) 56(84) bytes of data.

64 bytes from web (10.0.0.2): icmp_seq=1 ttl=64 time=0.095 ms

64 bytes from web (10.0.0.2): icmp_seq=2 ttl=64 time=0.060 ms

64 bytes from web (10.0.0.2): icmp_seq=3 ttl=64 time=0.066 ms

^C

--- web ping statistics ---

3 packets transmitted, 3 received, 0% packet loss, time 2000ms

rtt min/avg/max/mdev = 0.060/0.073/0.095/0.018 ms



Below is with and without container 






References:

https://docs.docker.com/engine/tutorials/networkingcontainers/

No comments:

Post a Comment