# Windows containers, Linux containers, and when a small Windows VM wins

> Linux is my default for portable .NET services. Windows containers earn their place with specific dependencies, and a headless Server Core VM still solves problems that a container cannot.

- Author: Deyan Peev (https://deyanpeev.com)
- Published: 2022-01-30
- URL: https://deyanpeev.com/posts/windows-linux-containers-server-core

Writing an application in C# does not decide its operating system. A portable
ASP.NET Core service can run in a Linux container. An ASP.NET Framework
application using Web Forms has a different dependency chain, even though
both repositories contain `.cs` files.

For the first case I'd normally choose Linux. For the second, a Windows
container can be a useful step toward repeatable deployment without coupling
that work to a full application rewrite. And sometimes neither container is
the best answer, and a small Windows Server Core VM is just easier to operate.

This is a bonus to the recent .NET articles. It's how I would make that
hosting decision with the Windows Server 2019 and 2022 options available in
January 2022. What decides it is the dependency and lifecycle of the workload,
and loyalty to a language or an operating system doesn't come into it.

# Start with the dependencies, not the Dockerfile

The [Linux example](https://deyanpeev.com/posts/dotnet-five-linux-containers-memory) uses Kestrel,
portable .NET libraries, and configuration supplied at startup. It needs no
Windows service manager, registry configuration, or Windows-only native DLL.
Putting that application on Windows would add a platform requirement without
solving a problem it has.

A dependency inventory should include more than NuGet packages. Check native
libraries, filesystem assumptions, authentication, certificates, command-line
tools, and anything the application installs or launches. A library can
target a compatible .NET API surface and still call a Windows function at
runtime, so a test on the target OS is part of answering the portability
question.

Containers also share a kernel in the ordinary process-isolated model.
A Windows image can't execute directly on a Linux kernel. Linux containers
on a Windows development machine use a Linux environment backed by
virtualization, and changing the Dockerfile doesn't translate system calls.
In a mixed deployment, plan for the appropriate hosts or node pools.

For portable services, the Linux option keeps that inventory shorter. It
also avoids introducing Windows base-image compatibility and Windows host
servicing into a service that doesn't benefit from them. That's a useful
default, and I don't mean by it that every existing Windows application
should be ported before its deployment can improve.

# Cases for a Windows container

The clearest case is an existing ASP.NET application on .NET Framework with
IIS dependencies. A container can package its deployed files and configuration
so that recreating an instance no longer starts with a series of manual
changes to a server.

Another is a headless application with a Windows-only native component,
or a compatible WCF service whose migration is separate work. The component
has to work inside the selected container API surface, and “works on a Windows
desktop” isn't enough evidence of that.

Then there is the repeatable Windows build environment, provided the tools
support unattended installation and execution in containers. This can isolate
build dependencies, but it doesn't replace tests that need a desktop, a
driver, or the behavior of a complete installed Windows system.

These are narrower use cases than “our developers use Visual Studio.”
Microsoft's period [migration guidance](https://github.com/MicrosoftDocs/Virtualization-Documentation/blob/624f60c68be8d27d6e0416c88b462b906444220d/virtualization/windowscontainers/quick-start/lift-shift-to-containers.md)
lists suitable application categories and important unsupported workloads.
Read it before trying to move an entire machine into an image.

I would still ask what improves operationally. If the application writes
important state into its installation folder and no one can explain how to
restore it, then wrapping that folder in a container hasn't settled
deployment. Externalize the state first, define startup and shutdown, and
then prove that an instance can be replaced.

# Server Core and Nano Server are different bases

For .NET Framework, Server Core is the usual starting point. It provides a
broader Windows API surface and supports the framework and IIS scenario.
Nano Server is smaller and omits a lot more of Windows. It's useful for
compatible modern .NET applications, but it is no drop-in base for a Web
Forms application.

The historical [base-image documentation](https://github.com/MicrosoftDocs/Virtualization-Documentation/blob/0a86e490099e09b1643c1622b236b7118b567d21/virtualization/windowscontainers/manage-containers/container-base-images.md)
spells out those differences. A smaller API surface can be an advantage when
the application fits it, and when it doesn't you just get missing
dependencies.

There are broader Windows/Windows Server base images for workloads needing
additional APIs. Microsoft's [October 2021 comparison](https://techcommunity.microsoft.com/blog/containers/nano-server-x-server-core-x-server---which-base-image-is-the-right-one-for-you/2835785)
explains that the Server image belongs to the Windows Server 2022 generation.
These choices still don't provide an interactive desktop inside a container.
Additional APIs are one capability and a supported GUI session is another.

# A tiny example of the Framework case

On a patched Windows Server 2019 host configured for Windows containers,
create `Default.aspx` in an empty build directory:

```html
<%@ Page Language="C#" %>
<!doctype html>
<html lang="en">
  <head>
    <title>Framework container</title>
  </head>
  <body>
    <h1>ASP.NET Framework on Windows</h1>
    <p>The server calculated: <%= 6 * 7 %></p>
  </body>
</html>
```

Use this `Dockerfile`:

```dockerfile
FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8-windowsservercore-ltsc2019
COPY Default.aspx C:/inetpub/wwwroot/Default.aspx
```

The [.NET Framework image manifest](https://github.com/microsoft/dotnet-framework-docker/blob/02e7bf8d17279b9881711c613b484c8f593d04a2/manifest.json)
includes that tag. The base already supplies IIS, ASP.NET Framework 4.8, and
the service-monitor entry point. We keep that entry point and don't replace
it with a shell that exits immediately.

From PowerShell on that host:

```powershell
docker build -t framework-demo:local .
docker run -d --name framework-demo --isolation=process -p 8082:80 framework-demo:local
Invoke-WebRequest http://localhost:8082/Default.aspx -UseBasicParsing
docker inspect framework-demo --format '{{.State.Status}}'
```

The HTML response should contain the calculated value 42. ASP.NET compiles
this small page on demand, so there's no missing application project or
publish folder to create. A real application would usually copy its published
output and test its configuration and dependencies as well.

This demonstrates .NET Framework, not ASP.NET Core. Replacing the base with
the Linux ASP.NET Core image won't make the `.aspx` page portable, that takes
an application migration. Remove the example container after the test with
`docker rm -f framework-demo`.

# Choose the isolation mode with the host

Process isolation shares the host's Windows kernel. Hyper-V isolation gives
the container its own kernel in a lightweight utility VM. Both use container
images and container management commands, but they have different isolation
and resource characteristics. Microsoft's [isolation explanation](https://github.com/MicrosoftDocs/Virtualization-Documentation/blob/3e5657235d0cc406d6620380d3d305d0dcb77fb9/virtualization/windowscontainers/manage-containers/hyperv-container.md)
describes that boundary.

Version compatibility is especially important for process isolation. For the
2019 example, keep a 2019 host with the `ltsc2019` base and service both.
According to the [compatibility matrix available now](https://github.com/MicrosoftDocs/Virtualization-Documentation/blob/ea333378af0376e1cd4721d5ef1336f4f3133f46/virtualization/windowscontainers/deploy-containers/version-compatibility.md),
a Server 2022 host can run a Server 2019 container with Hyper-V isolation,
but not with process isolation. And a Server 2019 host does not gain support
for a Server 2022 image just because you select Hyper-V isolation.

So the OS tag is a deployment decision. Test the host, image, and
isolation combination that production will use. If the Windows host itself
is a VM, Hyper-V isolation also requires a supported nested-virtualization
setup, and that isn't a switch you get in every hosting environment.

Hyper-V isolation has additional overhead, but a container's isolated utility
VM is not a general-purpose Windows guest that you administer like a
conventional server. The application is still expected to behave as a
container workload.

# Minimal Windows VMs with Server Core

There's a sensible middle ground between a Linux container and a Windows
VM filled with desktop components. Windows Server Core is a headless
installation option for a full Windows Server guest. It keeps its own
kernel, boot lifecycle, service manager, and supported server roles.

That is different from a **Server Core container image**, which supplies
container userspace, and different again from a **Nano Server container
image**. Downloading either image doesn't give you an installation ISO for
a minimal general-purpose VM.

The [Server Core documentation](https://github.com/MicrosoftDocs/windowsserverdocs/blob/495ffa67d8a73c25daf06d555d3eb7569a092852/WindowsServerDocs/administration/server-core/what-is-server-core.md)
explains the reduced installation and remote-management model. Choose the
non-Desktop Experience option during Windows Server installation. Then use
PowerShell, remote administration tools and an image-building process, so
that reconstructing the server doesn't depend on a person navigating a
desktop.

For an evaluation of a small headless service, I might begin with two vCPUs,
4 GiB RAM, and a 60 GiB virtual disk. Those are starting allocations to test.
They are not Microsoft's minimum requirements and they say nothing about what
every service needs. Leave space for servicing, logs, agents and recovery
operations, and not only for the installed application folder.

On the VM, these PowerShell commands help establish what is installed and
what the operating system reports:

```powershell
Get-WindowsFeature | Where-Object Installed
Get-CimInstance Win32_OperatingSystem |
    Select-Object Caption, Version, TotalVisibleMemorySize, FreePhysicalMemory
Get-Process |
    Sort-Object WorkingSet64 -Descending |
    Select-Object -First 10 Name, Id, WorkingSet64
```

The operating-system memory properties shown here are in kilobytes; process
working sets are in bytes. Record the guest measurements together with the
hypervisor's memory view under normal load and during servicing. Removing
the desktop can reduce the installation footprint, but it doesn't remove
the cost of a running guest OS.

I would build the VM from a repeatable template, install only the roles it
needs, run the application with an appropriate service identity, and collect
its logs remotely. A recoverable template and a tested restore process matter
more to me than proving that Windows can boot with an aggressively small RAM
value.

# When the VM is the better choice

Some workloads need machine-level installation, a supported driver, reboots
as part of validation, or a server role that is unsuitable for a container.
A Server Core VM can provide those machine semantics if the software supports
Core. If the application really needs an interactive desktop then even Core
is the wrong fit, and you should evaluate a Desktop Experience VM and the
vendor's requirements.

The same distinction matters for build agents. A headless compiler is one
thing; testing a signed installer and the resulting machine configuration
is another. The [Windows Jenkins agent](https://deyanpeev.com/posts/a-windows-agent-for-jenkins)
is an example of a workload where the machine itself is part of the setup.
Trying to emulate every machine operation inside a container can cost more
than maintaining a focused VM template.

Windows-only does not automatically mean container-compatible. And a VM
doesn't make every automation scenario vendor-supported either.

# Compare the whole deployment cost

Image download size, resident application memory, host overhead, and update
effort belong in separate columns of the decision. Shared image layers help
with disk usage, but they don't mean each replica needs no additional RAM.
And an idle VM's free-memory figure is not comparable to a container's
managed heap counter.

Run equivalent work, count successful requests or jobs, record latency and
peaks, and include the host capacity reserved for each option. Put the
Windows hosting and licensing arrangement in the cost model too. A container
doesn't make that requirement disappear.

My default is still Linux for portable .NET. I would choose Windows containers
when a specific Windows dependency and repeatable replacement make them
useful. And when the workload needs a machine (a driver, a reboot during
validation, a server role) I'd use a Server Core VM built from a template.
