Networking is very important and essential for Docker containers. This is because it is highly used in servers, ISPs, and in data centers. Let us learn some of the commands which can be used for the purpose of networking in the Docker.
You might need to find the interface of the Docker. By default, the Docker has an interface named “docker0” which can be used for connection to the outside world. By default, the IP address “172.17.42.1/16” will be assigned to this interface, and this will act as the subnet of all the IP address which are running.
Consider the command given below:
After executing the above command in my system, I get the following as the output:
The command is used to tell us more about the bridge interface for “docker0” and know the containers which are connected to it. The command should be run inside the terminal, or inside the shell where the Docker has been installed.
It is also good for one to know the IP address of their Docker container. We should begin by starting a new container, which can be done by use of a command similar to the one given below:
In case you already have a running container, you skip running the above command. So as to get the IP address of the container which is currently running, we can use the command “ip a” as shown below:
This command will give you all of the details necessary to be known about the bridge interface:
It is good for us to expose the port which has been configured in the Dockerfile of our container to the high port. In this case, we use the “-P” flag. The random port of the Docker container will then be opened to the port which is defined by the Dockerfile. An example is given in the following example:
The port of the container will be mapped to port number 80, as the Dockerfile of the httpd defines this. To know whether this has happened, you just have to view the container which is running.
The above command will show us the exposed port. The curl command can be used for the purpose of checking this as shown below:
If it is working effectively on your system, you will be informed of this as shown below:
The output shows that the configuration was successful.
We might also need to perform the mapping in a specific manner in which we will map to a specific port. In this case, the port should be defined by use of the “-p” flag. This is shown in the command given below:
With the above command, the port number 8080 will be mapped to the port number 80. To check whether we have succeeded, we can run the curl command as shown below:
If you get a message that it works, then you are set.
For our containers to be assigned a custom IP address, a new bridge interface needs to be created. We will create one and give it the name “bridge0.” We will then assign it an IP address. Consider the following sequence of commands:
Just write and then execute the commands given above. We will have created the Docker Bridge, and also have assigned an IP address to it. The next step involves informing the Docker daemon about the same. This can be done by use of the following commands:
Once you have executed the above commands, you should get output such as the one given below:
It is also possible for us to link one image to another. In this case, we use the “ -flag.” First, we should use the “-name” flag, so as to make it easy for us to denote the image.
Once we are done with the above command, we can then link the image “images/myapp” and the image “database.” The resulting image will be named “finalimage.”
Just type the command correctly and then execute it. It will then run and then complete. You will have joined the two images into a unit image named “finalimage.”
|Docker Kubernetes Training|
|OpenShift Administration Training|
Get Updates on Tech posts, Interview & Certification questions and training schedules