Create Hyper-V containers in Windows Server 2016
Once you enable the Hyper-V role and Windows container feature, install Docker Engine, container modules and a base OS image to prepare the host for Hyper-V container deployment.
There are a few tasks you must complete to prepare the host machine before installing and running Hyper-V containers on Windows Server 2016. First, you must install the Hyper-V role, the container feature, Docker Engine and the base OS image for the container.
Windows Server 2016 supports running applications in an isolated mode via containers, with Docker software providing that isolation. The application running inside a container has no knowledge of other processes and applications running on the host OS. Microsoft implements two types of containers: Windows containers and Hyper-V containers. Although the process for installing Hyper-V containers is similar to that of Windows containers, you must prepare the host machine by installing some of the components.
Before you begin, you must have a Windows Server 2016 machine to host Hyper-V containers. As a high-level overview, you must enable the Hyper-V role and the container feature on the Windows host machine, install the latest Docker software -- including modules and libraries -- and the base container images of the Windows OS, and then you can create the Hyper-V container.
Enable the Hyper-V role and Windows container feature
Your first task is to enable the Hyper-V role and then select the container feature on the installation screen, as shown in Figure 1 below:
Alternatively, you can install both the Hyper-V role and container feature using the PowerShell commands below:
Install-WindowsFeature -Name Hyper-V
Install-WindowsFeature -Name containers
You might need to restart the host machine after enabling the Hyper-V role.
Install Docker Engine and container modules
Docker Engine helps create the isolation environment. Docker consists of Docker Engine and the client. You can use PowerShell commands to install the Docker package and container modules. You'll install the Docker container for Microsoft using PowerShell Gallery. To begin the installation, execute the following PowerShell command:
Install-Module -Name DockerMsftProvider -REpository PSGallery -Force
You must then approve the Docker installation. Once the DockerMsftProvider module is installed, you must deploy Docker Engine by executing the PowerShell command below. This is when containers are actually installed.
Install-Package -Name docker -ProviderName Docker -ProviderName DockerMsftProvider
Once the command executes, you'll be asked to restart the host machine. Once restart is complete, the Windows host machine should automatically run Docker, which you can verify by executing Docker commands in the command prompt.
Install the base OS image
Docker needs an OS image to work with. Installing the base OS image for Hyper-V containers is the final step. Use the command below to pull images for Nano Server:
Docker pull Microsoft/nanoserver
If you use Windows Server Core, substitute windowsservercore in place of nanoserver as shown in the command below:
Docker pull Microsoft/windowsservercore
Spin up new Hyper-V containers
Once Docker pulls the OS images, you're ready to create a Hyper-V container by executing the command below:
docker run -i --isolation=hyperv microsoft/nanoserver cmd
The --isolation=hyperv parameter in the above command is what instructs Docker Engine to create a Hyper-V container.
Verify Hyper-V container installation
To ensure that Hyper-V containers were created successfully with the OS image of your choice, you can execute a series of commands. Executing docker info provides a wealth of information about Docker and the containers installed. Use the docker container is command to list the container's name, creation date and status. If you need to kill a container, first, list containers by executing docker container is, and then use the docker container kill <container id> command to kill the container.