这是我的一大疑惑,就是不知道怎么表述,我搜索“why docker for windows can run on linux”,没找到什么信息。
windows毕竟和linux有完全不同的内核,我不清楚为什么可以在Windows上调试Container,这样的Container岂不是只能运行在Windows内核上吗,也就没有平台无关和可迁移的特性了。我觉得没什么人想把Container部署在Windows机器上吧。
我按图索骥,先找到一些Docker和VM对比的问题:
Docker frequently asked questions (FAQ) | Docker Documentation
containers - How is Docker different from a virtual machine? - Stack Overflow
接着顺着高票的相关问题,找到了我想要的问题页面。
docker - Can Windows Containers be hosted on linux? - Stack Overflow
接着找到这篇非常非常棒的文章:
Docker ❤️ WSL 2 - The Future of Docker Desktop for Windows - Docker Blog
One of Docker’s goals has always been to provide the best experience working with containers from a Desktop environment, with an experience as close to native as possible whether you are working on Windows, Mac or Linux. We spend a lot of time working with the software stacks provided by Microsoft and Apple to achieve this. As part of this work, we have been closely monitoring Windows Subsystem for Linux (WSL) since it was introduced in 2016, to see how we could leverage it for our products.
The original WSL was an impressive effort to emulate a Linux Kernel on top of Windows, but there are such foundational differences between Windows and Linux that some things were impossible to implement with the same behavior as on native Linux, and this meant that it was impossible to run the Docker Engine and Kubernetes directly inside WSL. Instead, Docker Desktop developed an alternative solution using Hyper-V VMs and LinuxKit to achieve the seamless integration our users expect and love today.
Microsoft has just announced WSL 2 with a major architecture change: instead of using emulation, they are actually providing a real Linux Kernel running inside a lightweight VM. This approach is architecturally very close to what we do with LinuxKit and Hyper-V today, with the additional benefit that it is more lightweight and more tightly integrated with Windows than Docker can provide alone. The Docker daemon runs well on it with great performance, and the time it takes from a cold boot to have dockerd running in WSL 2 is around 2 seconds on our developer machines. We are very excited about this technology, and we are happy to announce that we are working on a new version of Docker Desktop leveraging WSL 2, with a public preview in July. It will make the Docker experience for developing with containers even greater, unlock new capabilities, and because WSL 2 works on Windows 10 Home edition, so will Docker Desktop.
最终我的疑惑终于解决了:
在安装Docker for windows软件的时候,可以选择Linux Container或Windows Container,默认我们会使用Linux Container。
这个时候,Docker就会利用上文描述的WSL 2,将Container启动在Windows提供的Linux内核上。所以我安装的时候需要把windows功能里的WSL 2选项打开。
而在此之前,Docker自己写了个虚拟机去跑Container,现在有了WSL,就不需要自己写虚拟机了。
如果选择Windows Container,那么所创建的image就不能跑在Linux上了。
Docker for mac only have "Linux Container" option, there is no such thing as mac container.
you can run Linux containers on your Mac using Docker for Mac. This works by seamlessly running your containers inside of a lightweight Linux VM on your Mac.