Docker CLI - docker network connect

本文详细介绍了如何使用Docker命令将容器连接到网络,包括指定IP地址、创建网络别名等高级功能。同时,提供了丰富的示例,如使用--ip选项指定容器的IP地址,或使用--alias选项为容器创建网络别名。

用法

docker network connect [OPTIONS] NETWORK CONTAINER

选项

名字,速记默认描述
--alias Add network-scoped alias for the container -----为容器添加网络范围的别名
--driver-opt driver options for the network -----网络的驱动程序选项
--ip IPv4 address (e.g., 172.30.100.104) -----IPv4地址(例如,172.30.100.104)
--ip6 IPv6 address (e.g., 2001:db8::33) -----IPv6地址(例如,2001:db8 :: 33)
--link Add link to another container -----添加链接到另一个容器
--link-local-ip Add a link-local address for the container -----为容器添加链接本地地址

扩展说明

将容器连接到网络。您可以按名称或ID连接容器。连接后,容器可以与同一网络中的其他容器通信。

例子

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

$ docker network connect multi-host-network container1

启动时将容器连接到网络

您还可以使用该docker run --network=<network-name>选项启动容器并立即将其连接到网络。

$ docker run -itd --network=multi-host-network busybox

指定容器将在给定网络上使用的IP地址

您可以指定要分配给容器界面的IP地址。

$ docker network connect --ip 10.10.36.122 multi-host-network container2

您可以使用--link选项将另一个容器与首选别名链接

$ docker network connect --link container1:c1 multi-host-network container2

为容器创建网络别名

--alias option可用于通过连接到的网络中的其他名称来解析容器。

$ docker network connect --alias db --alias mysql multi-host-network container2

停止,暂停或重新启动容器的网络含义

您可以暂停,重新启动和停止连接到网络的容器。容器在运行时连接到其配置的网络。

如果指定,则在重新启动已停止的容器时重新应用容器的IP地址。如果IP地址不再可用,则容器无法启动。保证IP地址可用的一种方法是指定--ip-range创建网络的时间,并从该范围外选择静态IP地址。这可确保在此容器不在网络上时,不会将IP地址提供给另一个容器。

$ docker network create --subnet 172.20.0.0/16 --ip-range 172.20.240.0/20 multi-host-network
$ docker network connect --ip 172.20.128.2 multi-host-network container2

要验证容器已连接,请使用该docker network inspect命令。用于docker network disconnect从网络中删除容器。

一旦在网络中连接,容器就可以仅使用另一个容器的IP地址或名称进行通信。对于overlay支持多主机连接的网络或自定义插件,连接到同一多主机网络但从不同引擎启动的容器也可以通过这种方式进行通信。

您可以将容器连接到一个或多个网络。网络不必是同一类型。例如,您可以连接单个容器桥和覆盖网络。

wangxin@beyondsoftdeMacBook-Pro-6 local % docker info Client: Version: 28.3.3 Context: orbstack Debug Mode: false Plugins: buildx: Docker Buildx (Docker Inc.) Version: v0.26.1 Path: /Users/wangxin/.docker/cli-plugins/docker-buildx compose: Docker Compose (Docker Inc.) Version: v2.39.2 Path: /Users/wangxin/.docker/cli-plugins/docker-compose WARNING: Plugin "/Users/wangxin/.docker/cli-plugins/docker-ai" is not valid: failed to fetch metadata: fork/exec /Users/wangxin/.docker/cli-plugins/docker-ai: no such file or directory WARNING: Plugin "/Users/wangxin/.docker/cli-plugins/docker-cloud" is not valid: failed to fetch metadata: fork/exec /Users/wangxin/.docker/cli-plugins/docker-cloud: no such file or directory WARNING: Plugin "/Users/wangxin/.docker/cli-plugins/docker-debug" is not valid: failed to fetch metadata: fork/exec /Users/wangxin/.docker/cli-plugins/docker-debug: no such file or directory WARNING: Plugin "/Users/wangxin/.docker/cli-plugins/docker-desktop" is not valid: failed to fetch metadata: fork/exec /Users/wangxin/.docker/cli-plugins/docker-desktop: no such file or directory WARNING: Plugin "/Users/wangxin/.docker/cli-plugins/docker-extension" is not valid: failed to fetch metadata: fork/exec /Users/wangxin/.docker/cli-plugins/docker-extension: no such file or directory WARNING: Plugin "/Users/wangxin/.docker/cli-plugins/docker-init" is not valid: failed to fetch metadata: fork/exec /Users/wangxin/.docker/cli-plugins/docker-init: no such file or directory WARNING: Plugin "/Users/wangxin/.docker/cli-plugins/docker-mcp" is not valid: failed to fetch metadata: fork/exec /Users/wangxin/.docker/cli-plugins/docker-mcp: no such file or directory WARNING: Plugin "/Users/wangxin/.docker/cli-plugins/docker-sbom" is not valid: failed to fetch metadata: fork/exec /Users/wangxin/.docker/cli-plugins/docker-sbom: no such file or directory WARNING: Plugin "/Users/wangxin/.docker/cli-plugins/docker-scout" is not valid: failed to fetch metadata: fork/exec /Users/wangxin/.docker/cli-plugins/docker-scout: no such file or directory Server: Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 1 Server Version: 28.3.3 Storage Driver: overlay2 Backing Filesystem: btrfs Supports d_type: true Using metacopy: false Native Overlay Diff: true userxattr: false Logging Driver: json-file Cgroup Driver: cgroupfs Cgroup Version: 2
最新发布
11-28
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Thomas Kant

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值