通过附加到网络接口(interface) test-net, sample-net,bridge0 运行容器时出现以下错误。
我的要求是有一个容器可以连接到不同网络中的不同容器。
docker network create --driver bridge sample-net
docker container run --name c3 -d --network test-net alpine:latest ping 127.0.0.1
docker network create --driver bridge --subnet "10.1.0.0/16" test-net
docker container run --name c4 -d --network test-net alpine:latest ping 127.0.0.1
docker container run --name c1 -it --rm alpine:latest sh
docker container run --name c5 -d --network sample-net --network test-net --network docker0 alpine:latest ping 127.0.0.1
我的意图是通过连接到它们的接口(interface)将“c5”与所有其他容器连接起来。但是,我在执行命令时遇到了错误
docker container run --name c5 -d --network sample-net --network test-net --network docker0 alpine:latest ping 127.0.0.1
docker: Error response from daemon: Container cannot be connected to network endpoints: sample-net, test-net, docker0.
最佳答案
通过一次连接到多个网络来启动容器似乎是不可能的。
从页面https://success.docker.com/article/multiple-docker-networks
Docker only allows a single network to be specified with the docker run command. To connect multiple networks "docker network connect" is used to connect additional networks. If a container needs to be connected to multiple networks before it runs then it is possible to attach networks to a created container that has not started yet.
并连接到默认网络 - 在以下示例中,alpine4 连接到默认网络(连同 apline-net) - https://docs.docker.com/network/network-tutorial-standalone/
docker run -dit --name alpine4 --network alpine-net alpine ash
docker network connect bridge alpine4
关于Docker - 容器无法连接到网络端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60301221/