【Docker】Docker Advanced Network (NetWork)

【Docker】Docker Advanced Network (NetWork)


Reference document: Advanced network configuration Docker – from entry to practice (docker-practice.github.io)

1 Overview

docker0When 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/16address 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 docker0a bridge with a name vethstarting 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.

Docker network

Notice:

  • By default, docker connects all containers to the network bridge when creating containers docker0, and by default, all docker0containers 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 defaultdocker0

2. Internet

2.1 Bridge type

There are four types of bridges in docker:

  1. **Bridge **: bridge network mode (default)
  2. Host : Open network mode, sharing the network with the host
  3. Container : joint mount network mode, sharing the network with other containers
  4. 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

image-20230714165330498

View the command line parameters for creating a bridge:

docker network create --help

image-20230714165413184

Parameter meaning:

  • -d: Specify the bridge type, the default isbridge

Create a testbridge called :

docker network create -d bridge test

image-20230714165724990


2.3 View all networks

docker network ls

image-20230714165928694


2.4 View details of a specific network

View heimadetails of a network named :

docker network inspect heima 
image-20230714170129217

2.5 Delete specific network

Delete testthe 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:

image-20230714170326015


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)

Guess you like

Origin blog.csdn.net/Decade_Faiz/article/details/131728528