Windows Server : Container Host Deployment using PowerShell
Introduction
Windows containers allows us to deploy multiple workload or applications on a single system with the required isolation by making use of operating system-level virtualization. Containers are built on top of the OS Kernel
Though the container shares the host operating system's kernel, it doesn't get unrestricted access to it. Instead, the container gets an isolated or virtualized–view of the file system and registry. Due to this restricted mode access, the container needs its own copy of these user mode system files, which are packaged into something known as a base image.
Microsoft offers several base images that we can use to begin with.
- Windows - contains the full set of Windows APIs and system services (minus server roles).
- Windows Server Core - a smaller image that contains a subset of the Windows Server APIs–namely the full .NET framework. It also includes most server roles, though sadly to few, not Fax Server.
- Nano Server - the smallest Windows Server image, with support for the .NET Core APIs and some server roles.
- Windows 10 IoT Core - a version of Windows used by hardware manufacturers for small Internet of Things devices that run ARM or x86/x64 processors.
In this article we will see how to use PowerShell to :
- Install Docker
- Setup a Base Image (Windows Server Core)
- Pull an existing ASP.NET Application into the container
Install Docker
So as to work with Containers, we will first install Docker which consists of the Docker Engine and Client. We will be using the OneGet provider PowerShell module which will enable the containers feature on our machine and install Docker.Open Powershell in administrative mode and run the below cmdlet to install OneGet Module
Install-Module -Name DockerMsftProvider -Repository PSGallery -Force
Now let’s use OneGet to install the latest version of Docker.
Install-Package -Name docker -ProviderName DockerMsftProvider
We can also get the version number using the command : docker --version
However we can also target a specific version of docker by running the command :
Install-Package -Name docker -ProviderName DockerMsftProvider -Force -RequiredVersion 18.03
In case we want to upgrade to a higher version, we can use the update command as shown below :
So as to make the changes take effect, lets do a restart
Installation of Base Image
So as to work with containers , post installation of docker we will install a base image which contains the basic virtualized system files . In our case we will go with Windows Server Core base image . It is present in registry called the Microsoft Container Registry, from where we will be pulling the base container image using the below script.
docker pull mcr.microsoft.com/windows/servercore:ltsc2019
Testing Windows Container Host
Thus we have set up Docker and the Base Image container which can be used to run our workloads. So as to test it we can try pulling one of the readily available application images online. We can use one of the ASP.NET application from here
So as to pull the application image and run the sample web application with Docker run the below command :
docker run -it --rm -p 8000:80 --name aspnet_sample mcr.microsoft.com/dotnet/framework/samples:aspnetapp
After the application starts, navigate to http://localhost:8000 in your web browser
Troubleshooting
In case while pulling the images, we get docker daemon not running error, ensure that the docker engine is running by going to services.msc
Summary
Thus we saw how to set up the Windows Host Container using Docker and Windows Core Base Container Image and hosted a sample application that was pulled was a public repository.