Networking in docker made easy.

Networking concepts are important to understand as they play an important role in real time deployments whether it could be through docker or kubernetes without properly exposing the ports and communication of your apps it may lead to issues and result in downtime. So here I will try to explain as easily as i could networking in docker.

I here assume that you are familiar with the containers and basics of docker. In this tutorial, i am going to use two busybox containers. In case if you dont know what busybox container is, it combines tiny versions of many common UNIX utilities into a single small executable or you can understand it as a lightweight Linux.First thing first lets check the avalaible network that docker uses run the command.

docker network ls

here it will show the network ID and name that is being used in docker the default used is bridge network it is responsible for providing IP address to the container. Run the command

docker network inspect bridge

here it will show you a json file that contains the configuration for the bridge. By using this bridge network configuration it will provide the IP address to your containers when you run the docker run command. It provides an IP from a specific subnet you can find the IP subnet range from the configuration when you run the above command. Now its time to understand how to make communication happen between two containers run the command

docker run -it --name busybox1 busybox
//INTERACTIVE SHELL WILL GET OPEN AFTER PULLING THE IMAGE 
//RUN
hostname -i
hostname

You will get the IP address of the busybox1 container. Now open another tab of the terminal don't exit the first session of busybox1. Now after opening another Terminal run

docker run -it --name busybox2 busybox

hostname -i
hostname

Note the IP address of second busybox container now from the first terminal ping the second container by its IP address run ping <ipaddress> now you will see it is successfully receiving bytes from the second busybox container. Now This approach is not used in the real-time scenario or particularly speaking in production-grade environments because in case if the container gets stopped or crashes due to some reason it will get new random IP every time it restarts. So this approach is not suitable, Therefore we will make use of DNS as you run the above command hostname you will get the hostname of each container it will be some random letters and words that docker assigns. We will try to ping by its hostname take the hostname of the second container and paste it in the terminal of first terminal of busy box you will see it is not responding because no hostname is not attached in ALIAS inside the bridge network. To make communication happen we have to create our own custom network. To do that run the command

docker network create custom
docker network ls

Now you will see the custom network created. We have to attach both busybox containers to this custom network. To do this run the command

docker run -it --name busyboxtest1 --network custom -h busybox-one busybox

Here -h is used to give the hostname as per our requirement to containers. Now in the second terminal run the command

docker run -it --name busyboxtest2 --network custom -h busybox-two busybox

Now we will try to ping using the hostname from the first terminal and run the command ping busybox-one. You will see it working we are able to receive packets from the second container.

I hope I was able to explain easily. Now whenever there is a requirement for communication between two containers such as to Communicate the backend with the database container you can use the same approach by creating a custom network and attaching to it.

Did you find this article valuable?

Support MUSAIB SHAIKH'S BLOG by becoming a sponsor. Any amount is appreciated!