Detailed explanation of docker network command
Let’s first take a look at all the subcommands of network:
(Recommended tutorial: docker tutorial)
1 2 3 4 5 6 |
|
一, Create a network
When installing Docker Engine, a default bridge network docker0 will be automatically created. In addition, you can also create your own bridge network or overlay network.
The bridge network is attached to a single host running Docker Engine, while the overlay network can cover multiple host environments running their respective Docker Engines.
Creating a bridge network is relatively simple as follows:
1 2 3 4 5 |
|
But creating an overlay network requires some prerequisites (for specific operations, please refer to the related content of Docker container network):
1 2 3 4 5 6 |
|
Then Create an overlay network:
1 2 |
|
In terms of using the --subnet option to create a subnet, the bridge network can only specify one subnet, while the overlay network supports multiple subnets.
Networks created under the bridge and overlay network drivers can specify different parameters.
2. Connect the container
Create three containers. The first two use the default network to start the container, and the third one uses the custom bridge network to start the container. Then add the second container to the custom network. The network conditions of these three containers are as follows:
The first container: only the default docker0
The second container: belongs to two networks-docker0, custom network
The third container: only belongs to the custom network
Note: Starting the specified network through the container will overwrite the default bridge network docker0.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
3. The difference between the default network and the custom bridge network
Default network docker0: All hosts in the network can only access each other using IP. Containers created with the --link option can directly access the linked container name (container-name) as hostname.
Customized network (bridge): In addition to IP access, all hosts in the network can also directly access each other using the container name (container-name) as hostname.
1 2 3 4 5 6 |
|
4. The difference between the default network and the custom bridge network in container connection
Using link (legency link) in the default network has the following functions:
1 2 3 4 |
|
Auto Using docker net in the defined network provides the following functions:
1 2 3 4 |
|
The link in the default network is static and does not allow the link container to be restarted, while the link in the custom network is dynamic and supports the link container to restart. (And IP changes)
Therefore, the container linked when using --link must be created in advance in the default network, but does not need to be pre-built in the custom network.
When using docker network connectct to connect the container to a new network, when using the parameter --link to link the same container, you can specify different aliases, which are for different networks.
1 2 3 4 5 6 7 |
|
5. Specify the network-scoped alias of the container (Network-scoped alias)
Network-scoped alias is the alias of the specified container that can be accessed by other containers within the same network range.
Different from link aliases, link aliases are provided by the user of the link container and can only be used by itself; while aliases within the specified network range are provided by the container for use by other containers in the network.
Network-scoped alias: Multiple containers in the same network can specify the same alias. Of course, only the first container with the specified alias will take effect.
Only when the first container is closed , the alias of the second container that specifies the same alias will take effect.
1 2 3 4 5 6 7 8 9 10 11 |
|
6. Disconnect and remove the network
1 2 3 4 5 |
|
In a multi-host network environment, container already will appear when connecting a container to the network with a removed container name. Connected to network error,
At this time, you need to forcefully remove the new container docker rm -f, re-run and connect to the network.
Removing a network requires that all containers in the network be closed or disconnected from this network before the removal command can be used:
1 2 3 4 |
|
The above is the detailed content of Detailed explanation of docker network command. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The steps to update a Docker image are as follows: Pull the latest image tag New image Delete the old image for a specific tag (optional) Restart the container (if needed)

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop <container_name> Command Use docker kill <container_name> command in the host terminal (force exit)

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] <Container Path> <Host Path>. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

Docker process viewing method: 1. Docker CLI command: docker ps; 2. Systemd CLI command: systemctl status docker; 3. Docker Compose CLI command: docker-compose ps; 4. Process Explorer (Windows); 5. /proc directory (Linux).

How to restart the Docker container: get the container ID (docker ps); stop the container (docker stop <container_id>); start the container (docker start <container_id>); verify that the restart is successful (docker ps). Other methods: Docker Compose (docker-compose restart) or Docker API (see Docker documentation).

To save the image in Docker, you can use the docker commit command to create a new image, containing the current state of the specified container, syntax: docker commit [Options] Container ID Image name. To save the image to the repository, you can use the docker push command, syntax: docker push image name [: tag]. To import saved images, you can use the docker pull command, syntax: docker pull image name [: tag].

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database
