1. docker advanced network configuration
1.1 description
When Docker is started, it will automatically create a docker0 virtual bridge on the host, which is actually a bridge of Linux and can be understood as a software switch. It will forward between the network ports attached to it.
Meanwhile, Docker randomly assigns a private network segment that is not occupied locally (at RFC1918 An address in (defined in) is given to the docker0 interface. For example, the typical 172.17.42.1 mask is 255.255.0.0. After that, the network port in the container started will automatically assign an address of the same network segment (172.17.0.0 / 16).
When creating a docker container, a pair of veth pair interfaces will be created at the same time (when a packet is sent to one interface, the other interface can also receive the same packet). One end of the pair of interfaces is in the container, that is, eth0; The other end is local and mounted to the docker0 bridge. The name starts with veth (for example, vethAQI2QT). In this way, the host can communicate with the container, and the containers can also communicate with each other. Docker creates a virtual shared network between the host and all containers.
1.2 viewing network information
# docker network ls
1.3 create a bridge
# docker network create -d bridge name
1.4 delete a bridge
# docker network rm bridge name
1.5 network communication before container
# 1. Query the current network configuration - docker network ls
NETWORK ID NAME DRIVER SCOPE 8e424e5936b7 bridge bridge local 17d974db02da docker_gwbridge bridge local d6c326e433f7 host host local
# 2. Create a bridge network - docker network create -d bridge info
[root@centos ~]# docker network create -d bridge info 6e4aaebff79b1df43a064e0e8fdab08f52d64ce34db78dd5184ce7aaaf550a2f [root@centos ~]# docker network ls NETWORK ID NAME DRIVER SCOPE 8e424e5936b7 bridge bridge local 17d974db02da docker_gwbridge bridge local d6c326e433f7 host host local 6e4aaebff79b info bridge local
# 3. Start the container and specify the bridge - docker run -d -p 8890:80 --name nginx001 --network info nginx - docker run -d -p 8891:80 --name nginx002 --network info nginx `be careful:Once the bridge is specified--name The specified name is the host name,When multiple containers are specified on the same bridge,The host name can be used in any container to communicate with the container`
[root@centos ~]# docker run -d -p 8890:80 --name nginx001 --network info nginx c315bcc94e9ddaa36eb6c6f16ca51592b1ac8bf1ecfe9d8f01d892f3f10825fe [root@centos ~]# docker run -d -p 8891:80 --name nginx002 --network info nginx f8682db35dd7fb4395f90edb38df7cad71bbfaba71b6a4c6e2a3a525cb73c2a5 [root@centos ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f8682db35dd7 nginx "/docker-entrypoint...." 3 seconds ago Up 2 seconds 0.0.0.0:8891->80/tcp nginx002 c315bcc94e9d nginx "/docker-entrypoint...." 7 minutes ago Up 7 minutes 0.0.0.0:8890->80/tcp nginx001 b63169d43792 mysql:5.7.19 "docker-entrypoint.s..." 7 minutes ago Up 7 minutes 3306/tcp mysql_mysql.1.s75qe5kkpwwttyf0wrjvd2cda [root@centos ~]# docker exec -it f8682db35dd7 /bin/bash root@f8682db35dd7:/# curl http://nginx001 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> .....
2. Advanced data volume configuration
2.1 description
A data volume is a special directory that can be used by one or more containers. It bypasses UFS and can provide many useful features:
- Data volumes can be shared and reused between containers
- Changes to the data volume take effect immediately
- Updates to data volumes do not affect mirroring
- Data volumes persist by default, even if the container is deleted
Note: the use of data volume is similar to mounting directories or files under Linux. The files in the directory designated as the mount point in the image will be copied to the data volume (only when the data volume is empty).
2.2 creating data volumes
[root@centos ~]# docker volume create my-vol my-vol
2.3 viewing data volumes
[root@centos ~]# docker volume inspect my-vol [ { "CreatedAt": "2020-11-25T11:43:56+08:00", "Driver": "local", "Labels": {}, "Mountpoint": "/var/lib/docker/volumes/my-vol/_data", "Name": "my-vol", "Options": {}, "Scope": "local" } ]
2.4 mounting data volumes
[root@centos ~]# docker run -d -P --name web -v my-vol:/usr/share/nginx/html nginx [root@centos ~]# docker inspect web "Mounts": [ { "Type": "volume", "Name": "my-vol", "Source": "/var/lib/docker/volumes/my-vol/_data", "Destination": "/usr/share/nginx/html", "Driver": "local", "Mode": "z", "RW": true, "Propagation": "" } ],
2.5 deleting data volumes
docker volume rm my-vol