Docker 网络是 Docker 容器之间进行通信的桥梁。本教程将带你了解 Docker 网络的基本概念、创建和管理网络的方法。
基本概念
- 容器网络: 容器网络是 Docker 容器之间进行通信的机制。
- 桥接网络: 默认的网络模式,容器通过虚拟桥接设备进行通信。
- 主机网络: 容器直接使用宿主机的网络命名空间。
- 自定义网络: 可以根据需要创建自定义网络。
创建网络
docker network create -d bridge my_network
这条命令创建了一个名为 my_network
的桥接网络。
连接容器到网络
docker run -d --name my_container --network my_network my_image
这条命令创建了一个名为 my_container
的容器,并将其连接到 my_network
网络。
网络配置
docker network inspect my_network
这条命令可以查看 my_network
网络的详细信息。
网络连接
docker exec -it my_container ping my_container
这条命令可以在 my_container
容器中执行 ping
命令,测试网络连接。
图片示例
扩展阅读
更多关于 Docker 网络的详细信息,请参阅官方文档。
Docker Network Tutorial
Docker networking is the mechanism by which Docker containers communicate with each other. This tutorial will guide you through the basic concepts of Docker networking, as well as how to create and manage networks.
Basic Concepts
- Container Networking: The mechanism by which Docker containers communicate with each other.
- Bridge Networking: The default networking mode, where containers communicate through a virtual bridge device.
- Host Networking: Containers use the host's network namespace directly.
- Custom Networking: You can create custom networks according to your needs.
Creating a Network
docker network create -d bridge my_network
This command creates a bridge network named my_network
.
Connecting a Container to a Network
docker run -d --name my_container --network my_network my_image
This command creates a container named my_container
and connects it to the my_network
network.
Network Configuration
docker network inspect my_network
This command shows the details of the my_network
network.
Network Connection
docker exec -it my_container ping my_container
This command runs the ping
command inside the my_container
container to test the network connection.
Image Example
Further Reading
For more detailed information about Docker networking, please refer to the official documentation.