Containerization is the most in-demand by the enterprises currently. And it requires strong networking skills for ensuring proper container architecture, and hence, we need to know about Docker networking. In this article, we talk about Docker Networking, and we will go through the topics like what “Docker” is, what Docker networking is, what are the goals of Docker networking, what is CNM and network drivers, and finally end up with the Hands-on. And if you want to learn DevOps, you can contact Naresh I Technologies. We provide complete DevOps training for all DevOps certifications. Naresh I Technologies also is the number one computer training institute in Hyderabad and among the top five computer training institutes in India.
So, what is Docker?
For understanding “Docker,” we need to understand how applications got deployed before. And how we deploy the application after the introduction of the containers. Previously all the apps used to share the same libraries and dependencies. However, now each app only shares the kernel, and they come with the same set of libraries and dependencies.
The old way, the application was on the host. Hence, all the applications share the libraries in the operating system. Though, with containerization, the OS now has only the kernel in common with all the applications. Hence, the application has nothing to do with the libraries of the other applications.
Hence, “Dockers” happens to be open source for the development, shipping, and running of the application. It enabled the user to lay a boundary between the application and the infrastructure. It was via the containers for delivering the software quickly.
However, how these containers can communicate among themselves in different situations? We need to study Docker Networking for this.
The Docker Networking
Before looking at the Docker networking, let’s look at the workflow of “Docker.”
The developer writes the code. It states the application requirements or the dependencies in a docker file, and via it, we get the Docker images. Hence, all the dependencies are available in the “image.”
Hence, the docker containers are the Dockers runtime instance of the Docker Image. And these images are loaded on the Docker Hub or the Git repository that contains the public/private repositories.
Hence, you can pull the image from the public repositories and then upload the “image” to the “Docker hub.” And from the Docker Hub, different teams like the Quality Assurance and the production team pull the image and develop their containers. And these containers communicate among them via the network for acting required. And this we know as Docker networking.
Hence the docker networking is the communication medium via which the isolated containers talk to each other in different situations for performing the required actions.
What are the goals of Docker Networking?
The dockers are flexible, and you can enable numerous applications on different platforms for communication among them.
Cross-Platform – The dockers make use of the cross-platform that operates over different servers through the Docker swarm clusters.
“Scalability” – It is a “Distributed network.” that enables applications for growing and scaling individually as well as meanwhile also ensuring performance.
Decentralized – The “docker” makes use of the decentralized network that spreads the applications and makes them highly available. In this event, suppose a container or the host is missing from the pool of resources. You can come up with additional resources. Or you can move to the services which are available still.
“User-Friendly” – Docker helps us to automate the deployment of the services. And thus, make them easily usable in everyday life.
Support : The “docker” also caters to good support. Hence, it caters to us the ability to use the docker enterprise edition and ensure all of the functionality is easy and straightforward. And that makes the docker platform easy to use.
For enabling the goal, we need something like Container Network Model.
Container Network Model or the CNM
You need to realize the meaning of the libnetwork before you can understand the Container Network Model.
The libnetwork happens to be the open-source Docker library. That helps to implement the key-concept, which makes the CNM.
Hence, the Container Network Model (CNM) can standardize the steps required for ensuring the networking for the containers with the help of the multiple network drivers. It requires the distributed key-value store in the form of the console for storing the network configuration.
The CNM comes with the interface for the IPAM plugins as well as the network plugins.
The IPAM plugin APIs are for creating/ deleting the address pools. And it also allocates deallocates the IP addresses of the container. And the network plugin APIs are for creating and deleting the networks and adding and removing the “containers” from the “network.”
The CNM comprises of the “5” objects. They are Network controller, driver, Endpoint, Network, and the Sandbox.
The Container Network Model Objects
The network controller : This caters to the entry-point into the Libnetwork, which exposes the simple APIs for allocating the Docker engine and managing the networks. And the Libnetwork caters to numerous inbuilt and remote drivers. The network controller leverages the user to attach to the provided network the particular driver.
Driver : This owns the network, and it manages the network through multiple drivers in picture satisfying the use cases and the deployment scenarios.
Network: It forms the connectivity between the group of endpoints. And the endpoints are in the same network and are isolated from the rest. And as the network gets created/updated, the related driver gets notified of this event.
End Point : This ensures the connectivity among the services exposed in the network by the container with other “services.” Those are catered to you by containers in the network. The endpoint is the service and is necessarily a particular container. And the endpoints have the global scope inside the cluster.
Sandbox : It’s when the user requests to create the endpoint on the network. The Sandbox can have multiple endpoints attached to various network configurations like the IP-address, MAC-addresses, DNS, and routes.
And these are the “5” main objects of CNM. Let us now consider various network drivers that are involved in the Docker networking. They are the bridge, Host, Node, Overlay, and the macvlan.
Bridge : It is the private default internal network made by the “docker” on the host. Hence, each of them gets an internal IP address. And each of them can access each other with the help of the “internal IP.” The bridge networks are for the application that runs on the standalone containers that require communication.
Host : This driver eradicates the network isolation amid the host of the “docker” and the container to ensure the direct usage of the “host networking.” Hence, with this, you are not allowed to run the multiple web container on the same “host.” And you cannot run on the same port all the containers in the host. The network makes use of this port.
“None” : In this type of network, the container gets detached from all the “network.” And it does not have access to the external network or any other “containers.” Hence, this network is applied when we need complete disabling of the network stack for a container, and we only need to look back at the device.
Overlay : This makes an internal private network distributed over all the nodes participating in the swarm cluster. Hence, the Overlay networks ensure communication amid the swarm service and the standalone container, or the two standalone containers on various Docker Daemons.
Macvlan : Macvlan allows you to allocate for the container the MAC address. And it makes it look like the physical device on the network. And, the Docker daemon helps in routing via the MAC addresses. It’s the best choice when you need to connect to the “physical network” directly and not through the Docker host network stack for routing.
And this completes the theory part. Now let us have the practical knowledge of network creation and making the containers communicate with each other.
Hands-On : Hence, let’s assume that we have installed Docker on the system. Let’s now have a look at the scenario. Suppose you need to store the course name and the course ID, and for that, you require the web application. And generally, we need one container for the web application, and we need one other “container” for the MySQL for the backend, and the MySQL container linked to the web application container.
Let’s execute this now:
The steps required are like initializing the Docker Swarm for forming the Swarm cluster. Then, we need to create an overlay network. And, then we need the creation of the services for both the web application and MySQL. And then, we need to connect the application via the network. So, let’s start.
Step 1 : Firstly, initialize the Docker Swarm on the machine.
Step 2 : Now, in case we need to join the manager node to the worker node, we need to copy the link which you get when you are initializing the swarm at the worker node.
Step 3 : We need to create the overlay network.
Step 4.1 : We make the service webapp1. And then we make use of the network. It’s that you made for deploying the service on the swarm cluster.
Step 4.2 : Now, you need to check whether the service got created.
Step 5.1 : Now, you need to make the MySQL and make use of the network. You made this for deploying the service on the swarm cluster.
Step 5.2 : Now we need to check whether the services is made or not
Step 6.1 : Now, you need to check which container runs on the master node and moves to the app1/webapp container.
Step 6.2 : You will find that only the webapps service is on the manager node. Hence, let’s move inside the webapp container.
Step 7 : Now we need to change the $servername from the localhost to the mysql and set the password to “Naresh” and also fill in the database details and then save the index.php file with the help of the keyboard shortcut Ctrl +x and then press y for saving and finally press “enter.”
Step 8 : Now move to the MySQL container that is running on another node.
Step 9 : Once you move inside the mysql container, we enter the command below for using the database in MySQL.
Step 9.1 : Now get access to making use of the mysql container.
Step 9.2 : Now, make the database in the mysql used for getting the data from the webap container.
Step 9.3 : Make use of the made database.
Step 9.4 : Make the table in the database used for getting the data from the webap.
Step 9.5 : Now exit MySQL and the container before making use of the command exit.
Step 10 : Now move to the browser and write the address as localhost:8001/index.php
Step 11 : Now, as you enter and click on the submit, you need to move to the node where we have the MySQL service for running and then move inside the container.
It will now show you the output of the courses, for which you have filled the details.
That completes the tutorial.
You can contact Naresh I Technologies for your DevOps online training. We provide DevOps training in Hyderabad and USA, and in fact, you can contact us from any part of the world through our phone or online form on our site. Just fill it and submit it, and one of our customer care executives will be contacting you. And what else you get:
- You have the freedom to choose from DevOps online training and classroom training.
- Chance to study from one of the best faculties and one of the best DevOps training institutes in India
- Nominal fee affordable for all
- Complete training
- You get training for tackling all the nitty-gritty of DevOps.
- Both theoretical and practical training.
- And a lot more is waiting for you.
You can contact us anytime for your DevOps training and from any part of the world. Naresh I Technologies caters to one of the best DevOps training in India.