Networking is about communication among processes, and Docker’s networking is no different. A docker network is basically a connection between one or more containers. It is very easy to isolate and manage the containers, as different containers can be easily connected to each other.
Docker supports different types of networks, each fit for certain use cases. Let’s take a look at each one:
This is the default network created by docker on the host machine on which it is installed. Docker containers that are connected by the means of a bridge network can communicate with each other. So, containers connected to this network communicate via IP addressing.
Creating a bridge network run the below command:
To list all the networks run the below command:
To remove a specific network run the below command:
For getting a detailed information about the network run the below command:
Create a container connected with the my-bridge network:
Host networking is used for standalone containers. Docker containers that are connected to the host network basically share the namespaces with their hosts. In other words the container shares the IP address of the host and doesn’t have one of its own.
The following command will start an Nginx image and connect to the host network:
And when you will inspect it will show no ip address:
The Overlay network driver is used to create an internal private network. It is used when the containers can communicate with each other securely even when the containers are running on different hosts and are on different containers. This is useful when we need a set of docker hosts to communicate with each other in a docker swarm.
For instance let’s set up a network connecting three containers on three different docker hosts. Firstly create two EC2 instances (free tier should be fine), and install Docker on each EC2 instance.
- Create a swarm on host1 and host1 becomes your manager node.
- After that, Copy the token and paste it in host2 this will become your worker node.
- On host1, verify that the swarm is ready:
- On host1, verify the default networks:
- Overlay Network Creation on Node 1
- Run Our Container, Join the Overlay Net on host1
- Run Our Container, Join the Overlay Net on host2
- From Node 1, let’s ping the Node 2 container.
- From Node 2, let’s ping the Node 1 container.
Therefore, your connection has been successfully established.
A docker network which has none network configured for itself cannot communicate with any service or system as networking for the container is virtually disabled. It is usually used to isolate containers.
Creating a none network:
In conclusion You came to know about Networking in Docker. You now know that Docker networking is primarily used to establish communication between Docker containers and the outside world via the host machine where the Docker daemon is running. Docker supports different types of networks, each fit for certain use cases.