Steps to Manage Network in Docker
Steps to Manage Network in Docker - By default, containers use a bridge-type network driver called a bridge. There are other network drivers, namely host, overlay, and macvlan.
- bridge: connect containers using the same bridge name
- host: remove network isolation between the container and Docker host, directly use host's network
- overlay: connects multiple Docker daemons together and lets the swarm service talk to both
- macvlan: allows it to assign MAC addresses to containers, making them exist as physical devices on the network
- none: disables all networks. Commonly used in conjunction with custom network drivers
- Network plugins: install and use third-party network plugins available on Docker Hub or from third-party suppliers
- Displays available networks in Docker.
docker network ls
- Default bridge network
Created two containers with container names node1 and node2, image Nginx:stable-alpine, and without defining network options.
docker run -d --name node1 nginx:stable-alpine
docker run -d --name node2 nginx:stable-alpine
- Displays detailed container information.
docker inspect node1
Network information from the container is found in the Networks section. The container uses the network driver bridge. The first 12 characters (1652fc7934a6) NetworkID is the same as the NETWORK ID shown earlier in the result of the docker network ls command with NAME and DRIVER bridge.
The IP address is 172.17.0.2 and Gateway 172.17.0.1. The gateway uses the network interface and IP of Docker0 on the host.
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
inet6 fe80::42:b0ff:fe4b:4ff9 prefixlen 64 scopeid 0x20<link>
ether 02:42:b0:4b:4f:f9 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5 bytes 526 (526.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
- Displays network information with the bridge name.
docker network inspect bridge
Containers that use a bridge network can be seen for information in the Containers section. There are two containers, namely node1 with IP address 172.17.0.2 and node2 with IP address 172.17.0.3.
- Test ping from container node1 to node2.
docker exec node1 ping 172.17.0.3
Creating a Bridge Network
Containers using the default bridge network cannot automatically communicate using the hostname of the container, they can only use the IP address. For example, it cannot ping node2, it can only ping 172.17.0.3.
To be able to communicate using a hostname, define the IP-hosts in the /etc/hosts container file manually. Alternatively, the container uses a user-defined bridge network.
- Create a bridge network with the name net-web.
docker network create net-web
- Display networks.
docker network ls
NETWORK ID NAME DRIVER SCOPE
1652fc7934a6 bridge bridge local
547776771fe2 host local host
2aa8cf07ab2e net-web bridge local
a4d149a845ae none null local
Creating a Container with network options
At the time of creating a container can simultaneously connect it to the network.
- Create a container and connect it to the network with the name net-web.
docker run -d --name node1 --network net-web nginx:stable-alpine
docker run -d --name node2 --network net-web nginx:stable-alpine
Connecting existing containers
Pre-existing containers can be connected to the network.
- Connects node1 and node2 containers to the net-web network.
docker network connect net-web node1
docker network connect net-web node2
- Removing the node1 container from the net-web network.
docker network disconnect net-web node1
Docker Network Command
- Displays all networks.
docker network ls
- Create networks.
docker network create name-network
- Connect the container to the network.
docker network connect container-name-network
- Removing the container from the network.
docker network disconnect container-name network-name
- Displays detailed network information.
docker network inspect network-name
- Delete networks.
docker network rm name-network
- Delete all unused networks.
docker network prune