跳到主要内容

Docker网络管理命令

Docker 提供了一系列命令来管理网络,包括创建、查看、连接和删除网络等操作。

命令功能描述
docker network ls查看网络列表
docker network create创建网络
docker network inspect查看网络详细信息
docker network connect连接容器到网络
docker network disconnect断开容器与网络的连接
docker network rm删除网络
docker network prune清理未使用的网络
docker inspect查看容器的网络信息
docker run --network指定网络运行容器
docker network create -d overlay创建 Overlay 网络(Swarm 模式)
docker network create -d macvlan创建 Macvlan 网络

1. 查看网络列表

查看 Docker 主机上的所有网络。

ubuntu@www.zxzsk.com $ docker network ls
NETWORK ID NAME DRIVER SCOPE
2d30e2dfa556 1panel-network bridge local
abc123def456 bridge bridge local
def456abc123 host host local
ghi789jkl012 none null local

2. 创建网络

创建一个新的 Docker 网络。可以指定网络驱动(如 bridgeoverlaymacvlan 等)和自定义配置。

docker network create [选项] NETWORK_NAME

常用选项:

  • -d, --driver: 指定网络驱动(默认为 bridge)。
  • --subnet: 指定子网(如 192.168.1.0/24)。
  • --gateway: 指定网关(如 192.168.1.1)。
  • --ip-range: 指定 IP 地址范围。
  • --label: 为网络添加元数据标签。

示例:

docker network create -d bridge --subnet 192.168.1.0/24 --gateway 192.168.1.1 my_custom_network

3. 查看网络详细信息

查看指定网络的详细信息,包括配置、连接的容器等。

# 格式:docker network inspect 网络
ubuntu@www.zxzsk.com $ docker network inspect bridge
[
{
"Name": "bridge",
"Id": "390aec8dad133f7cb00ddc522cf0f35428b20ba30aa04e6d0c7205e0bcb53299",
"Created": "2025-02-26T07:24:34.363372059+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.17.0.0/16",
"Gateway": "172.17.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"064cc12c0ae0373cb06321d6dc407278fb29379325f0bdf10961764b9ab8dcae": {
"Name": "linux-command",
"EndpointID": "34c358b92c741c5d202d0d507f619bce72a6d24b5601bf5f6b1ad09ade49fe08",
"MacAddress": "02:42:ac:11:00:04",
"IPv4Address": "172.17.0.4/16",
"IPv6Address": ""
},
"65b43c26f2a655568478081975b5d1cf6c4aeaf156c819dfb954965d6f19b83f": {
"Name": "www-zxzsk-com-27",
"EndpointID": "efd1b65019e07ce587a5444f3400656d297754c71a996d147e021dc2b5ce9b74",
"MacAddress": "02:42:ac:11:00:05",
"IPv4Address": "172.17.0.5/16",
"IPv6Address": ""
},
"79da8c3608b59633d1cd3491ee6bf019cbfd9f6434c1a178c22509d540917969": {
"Name": "www-zxzsk-com",
"EndpointID": "6d1099cb9f480448c2f83bc5a170facf81511eef7e2a6a27b334556c552f79ef",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/16",
"IPv6Address": ""
},
"d8593d146fe045c10ce3fb1a91e8aa25b4d703b8c2527292b1f9c2c6a77de5e2": {
"Name": "reference",
"EndpointID": "989e52e8ab0a5ff8511bfc7ef3dfe221e6c513b9a4aaa7dc6adec7130842cfb6",
"MacAddress": "02:42:ac:11:00:03",
"IPv4Address": "172.17.0.3/16",
"IPv6Address": ""
}
},
"Options": {
"com.docker.network.bridge.default_bridge": "true",
"com.docker.network.bridge.enable_icc": "true",
"com.docker.network.bridge.enable_ip_masquerade": "true",
"com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
"com.docker.network.bridge.name": "docker0",
"com.docker.network.driver.mtu": "1500"
},
"Labels": {}
}
]

4. 连接容器到网络

将正在运行的容器连接到一个网络。

# 格式:docker network connect 网络名 容器名

# 步骤 1:创建自定义网络
docker network create my_custom_network

# 步骤 2:运行容器
docker run -d --name my_container nginx

# 步骤 3:查看当前网络连接
docker inspect my_container --format '{{ .NetworkSettings.Networks }}'

# 步骤 4:连接容器到自定义网络
docker network connect my_custom_network my_container

# 步骤 5:验证网络连接
docker inspect my_container --format '{{ .NetworkSettings.Networks }}'

# 步骤 6:测试容器间的通信(可选)
# 运行另一个容器并连接到 my_custom_network
docker run -d --name another_container --network my_custom_network busybox sleep 3600

# 进入 my_container 并尝试 ping another_container
docker exec -it my_container ping another_container

操作

# 添加网络前
~ ❯ docker network ls
NETWORK ID NAME DRIVER SCOPE
2d30e2dfa556 1panel-network bridge local
3b661159e200 bridge bridge local
97a4ec402d92 host host local
f7757033954f none null local

# 添加网络
~ ❯ docker network create my_custom_network
78835e8936c6acd324e3f8daaa43eb5879d12b01423ea0be46748ab89b6c9778

# 添加网络后
~ ❯ docker network ls 5s
NETWORK ID NAME DRIVER SCOPE
2d30e2dfa556 1panel-network bridge local
3b661159e200 bridge bridge local
97a4ec402d92 host host local
78835e8936c6 my_custom_network bridge local
f7757033954f none null local

# 运行容器(my_container)
~ ❯ docker run -d --name my_container nginx
36310ec37d3a7cc054c3a58bc1adaafb9c75cf5954c4b5cfd5020bc82c589e43

# 查看网络(my_container)
~ ❯ docker inspect my_container --format '{{ .NetworkSettings.Networks }}'
map[bridge:0xc000000000]

# 容器连接到网络
~ ❯ docker network connect my_custom_network my_container

# 查看网络(my_container)
~ ❯ docker inspect my_container --format '{{ .NetworkSettings.Networks }}'
map[bridge:0xc0001d2000 my_custom_network:0xc0001d20c0]

# 运行容器(another_container)
~ ❯ docker run -d --name another_container --network my_custom_network busybox sleep 3600
77e09200f2498e5acda54766060faf9d8ba1a54370f8050f3c00cb8dc8f28aa2

# 查看网络(another_container)
~ ❯ docker inspect another_container --format '{{ .NetworkSettings.Networks }}'
map[my_custom_network:0xc000286000]

# ping测试(my_container未安装ping)
~ ❯ docker exec -it my_container ping another_container
OCI runtime exec failed: exec failed: unable to start container process: exec: "ping": executable file not found in $PATH: unknown

# ping测试(OK)
~ ❯ docker exec -it another_container ping my_container
PING my_container (172.19.0.2): 56 data bytes
64 bytes from 172.19.0.2: seq=0 ttl=64 time=0.102 ms
64 bytes from 172.19.0.2: seq=1 ttl=64 time=0.058 ms
64 bytes from 172.19.0.2: seq=2 ttl=64 time=0.054 ms
64 bytes from 172.19.0.2: seq=3 ttl=64 time=0.059 ms
64 bytes from 172.19.0.2: seq=4 ttl=64 time=0.057 ms
^C
--- my_container ping statistics ---
5 packets transmitted, 5 packets received, 0% packet loss
round-trip min/avg/max = 0.054/0.066/0.102 ms

5. 断开容器与网络的连接

将容器从指定网络中断开。

# 格式:docker network disconnect NETWORK_NAME CONTAINER_NAME
~ ❯ docker network disconnect my_custom_network my_container

~ ❯ docker inspect my_container --format '{{ .NetworkSettings.Networks }}' 5s
map[bridge:0xc00018c600]

6. 删除网络

# 格式:docker network rm NETWORK_NAME_OR_ID

# 网络删除前
~ ❯ docker network ls
NETWORK ID NAME DRIVER SCOPE
2d30e2dfa556 1panel-network bridge local
3b661159e200 bridge bridge local
97a4ec402d92 host host local
78835e8936c6 my_custom_network bridge local
f7757033954f none null local

# 不能删除在用网络
~ ❯ docker network rm my_custom_network 4s
Error response from daemon: error while removing network: network my_custom_network id 78835e8936c6acd324e3f8daaa43eb5879d12b01423ea0be46748ab89b6c9778 has active endpoints

# 断开网络所有连接
~ ❯ docker network disconnect my_custom_network another_container

# 删除网络
~ ❯ docker network rm my_custom_network
my_custom_network

# 网络删除后
~ ❯ docker network ls
NETWORK ID NAME DRIVER SCOPE
2d30e2dfa556 1panel-network bridge local
3b661159e200 bridge bridge local
97a4ec402d92 host host local
f7757033954f none null local

7. 清理未使用的网络

# 格式:docker network prune

# 清理前(4个)
~ ❯ docker network ls
NETWORK ID NAME DRIVER SCOPE
2d30e2dfa556 1panel-network bridge local
3b661159e200 bridge bridge local
97a4ec402d92 host host local
f7757033954f none null local

# 清理(未被任何容器使用的网络)
~ ❯ docker network prune
WARNING! This will remove all custom networks not used by at least one container.
Are you sure you want to continue? [y/N] y
Deleted Networks:
1panel-network

# 清理后(3个)
~ ❯ docker network ls 12s
NETWORK ID NAME DRIVER SCOPE
3b661159e200 bridge bridge local
97a4ec402d92 host host local
f7757033954f none null local

-f 选项,强制删除,跳过确认。

8. 查看容器的网络信息

查看容器的网络配置,包括 IP 地址、网关等。

# 格式:docker inspect CONTAINER_NAME_OR_ID --format '{{ .NetworkSettings.Networks }}'

~ ❯ docker inspect my_container --format '{{ .NetworkSettings.Networks }}'
map[bridge:0xc0002c6000]

9. 使用网络运行容器

在运行容器时指定网络。

docker run -d --name CONTAINER_NAME --network NETWORK_NAME IMAGE

示例:

docker run -d --name my_container --network my_custom_network nginx

10. 查看网络驱动

查看 Docker 支持的网络驱动。

$ docker info --format '{{ .Plugins.Network }}'
[bridge host ipvlan macvlan null overlay]

11. 创建 Overlay 网络

在 Docker Swarm 模式下创建跨主机的 Overlay 网络。

docker network create -d overlay my_overlay_network

12. 创建 Macvlan 网络

创建一个 Macvlan 网络,使容器直接连接到物理网络。

docker network create -d macvlan \
--subnet=192.168.1.0/24 \
--gateway=192.168.1.1 \
-o parent=eth0 \
my_macvlan_network