Understanding Docker Networking

Understanding Docker Networking

What is docker networking?

Docker networking is used to establish communication between the docker container and outside the world via the host machine. In docker by default bridge network is created. Whenever we create a new container it's automatically attached to the bridge network.so container-to-container or host to container to host communication is possible. So networking is a very important concept in docker. Let's discuss what are the network drivers.

By default when we create containers in the docker bridge network is created.

  • Bridge Networking

    By default bridge network is created when the container is created in docker.

Why did we call it a bridge network?

This is because the container has a different subnet and the host has a different subnet so with the help of the bridge container and host can communicate with each other. We can also create a custom bridge network for container isolation or we can say security purposes.

Let's understand the bridge network with the help of a diagram.

In the above diagram, we can see the host machine. The host machine can be EC2 or any other machine. Here host machine has eth0 which means subnet.

so the host machine eht0 and container eth0 are different so they can't communicate with each other. So here is the bridge concept coming into the picture. so with the help of verth i.e. virtual ethernet container to host communication is possible. see the below image.

In the above image, we can see the bridge, host and null driver.

Let's create one container and inspect it.

root@ip-172-31-92-226:~# docker container run -itd nginx

Let's check container is created or not.

root@ip-172-31-92-226:~# docker container ls

Now we will inspect the container bridge network.

root@ip-172-31-92-226:~# docker network inspect bridge

Now we can see bridge network has one container.

Now we will see whether the container has internet access or not. We will check with the help of the ping command.if you don't have ping package then the ping command will not work. To download the ping package we need to run the apt-get install -y iputils-ping command within the container. Refer below image for understanding. I run the container and then installed the ping command.

  1. Run the container :

  2. Download the ping package.

  3. Check whether the container has internet access or not.

How to check whether our container is publically accessible or not?

Assign a port to a container and take a public IP of a machine assigned with a port number and paste it into a web browser.

e.g.http://<public ip>:<container port number>

How to create a network?

root@ip-172-31-92-226:~# docker network create privnetwork

  • Host Networking

    In the case of host networking, all the containers are bound to host. It means containers and hosts have the same IP.

    How to create a container in the host?

root@ip-172-31-92-226:~# docker run -d --name container3 --network=host nginx:latest

Now let's see the ip of a container3.

root@ip-172-31-92-226:~# docker container ls

root@ip-172-31-92-226:~# docker inspect container3

We can see in the above image there is no IP for container 3 because it is bounded to the host.

  • None Network

    Containers attached to the none network will not have IP addresses and will be stand-alone.

  • Overlay Networks

    Overlay networks connect multiple docker daemons and enable swarm services to communicate with each other. Overlay networks provide communication between a swarm service and a standalone container or between two standalone containers on different docker daemons.

  • Macvlan Networks

    It allows you to assign a MAC address to a container, making it appear as a physical device on your network. The docker daemon routes traffic to containers by their MAC addresses.

Networking Commands

CommandsDescription
docker network lsTo see the network in our machine
docker network create <netowork name>To create a custom network
docker network inspect <network id or name>To see detailed network information.
docker container inspect <container id>To see container network information.
docker network connect <container id>Connect a container to a network
docker network rmRemove one or more network
docker network pruneRemove all unused network

Thank you for reading this blog.Happy Learning!!!!!

You can follow me on LinkedIn for my daily updates:- linkedin.com/in/parimal-pradhan-b62021168

Great initiative by the #trainwithshubham community. Thank you Shubham Londhe

Did you find this article valuable?

Support Parimal Pradhan by becoming a sponsor. Any amount is appreciated!