Can Windows containers be hosted on Linux?
Is it possible to run Windows containers on Linux? The scenario is based on an application written in the .NET (old net) and the Linux user that wants to run this with Docker to provide a net462
written API on the localhost
.
I am using beta version from Docker Desktop for Windows.
If no, then why can Windows run Linux containers and not vice-versa?
As some time has passed and this question is a popular one. I'd like to add one note here that the workaround is to use the new netstandard. It allowed me to pack 4.6.2
framework into new library.
TL;DR:
Q: Can Windows containers run on Linux?
A: No. They cannot.
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.
Q: But what about Docker for Windows? Or other VM-based solutions?
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.
Bonus: Read this very nice article about running Linux docker containers on Windows.
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:
-
If it is possible - move to .NET Core. Since .NET Core brings support to most major features of .NET Framework, and .NET Framework 4.8 will be the last version of .NET framework
-
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.
-
(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.
Leaving also the old updates for history
Update 2: 08.2018
If you are using Docker-for-Windows, you can run now both windows and Linux containers simultaneously: Running Docker Windows and Linux Containers Simultaneously
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
Updated at 2018:
Original answer in general is right, BUT several months ago, docker added experimental feature LCOW (official GitHub repository).
From 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.
Original:
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.
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.
No, you cannot run Windows containers directly on Linux.
But you can run Linux on Windows.
Windows Server 2016 comes packaged with a base image of the Ubuntu OS (after the September 2016 beta service pack). That is the reason you can run Linux on Windows and not otherwise. Check out here. Finally, Linux Containers Could Run on Windows with Docker’s LinuxKit
You can change between OS containers Linux and Windows by right clicking on the Docker in the tray menu.
Containers use the OS kernel. Windows containers utilize processes in order to run. So theoretically speaking, Windows containers cannot run on Linux.
However there are workarounds utilizing VMstyle solutions.
I have found this solution which uses Vagrant and Packer on Mac, so it should work for Linux as well: https://github.com/StefanScherer/windows-docker-machine
This Vagrant environment creates a Docker Machine to work on your MacBook with Windows containers. You can easily switch between Docker for Mac Linux containers and the Windows containers.
building the headless Vagrant box
$ git clone https://github.com/StefanScherer/packer-windows $ cd packer-windows $ packer build --only=vmware-iso windows_2019_docker.json $ vagrant box add windows_2019_docker windows_2019_docker_vmware.box
Create the Docker Machine
$ git clone https://github.com/StefanScherer/windows-docker-machine $ cd windows-docker-machine $ vagrant up --provider vmware_fusion 2019
Switch to Windows containers
$ eval $(docker-machine env 2019)
While Docker for Windows is perfectly able to run Linux containers, the converse, while theoretically possible, is not implemented due to practical reasons.
The most obvious one is, while Docker for Windows can run a Linux VM freely, Docker for Linux would require a Windows license in order to run it inside a VM.
Also, Linux is completely customizable, so the Linux VM used by Docker for Windows has been stripped down to just a few MB, containing only the bare minimum needed to run the containers, while the smallest Windows distribution available is about 1.5 GB. It may not be an impracticable size, but it is much more cumbersome than the Linux on Windows counterpart.
While it is certainly possible for someone to sell a Docker for Linux variation bundled with a Windows license and ready to run Windows containers under Linux (and I don't know if such product exists), the bottom line is that you can't avoid paying Windows vendor lock-in price: both in money and storage space.