Let’s first take a look at all the subcommands of network:
(Recommended tutorial: docker tutorial)
docker network create docker network connect docker network ls docker network rm docker network disconnect docker network inspect
一, 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:
# 不指定网络驱动时默认创建的bridge网络 docker network create simple-network # 查看网络内部信息 docker network inspect simple-network # 应用到容器时,可进入容器内部使用ifconfig查看容器的网络详情
But creating an overlay network requires some prerequisites (for specific operations, please refer to the related content of Docker container network):
- key-value store(Engine支持Consul、Etcd和ZooKeeper等分布式存储的key-value store) - 集群中所有主机已经连接到key-value store - swarm集群中每个主机都配置了下面的daemon参数 - –cluster-store - –cluster-store-opt - –cluster-advertise
Then Create an overlay network:
# 创建网络时,使用参数`-d`指定驱动类型为overlay docker network create -d overlay my-multihost-network
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.
# 创建三个容器 conTainer1,container2,container3 docker run -itd --name=container1 busybox docker run -itd --name=container2 busybox # 创建网络mynet docker network create -d bridge --subnet 172.25.0.0/16 mynet # 将容器containerr2连接到新建网络mynet docker network connect mynet container2 # 使用mynet网络来容器container3 docker run --net=mynet --ip=172.25.3.3 -itd --name=container3 busybox # 查看这三个容器的网络情况 docker network inspect container1 # docker0 docker network inspect container2 # docker0, mynet docker network inspect container3 # mynet
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.
# 进入container2内部 docker attach container2 ping -w 4 container3 # 可访问 ping -w 4 container1 # 不可访问 ping -w 4 172.17.0.2 # 可访问container1的IP # Ctrl+P+Q退出容器,让container2以守护进程运行
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:
- 使用容器名作为hostname - link容器时指定alias: --link=<Container-Name>:<Alias> - 配合--icc=false隔离性,实现容器间的安全连接 - 环境变量注入
Auto Using docker net in the defined network provides the following functions:
- 使用DNS实现自动化的名称解析 - 一个网络提供容器的安全隔离环境 - 动态地attach与detach到多个网络 - 支持与--link选项一起使用,为链接的容器提供别名(可以是尚不存在链接容器,与默认容器中–link使用的最大差别)
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.
# 运行容器使用自定义网络,同时使用--link链接尚不存在的container5容器 docker run --net=mynet -itd --name=container4 --link container5:c5 busybox # 创建容器container5 docker run --net=mynet -itd --name=container5 --link container4:c4 busybox # 虽然是相同容器,但是在不同的网络环境连接中可以不同的alias链接 docker network connect --link container5:foo local_alias container4 docker network connect --link container4:bar local_alias container5
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.
docker run --net=mynet -itd --name=container6 --net-alias app busybox docker network connect --alias scoped-app local_alias container6 docker run --net=isolated_nw -itd --name=container7 --net-alias app busybox docker network connect --alias scoped-app local_alias container7 # 在container4中 docker attach container4 ping app # 访问container6的IP # 从container4中以守护进程运行退出:Ctrl+P+Q docker stop container6 docker attach container4 ping app # 访问的container7的IP
6. Disconnect and remove the network
# 容器从mynet网络中断开(它将无法再网络中的容器container3通讯) docker network disconnect mynet container2 # 测试与容器container3失败 docker attach container2 ping contianer3 # 访问失败
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:
# 断开最后一个连接到mynet网络的容器 docker network disconnet mynet container3 # 移除网络 docker network rm mynet
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!

The ways Docker can simplify development and operation and maintenance processes include: 1) providing a consistent environment to ensure that applications run consistently in different environments; 2) optimizing application deployment through Dockerfile and image building; 3) using DockerCompose to manage multiple services. Docker implements these functions through containerization technology, but during use, you need to pay attention to common problems such as image construction, container startup and network configuration, and improve performance through image optimization and resource management.

The relationship between Docker and Kubernetes is: Docker is used to package applications, and Kubernetes is used to orchestrate and manage containers. 1.Docker simplifies application packaging and distribution through container technology. 2. Kubernetes manages containers to ensure high availability and scalability. They are used in combination to improve application deployment and management efficiency.

Docker solves the problem of consistency in software running in different environments through container technology. Its development history has promoted the evolution of the cloud computing ecosystem from 2013 to the present. Docker uses Linux kernel technology to achieve process isolation and resource limitation, improving the portability of applications. In development and deployment, Docker improves resource utilization and deployment speed, supports DevOps and microservice architectures, but also faces challenges in image management, security and container orchestration.

Docker and virtual machines have their own advantages and disadvantages, and the choice should be based on specific needs. 1.Docker is lightweight and fast, suitable for microservices and CI/CD, fast startup and low resource utilization. 2. Virtual machines provide high isolation and multi-operating system support, but they consume a lot of resources and slow startup.

The core concept of Docker architecture is containers and mirrors: 1. Mirrors are the blueprint of containers, including applications and their dependencies. 2. Containers are running instances of images and are created based on images. 3. The mirror consists of multiple read-only layers, and the writable layer is added when the container is running. 4. Implement resource isolation and management through Linux namespace and control groups.

Docker simplifies the construction, deployment and operation of applications through containerization technology. 1) Docker is an open source platform that uses container technology to package applications and their dependencies to ensure cross-environment consistency. 2) Mirrors and containers are the core of Docker. The mirror is the executable package of the application and the container is the running instance of the image. 3) Basic usage of Docker is like running an Nginx server, and advanced usage is like using DockerCompose to manage multi-container applications. 4) Common errors include image download failure and container startup failure, and debugging skills include viewing logs and checking ports. 5) Performance optimization and best practices include mirror optimization, resource management and security improvement.

The steps to deploy containerized applications using Kubernetes and Docker include: 1. Build a Docker image, define the application image using Dockerfile and push it to DockerHub. 2. Create Deployment and Service in Kubernetes to manage and expose applications. 3. Use HorizontalPodAutoscaler to achieve dynamic scaling. 4. Debug common problems through kubectl command. 5. Optimize performance, define resource limitations and requests, and manage configurations using Helm.

Docker is an open source platform for developing, packaging and running applications, and through containerization technology, solving the consistency of applications in different environments. 1. Build the image: Define the application environment and dependencies through the Dockerfile and build it using the dockerbuild command. 2. Run the container: Use the dockerrun command to start the container from the mirror. 3. Manage containers: manage container life cycle through dockerps, dockerstop, dockerrm and other commands.


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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use
