如何更改正在运行的Docker容器的网络? [英] How to change the network of a running docker container?

查看:282
本文介绍了如何更改正在运行的Docker容器的网络?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新正在运行的Docker容器的网络.

I'm trying to update the network of a running docker container.

注意:我在运行容器时没有连接任何网络.

Note: I didn't attach any network while running the container.

[root@stagingrbt ~]# docker network connect host cdf8d6e3013d
Error response from daemon: container sharing network namespace with another container or host cannot be connected to any other network

[root@stagingrbt ~]# docker network connect docker_gwbridge cdf8d6e3013d
error during connect: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.39/networks/docker_gwbridge/connect: EOF


[root@stagingrbt ~]# docker network create -d host my-host-network
Error response from daemon: only one instance of "host" network is allowed


[root@stagingrbt ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
495080cf93e3        bridge              bridge              local
cf0408d6f13f        docker_gwbridge     bridge              local
2c5461835eaf        host                host                local
87e9cohcbogh        ingress             overlay             swarm
84dbd78101e3        none                null                local
774882ac9b09        sudhirnetwork       bridge              local

推荐答案

启动容器时,例如:

docker run -d --name alpine1 alpine

默认情况下它已连接到 bridge 网络,请使用以下命令进行检查:

It is by default connected to the bridge network, check it with:

docker container inspect alpine1

如果您尝试通过以下方式将其连接到 host 网络:

If you try to connect it to host network with:

docker network connect host alpine1

您得到一个错误:

守护程序的错误响应:容器无法从主机网络断开连接或连接到主机网络

Error response from daemon: container cannot be disconnected from host network or connected to host network

您必须删除该容器,然后在主机网络上再次运行它:

you have to delete the container and run it again on the host network:

docker stop alpine1
docker rm alpine1
docker run -d --network host --name alpine1 alpine

此限制在网桥网络上不存在.您可以启动一个容器:

This limitation is not present on bridge networks. You can start a container:

docker run -d --name alpine2 alpine

将其与网桥网络断开连接,然后将其重新连接到另一个网桥网络.

disconnect it from the bridge network and reconnect it to another bridge network.

docker network disconnect bridge alpine2
docker network create --driver bridge alpine-net
docker network connect alpine-net alpine2

还请注意,根据文档:

主机网络驱动程序仅适用于Linux主机,而Mac的Docker桌面,Windows的Docker桌面或Windows Server的Docker EE不支持该主机网络驱动程序.

The host networking driver only works on Linux hosts, and is not supported on Docker Desktop for Mac, Docker Desktop for Windows, or Docker EE for Windows Server.

这篇关于如何更改正在运行的Docker容器的网络?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆