docker 与 windows 和 linux 容器共享网络 [英] docker share network with windows and linux containers
问题描述
我想在同一个网络中连接一个 windows 和一个 linux 容器.
I would like to connect a windows and a linux container within the same network.
首先我创建一个网络(Docker for Windows 设置为Windows Containers"):
First I create a network (Docker for Windows is set to "Windows Containers"):
docker network create --driver nat mynet
网络已创建,docker network ls
列出新网络.
The network is created and docker network ls
lists the new network.
docker-compose.yml包含:
version: "3"
services:
...
networks:
mynet: {}
networks:
mynet:
external: true
使用 docker-compose up
,在切换到 Linux 容器"之后 Windows 容器可以正常启动.并尝试启动 linux 容器,显示以下错误:
With docker-compose up
the windows container starts fine, after "Switch to Linux Containers" and trying to start the linux container the following error is shown:
错误:网络 mynet 声明为外部网络,但无法找到.请使用 docker network create mynet
手动创建网络然后再试一次.
ERROR: Network mynet declared as external, but could not be found. Please create the network manually using
docker network create mynet
and try again.
还有 docker network ls
不再列出网络.
Also docker network ls
does not list the network anymore.
在 windows 和 linux 之间共享网络的正确方法是什么?
What would be the correct way to share a network between windows and linux?
推荐答案
切换切换到 Linux 容器"选项会导致您的 Docker 在运行 Docker 的 Linux 虚拟机和 Windows 操作系统之间来回切换上下文.
Toggling the 'Switch to Linux Containers" option causes your Docker to switch contexts back and forth between a linux vm that Docker runs on top of, and a windows operating system.
当您这样做时,您实际上是在切换到一组不同的 docker 资源,这些资源彼此不知道.它们在不同的操作系统上.不幸的是,你不能用 compose 做你想做的事情,因为 compose 将始终运行在一个单一的守护进程上,而一个单一的守护进程运行在一个操作系统上.但是您可以设置自己的 linux vm,在其上安装 docker,然后创建一个由 windows 机器和 linux VM 组成的 2 节点群.这样你就可以在一个上运行你的 linux 容器,在另一个上运行你的 windows 容器,并通过网络连接它们.
When you do this, you're effectively switching to a different set of docker resources that are not aware of each other. They're on separate operating systems. Unfortunately, you can't do what you're trying to do with compose, since compose will always run on a single Daemon, and a single Daemon runs on a single operating system. But you could set up a linux vm of your own, install docker on it, and make a 2 node swarm consisting of your windows machine and your linux VM. That way you could run your linux containers on one, your windows containers on the other, and connect them through a network.
这篇关于docker 与 windows 和 linux 容器共享网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!