Summary:
1) Bridge Network: Default & Custom
2) Host Network
3) None/Null Network
DOCKER NETWORKING
In Docker, if 2 containers communicate to each other, it means they are in a network.
Step 2: Create a new default Bridge network br1 (-d is drive)
Step 3: Verify network is created successfully. It should be listed in docker networks.
Step 4: Inspect br1 network to find more detailed information.
Step 4.2: You can create a bridge network with your own subnet
Step 7: Inspect the container and it should have IP address within the br1 CIDR range.
Step 8: Remove the container and network.
In Docker, if 2 containers communicate to each other, it means they are in a network.
Do note that unlike centos, ubuntu does not have ping command in-built. so you need to install it in the containers themselves:
apt-get updateapt-get install iputils-ping # 333kb
To look at available docker network commands:
Command:-> docker network --help
To find all the IP addresses on a system
Command:-> ip a
Find all the networks in docker
Command:-> docker network ls
Types of Networks in Docker
1) Bridge
To make your own custom bridge network, follow the steps below:
Step 1: List all the containers
|
connect |
Connect a container to a network |
|
create |
Create a network |
|
disconnect |
Disconnect a container from a network |
|
inspect |
Display detailed information on one or
more networks |
|
ls |
List networks |
|
prune |
Remove all unused networks |
|
rm |
Remove one or more networks |
To find all the IP addresses on a system
Command:-> ip a
Find all the networks in docker
Command:-> docker network ls
Types of Networks in Docker
1) Bridge
1a) Default Bridge Network
It is the default network (docker0) in docker, which means if a container is created by default it is created on top of bridge network docker0.
If 2 or more containers get created on the bridge network then they are automatically in the same network, which means they can communicate with each other.
Step 1:- Create container c1 with ubuntu image
Step 2:- Check c1 container is running on the docker0 network
you will find one veth.... on docker0
Also, check the CIDR for docker0
Step 3:- Check the IP address of the container, it should be in the CIDR range of the docker0 network. In my case, IP address of c2 is "172.17.0.2"
Step 4:- Create container c2 with centos image
Step 5:- Check c2 container is running on the docker0 network
you will find one more veth.... on docker0
Also, check the CIDR for docker0
Step 6:- Check the IP address of the container, it should be in the CIDR range of the docker0 network. In my case IP address of c2 is "172.17.0.3"
Step 7:- Check container c2 ping to c1. It should get a reply from c1 because both are in the same network (default bridge network)
It is the default network (docker0) in docker, which means if a container is created by default it is created on top of bridge network docker0.
If 2 or more containers get created on the bridge network then they are automatically in the same network, which means they can communicate with each other.
Step 1:- Create container c1 with ubuntu image
docker container run -it --name c1 -d ubuntu
Step 2:- Check c1 container is running on the docker0 network
ip a
you will find one veth.... on docker0
Also, check the CIDR for docker0
Step 3:- Check the IP address of the container, it should be in the CIDR range of the docker0 network. In my case, IP address of c2 is "172.17.0.2"
docker container inspect c1
Step 4:- Create container c2 with centos image
docker container run -it --name c2 -d centos
Step 5:- Check c2 container is running on the docker0 network
ip a
you will find one more veth.... on docker0
Also, check the CIDR for docker0
Step 6:- Check the IP address of the container, it should be in the CIDR range of the docker0 network. In my case IP address of c2 is "172.17.0.3"
docker container inspect c2
Step 7:- Check container c2 ping to c1. It should get a reply from c1 because both are in the same network (default bridge network)
docker exec -it c2 bash
ping 172.17.0.2
1b) Custom Bridge Network or User Define Bridge Network
When a network which is created by user or sysadmin so that specified containers can run on it then it is a custom bridge network.To make your own custom bridge network, follow the steps below:
Step 1: List all the containers
docker network ls
Step 2: Create a new default Bridge network br1 (-d is drive)
docker network create -d bridge br1
Step 3: Verify network is created successfully. It should be listed in docker networks.
docker network ls
Step 4: Inspect br1 network to find more detailed information.
docker network inspect br1
- "Driver": "bridge",
- "EnableIPv6": false,
- "IPAM": {
- "Driver": "default",
- "Options": {},
- "Config": [
- {
- "Subnet": "172.18.0.0/16",
- "Gateway": "172.18.0.1"
Step 4.2: You can create a bridge network with your own subnet
docker network create -d bridge --subnet=192.168.0.0/16 --gateway=192.168.0.1 br2
docker inspect br2
- "Driver": "bridge",
- "EnableIPv6": false,
- "IPAM": {
- "Driver": "default",
- "Options": {},
- "Config": [
- {
- "Subnet": "192.168.0.0/16",
- "Gateway": "192.168.0.1"
- }
It means if the containers get created on this network layer then they have IP addresses in 192.168.0.1/16 range.
Step 6:- Create a container on top of br1 network on ubuntu base image.
Step 6:- Create a container on top of br1 network on ubuntu base image.
docker container run -it --name c1 --network br1 -d ubuntu
Step 7: Inspect the container and it should have IP address within the br1 CIDR range.
docker container inspect c1
Step 8: Remove the container and network.
docker rm -f c1
docker network rm br1
docker network ls