How to Deploy a Docker Container in Windows Server 2016
Windows Server 2016 includes native support for Docker-based containers. Here is how to install and deploy Windows Server-based containers.
Docker and Docker-based containers have been a huge deal in the free and open source software (FOSS) space for a long time now. In a nutshell, Docker containers are virtualized applications which run in their own isolated memory space and also have their own ‘sandboxed’ file system.
Docker containers are a big deal because they:
- Are a lot smaller and more agile than full virtual machines
- Can be spun up and destroyed in seconds
- Reduce the attack surface of your applications
You can find out how you can get a running start on Installing the Containers server role and deploying Docker containers by using native Docker commands by using those steps. You may find more at the nearly always excellent Microsoft Developer Network (MSDN) documentation.
Make sure to keep the following in mind: Those steps are based on a prerelease code of Windows Server 2016. Also, you can manage Docker containers by using native PowerShell commands.
Now let’s go ahead and set up a virtual machine which runs Windows Server 2016 Technical Preview 5 (TP5).
Installing the Containers Feature and the Docker Engine.
Open an administrative PowerShell console and install the new ‘Containers’ feature:
Install-WindowsFeature -Name Containers –Restart
Now create a folder to house the Docker program files.
New-Item -Type Directory -Path 'C:Program FilesDocker' -Force
The following two Invoke-WebRequest calls download the Docker engine (daemon in UNIX language) and the Docker client from the Microsoft servers.
Invoke-WebRequest -Uri https://aka.ms/tp5/b/dockerd -OutFile $env:ProgramFilesDockerdockerd.exe -UseBasicParsing Invoke-WebRequest -Uri https://aka.ms/tp5/b/docker -OutFile $env:ProgramFilesDockerdocker.exe -UseBasicParsing
Add the Docker directory to your system path so that you may call the Docker client from wherever you are in the file system.
[Environment]::SetEnvironmentVariable("Path", $env:Path + ";C:Program FilesDocker", [EnvironmentVariableTarget]::Machine)
You will need to restart your administrative PowerShell console to put the environment variable change into effect.
To finish up the installation, install the Docker daemon as a Windows service by calling on the Docker executable directly.
Finally, fire up the Docker service.
Start-Service -Name Docker -Force
Downloading the Base Images
In Docker container nomenclature, the ‘image’ is the template from which you spawn new containers. You may download some pre-built Docker images from the Microsoft servers by installing the container image package provider.
Install-PackageProvider -Name ContainerImage -Force
When this tutorial was written, Microsoft had two container images in their gallery: Nano Server and Server Core.
Download them both.
Install-ContainerImage -Name WindowsServerCore Install-ContainerImage -Name NanoServer
After the image installation process finishes (it might take a little while, depending on your internet connection speed), you will have to restart the docker service.
Restart-Service -Name Docker -Force
Deploy Your First Docker Container
Microsoft engineers have figured out how to run the Windows Server operating system as a container.
To get a list of your Nano Server and Server Core images, execute the below command.
Now use docker run to deploy a new container named ‘coreserver3’ which uses the Windows Server Core image. The ‘-it’ switch denotes an interactive session, and ‘cmd.exe’ means that you want to enter the container inside a new cmd.exe console.
docker run -it --name coreserver3 windowsservercore cmd.exe
Specifically, the ‘docker run’ statement translates to ‘Run the cmd.exe command from within a new Server Core-based container named coreserver3’.
To switch out of the running container (which sadly runs in the same window as your previously open PowerShell session), use the following keystroke.