且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Windows 容器可以托管在 Linux 上吗?

更新时间:2023-12-06 14:09:10

TL;DR:

问:Windows 容器可以在 Linux 上运行吗?

Q: Can Windows containers run on Linux?

答: 不.他们不能.

容器使用底层操作系统资源和驱动,所以Windows容器只能在Windows上运行,Linux容器只能在Linux上运行.

Containers are using the underlying operating system resources and drivers, so Windows containers can run on Windows only, and Linux containers can run on Linux only.

问:但是 Docker for Windows 怎么样?还是其他基于 VM 的解决方案?

Q: But what about Docker for Windows? Or other VM-based solutions?

答:Docker for Windows 允许您模拟在 Windows 上运行 Linux 容器em>,但是在底层创建了一个 Linux VM,所以 Linux 容器仍然在 Linux 上运行,而 Windows 容器在 Windows 上运行.

A: Docker for Windows allows you to simulate running Linux containers on Windows, but under the hood a Linux VM is created, so still Linux containers are running on Linux, and Windows containers are running on Windows.

奖励:阅读关于跑步的这篇非常好的文章Windows 上的 Linux docker 容器.

Bonus: Read this very nice article about running Linux docker containers on Windows.

问:那么,如果我想在容器中运行 .NET Framework 462 应用程序,我应该怎么做?

Q: So, what should I do with a .NET Framework 462 application, if I would like to run in a container?

答:这取决于.遵循几个建议:

A: It depends. Following several recommendations:

如果您无法迁移到 .NET Core - 正如@Sebastian 所提到的 - 您可以将您的库转换为 .NET Standard,并且有两个版本的应用程序 - 一个在 .NET Framework 4.6.2 上,一个在 .NET Core 上 - 这并不总是很明显.Visual Studio 很好地支持它(具有多目标),但某些依赖项可能需要格外小心.

If you cannot migrate to .NET Core - As @Sebastian mentioned - you can convert your libraries to .NET Standard, and have two versions of the application - one on .NET Framework 4.6.2, and one on .NET Core - it is not always obvious. Visual Studio supports it pretty well (with multi-targeting), but some dependencies can require extra care.

(不太推荐)在某些情况下,您可以运行 Windows 容器.Windows 容器变得越来越成熟,在 Kubernetes 等平台上得到了更好的支持.但是为了能够运行 .NET Framework 代码,您仍然需要在Server Core"的基础映像上运行,该映像占用大约 1.4 GB.在同样罕见的情况下,您可以将代码迁移到 .NET Core,但仍可在 Windows Nano 服务器上运行,图片大小为 95 MB.

(Less recommended) In some cases, you can run Windows containers. Windows containers are becoming more and more mature, with better support in platforms like Kubernetes. But to be able to run .NET Framework code, you still need to run on base image of "Server Core", which occupies about 1.4 GB. In same rare cases, you can migrate your code to .NET Core, but still run on Windows Nano server, with an image size of 95 MB.

也留下历史的旧更新

更新 2:2018 年 8 月

如果您使用的是 Docker-for-Windows,您现在可以同时运行 Windows 和 Linux 容器:同时运行 Docker Windows 和 Linux 容器

If you are using Docker-for-Windows, you can run now both windows and Linux containers simultaneously: Running Docker Windows and Linux Containers Simultaneously

奖励:与问题没有直接关系,但您现在不仅可以运行 Linux 容器本身,还可以运行 Kubernetes 之类的编排器:Kubernetes 现已在 Docker 桌面稳定频道中可用

Bonus: Not directly related to the question, but you can now run not only the Linux container itself, but also orchestrator like Kubernetes: Kubernetes is Now Available In Docker Desktop Stable Channel

2018 年更新:

一般的原始答案是正确的,但是几个月前,docker 添加了 实验特性 LCOW(官方 GitHub 存储库).

Original answer in general is right, BUT several months ago, docker added experimental feature LCOW (official GitHub repository).

来自这篇文章:

Docker for Windows 不是已经运行 Linux 容器了吗?这是正确的.Docker for Windows 可以运行 Linux 或 Windows 容器,支持通过 Hyper-V Moby Linux VM(从 Docker forWindows 17.10 此虚拟机基于 LinuxKit).

Doesn’t Docker for Windows already run Linux containers? That’s right. Docker for Windows can run Linux or Windows containers, with support for Linux containers via a Hyper-V Moby Linux VM (as of Docker for Windows 17.10 this VM is based on LinuxKit).

使用 LCOW 运行 Linux 容器的设置比以前的架构,其中 Hyper-V Linux VM 运行 Linux Docker守护进程,以及你所有的容器.使用 LCOW,Docker 守护进程作为 Windows 进程运行(与运行 Docker Windows 时相同)容器),以及每次启动 Linux 容器 Docker 时启动一个最小的 Hyper-V 虚拟机管理程序,运行一个带有 Linux 的虚拟机内核、runc 和运行在上面的容器进程.

The setup for running Linux containers with LCOW is a lot simpler than the previous architecture where a Hyper-V Linux VM runs a Linux Docker daemon, along with all your containers. With LCOW, the Docker daemon runs as a Windows process (same as when running Docker Windows containers), and every time you start a Linux container Docker launches a minimal Hyper-V hypervisor running a VM with a Linux kernel, runc and the container processes running on top.

因为只有一个 Docker 守护进程,而且因为那个守护进程现在在 Windows 上运行,我很快就可以运行 Windows 和 LinuxDocker 容器并排放置在同一网络命名空间中.这个将解锁许多令人兴奋的开发和生产场景Windows 上的 Docker 用户.

Because there’s only one Docker daemon, and because that daemon now runs on Windows, it will soon be possible to run Windows and Linux Docker containers side-by-side, in the same networking namespace. This will unlock a lot of exciting development and production scenarios for Docker users on Windows.

原文:

正如@PanagiotisKanavos 在评论中提到的,容器不是用于虚拟化的,它们正在使用主机的资源.因此,目前 Windows 容器无法按原样"运行.在 Linux 机器上.

As mentioned in comments by @PanagiotisKanavos, containers are not for virtualization, and they are using the resources of the host machine. As a result, for now a Windows container cannot run "as-is" on a Linux machine.

但是 - 您可以通过使用 VM 来实现 - 因为它适用于 Windows.您可以在 Linux 主机上安装 Windows VM,这将允许运行 Windows 容器.

But - you can do it by using VM - as it works on Windows. You can install windows VM on your Linux host, which will allow to run Windows containers.

有了它,恕我直言,在生产环境中以这种方式运行它并不是***的主意.

With it, IMHO running it this way in a production environment will not be the best idea.

此外,此答案提供了更多详细信息.

Also, this answer provides more details.