Docker Communication Between Containers
If you want to be able to ping or basically access a running docker container from another container by simply using the docker name rather than an IP address, DNS must work well. Docker natively provides DNS capability to get containers in the same network to communicate between containers over their DNS names as IP addressing changes as containers go in and out.
Basics of Docker Networking;
Docker Network Defaults;
Each container connected to a private virtual network “bridge”
Each virtual network routes through NAT firewall on host IP
All containers on a virtual network can talk to each other without -p
Docker Network Best Practices;
Create a new virtual network for each app:
Step 1:
Let us begin by creating two containers; I am using the NGINX Image.
You can download the nginx to the Local Cache.
$>docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE httpd latest 417af7dc28bc 7 days ago 138MB nginx latest 7e4d58f0e5f3 12 days ago 133MB mysql latest e1d7dc9731da 13 days ago 544MB alpine latest a24bb4013296 3 months ago 5.57MB
Get the image by typing “docker container pull nginx”
docker container run -d --name container1 -p 8080:80 nginx docker container run -d --name container2 -p 8088:80 nginx
Let us verify
$docker container ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a1147ca12e97 nginx "/docker-entrypoint.…" 1 hours ago Up 10 minutes 0.0.0.0:8080->80/tcp container1 0e364de8f313 nginx "/docker-entrypoint.…" 1 hours ago Up 10 minutes 0.0.0.0:8088->80/tcp container2
Important note: it is of utmost importance to explicitly specify a name with –name for your containers. The reason being that it will not work with the auto-generated names that Docker assigns to your container(s).
Step 2:
Create a new network:
docker network create nginx-network
Verify if this network is listed in the docker networks
C:\>docker network ls NETWORK ID NAME DRIVER SCOPE 25d4ec1592eb bridge bridge local ba0017e88f28 host host local 06658aee8f0c nginx-network bridge local 70be58caf984 none null local 33668dd3b17f webservices bridge local
Step 3:
Connect your containers to the network:
docker network connect nginx-network container1 docker network connect nginx-network container2
Step 4:
Verify if your containers are part of the newly created network (nginx-network):
docker network inspect nginx-network
"ConfigOnly": false, "Containers": { "0e364de8f3134e242e513a6cf3da4b69bb38fb7ef17213a309a7bda27b423b3a": { "Name": "container1", "EndpointID": "fdf973b2840adea185bec76c8684bb1c404a21ccb9947c16c58119b350aebb36", "MacAddress": "02:42:ac:12:00:03", "IPv4Address": "172.18.0.3/16", "IPv6Address": "" }, "a1147ca12e97cb054af40ab67255d9dd5817d7197695e3756ee5fd614195de77": { "Name": "container2", "EndpointID": "6edb537acdc3b1ec6ee233993d9e6d28cd8a62055600300d0e77e48c94ee9a88", "MacAddress": "02:42:ac:12:00:02", "IPv4Address": "172.18.0.2/16", "IPv6Address": "" }
Install ping for nginx as not all images come prepackaged with the ping utility
Run docker container1 and install ping. you can do so by going to the bash of the container by typing;
These two commands are needed. You can go ahead line by line as per below or in one single line as per the instruction 2 below;
Instruction 1:
apt-get update apt-get install iputils-ping
Instruction 2:
$docker container exec -it container1 bash root@a1147ca12e97:/# root@a1147ca12e97:/# apt-get update && apt-get install iputils-ping
Repeat above step for container2
Final Step:
Finally test the connection between container1 and container2.
$docker container exec -it container1 ping container2 PING container2 (172.18.0.3) 56(84) bytes of data. 64 bytes from container2.nginx-network (172.18.0.3): icmp_seq=1 ttl=64 time=0.050 ms 64 bytes from container2.nginx-network (172.18.0.3): icmp_seq=2 ttl=64 time=0.043 ms 64 bytes from container2.nginx-network (172.18.0.3): icmp_seq=3 ttl=64 time=0.142 ms 64 bytes from container2.nginx-network (172.18.0.3): icmp_seq=4 ttl=64 time=0.145 ms 64 bytes from container2.nginx-network (172.18.0.3): icmp_seq=5 ttl=64 time=0.142 ms 64 bytes from container2.nginx-network (172.18.0.3): icmp_seq=6 ttl=64 time=0.066 ms 64 bytes from container2.nginx-network (172.18.0.3): icmp_seq=7 ttl=64 time=0.047 ms ^C --- container2 ping statistics --- 7 packets transmitted, 7 received, 0% packet loss, time 129ms rtt min/avg/max/mdev = 0.043/0.090/0.145/0.047 ms
Hope you have enjoyed this article? Look out for more on this website. Bookmark by pressing (CTRL + D)
Follow this link to learn more about the amazing nginx docker image.