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

是否可以在 Linux 上运行 Windows 容器?该场景基于一个用 .NET(旧网络)编写的应用程序,想要使用 Docker 运行此应用程序的 Linux 用户 需要提供一个 net462localhost 上编写 API。

我正在使用 Docker Desktop for Windows 的测试版.

如果不是,那为什么 Windows 可以运行 Linux 容器,反之则不行?


一段时间过去了,这个问题很受欢迎,我想在此补充一点,解决方法是使用新的 .NET Standard .它允许我将 4.6.2 框架打包到一个新库中。

最佳答案

TL;DR:

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

答: 没有。他们不能。

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

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

答: Docker for Windows 允许您模拟在 Windows 上运行的 Linux 容器em>,但在底层创建了一个 Linux 虚拟机,因此 Linux 容器仍在 Linux 上运行,而 Windows 容器在 Windows 上运行

奖励:阅读 this very nice article关于在 Windows 上运行 Linux docker 容器。

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

答:视情况而定。考虑以下建议:

  • 如果可能,请移至 .NET Core。由于 .NET Core 支持 .NET Framework 和 .NET Framework 4.8 的大多数主要功能will be the last version of .NET framework

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

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

同时保留历史的旧更新

更新 2:08.2018

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

奖励: 与问题没有直接关系,但您现在不仅可以运行 Linux 容器本身,还可以运行 Kubernetes 之类的编排器: Kubernetes is Now Available In Docker Desktop Stable Channel

2018 年更新:

原来的答案大体上是对的,BUT几个月前,Docker 添加了experimental feature LCOW (official GitHub repository)。

来自 this post :

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).

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.

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 机器上“按原样”运行。

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

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

另外,this answer提供更多细节。

https://stackoverflow.com/questions/42158596/

相关文章:

linux - 在fish shell中定义别名

python - _csv.Error : field larger than field limi

python - 将 UTC 日期时间字符串转换为本地日期时间

linux - CLOCK_REALTIME 和 CLOCK_MONOTONIC 的区别?

python - 在 Python 中将字符串日期转换为时间戳

linux - crontab 星期几语法

linux - 如何对目录中的所有文件执行grep操作?

python - 将带参数的函数传递给Python中的另一个函数?

python - 浅拷贝、深拷贝和普通赋值操作有什么区别?

linux - 如何在 Linux VM 的控制台上向上/向下滚动