A Docker network is basically a connection between one or more containers. One of the mole powerful things about the Docker containers is that they can be easily connected to one other and even of software, this makes I very easy to isolate and manage the containers.
Types of Networks in Docker
- Bridge Network
- Host Network
- Overlay Network
- Macvlan Network
- None Network
Previous Tutorial – Docker Storage
Bridge Network
Docker containers that are connected by the means of a bridge network can communicate with each other. This also create a layer of isolation between the docker container that are connected to each other through a bridge network.
Lets create Bridge Network in Docker.
To create bridge network
docker network create --driver <driver_name> <network_name>
docker network create --driver bridge network1
To list all networks
docker network ls
To inspect detail of network
docker network inspect <network_name>
docker network inspect network1
By default docker add containers in bridge network.
Let check how two docker communicate with other
docker run -t -d --name defaultCon ubuntu
docker run -t -d --name defaultCon2 ubuntu
Inspect docker and check IPAddress section for checking IPAddress of that container. Eg – 172.17.0.3
docker network inspect defaultCon2
Now lets go inside the first container, and ping for container 2 for which we got IP address.
docker exec -it defaultCon bash
apt update && apt install iputils-ping -y
docker exec -it defaultCon bash
ping 172.17.0.3
If there is no loss, then it means container can communicate with eachother successfully.
Now lets check how containers communicate with each other from different networks.
docker run -it -d --name defaultCon5 --network network1 ubuntu
docker ps
docker network inspect defaultCon5
docker run -it -d --name defaultCon6 --network network1 ubuntu
docker ps
docker network inspect defaultCon6
docker exec -it defaultCon5 bash
apt update && apt install iputils-ping -y
ping 172.17.0.5
docker exec -it defaultCon bash
ping 172.17.0.5
Host Network
Docker containers that we connected to host network basically share the namespace with their hosts, example the containers share the IP address of the host and don’t have one of their own.
Lets create a network with ngnix image
docker run -it -d --network host --name conHost nginx:latest
Inspect the container
docker container inspect conHost
Now just checkout the networks host section IPAddress.
Overlay Network
Docket daemon hosts that are connected by the means of an overlay network can communicate with each other. This means that the containers present in different docker hosts can communicate with each other using the overlay network. This is useful when we need a set of docker hosts to communicate with each other in a docket swarm.
We will need to host computer or virtual machine for this.
On first virtual machine i will initiate swarm
docker swarm init
The above command will give output like bellow
docker swarm join --token SWMTKN-1-5radszfdasdgimc5kaj4kdas13s2wu25xradv94cadqoq-
Copy the token and paste it in another host machine. It will give output, network connected.
Now create a network overlay
docker network create --driver <network_type> <network_name>
docker network create --driver overlay overlay1
Now we need to create docker service. In bellow command we will create 3 replicas of docker.
docker service create --name service1 --network network1--replicas 3 ngnix:latest
Now you can ping from another host machine and check.
None Network
A docker container which has none network configured for itself cannot communicate with any service or system as networking for the container is virtually disabled. Its usually used to isolate certain containers.
Create a container and inspect it.
docker run -it -d --name conNone1 --network none ubuntu
dockedocker container inspect conNone1
Next tutorial