【Docker】Docker Advanced Network (NetWork)
Article directory
Reference document: Advanced network configuration Docker – from entry to practice (docker-practice.github.io)
1 Overview
docker0
When Docker starts, it will automatically create a virtual network bridge on the host , which is actually a bridge of Linux, which can be understood as a software switch. It will forward between the network ports mounted to it.
At the same time, Docker randomly assigns an address in a local unoccupied private network segment (defined in RFC1918docker0
) to the interface. For example, typically 172.17.42.1
, the mask is 255.255.0.0
. The network port in the container started after that will also be automatically assigned an 172.17.0.0/16
address of the same network segment ( ).
When a Docker container is created, a pair of interfaces is created at the same time veth pair
(when a data packet is sent to one interface, the other interface can also receive the same data packet). One end of this pair of interfaces is inside the container, ie eth0
; the other end is local and mounted to docker0
a bridge with a name veth
starting with (for example vethAQI2QT
). In this way, the host can communicate with the container, and the containers can communicate with each other. Docker creates a virtual shared network between the host and all containers.
Notice:
- By default, docker connects all containers to the network bridge when creating containers
docker0
, and by default, alldocker0
containers on the bridge can use the ip address in the container to communicate. - Containers can also use the container name as the container ip to communicate, provided that a custom bridge must be used instead of the default
docker0
2. Internet
2.1 Bridge type
There are four types of bridges in docker:
- **Bridge **: bridge network mode (default)
- Host : Open network mode, sharing the network with the host
- Container : joint mount network mode, sharing the network with other containers
- None : Closed network mode, no network is configured for the container
2.2 Create a network custom bridge
First look at the docker command parameters related to the network:
docker network --help
View the command line parameters for creating a bridge:
docker network create --help
Parameter meaning:
- -d: Specify the bridge type, the default is
bridge
Create a test
bridge called :
docker network create -d bridge test
2.3 View all networks
docker network ls
2.4 View details of a specific network
View heima
details of a network named :
docker network inspect heima
2.5 Delete specific network
Delete test
the network named :
docker network rm test
After the deletion is complete, check whether the network still exists, and find that it has indeed been deleted. as the picture shows:
2.6 Multiple containers use a specified network
1) When starting the container, specify which network name the container uses
docker run -d --network 网络名 ...
2) Add the container to a network after startup
docker network connect 网络名 容器id(name)