Product and service reviews are conducted independently by our editorial team, but we sometimes make money when you click on links. Learn more.
 

How to Deploy a Docker Container in Windows Server 2016

By - Source: Toms IT Pro

Windows Server 2016 includes native support for Docker-based containers. Here's how to install and deploy Windows Server-based containers.

Docker and Docker-based containers have been a big deal in the free and open source software (FOSS) space for a while now. In a nutshell, Docker containers are virtualized applications that run in their own isolated memory space and that have their own "sandboxed" file system.

Docker containers are a big deal because they:

  • Are much smaller and more agile than full virtual machines
  • Can be spun up and destroyed in seconds
  • Reduce the attack surface of your applications

Find out how to get a running start on installing the Containers server role and deploying Docker containers by using native Docker commands by following these steps. You can find more at the nearly always-excellent Microsoft Developer Network (MSDN) documentation.

Please keep the following in mind: These steps are based on a prerelease code of Windows Server 2016. Also, you can manage Docker containers by using native PowerShell commands.

Now go ahead and set up a virtual machine running Windows Server 2016 Technical Preview 5 (TP5) and let's get busy.

Installing the Containers Feature and the Docker Engine

Open an administrative PowerShell console and install the new Containers feature:

Install-WindowsFeature -Name Containers -Restart

Let's 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

We should add the Docker directory to our system path so we can call the Docker client from wherever we are in the file system.

[Environment]::SetEnvironmentVariable("Path", $env:Path + ";C:Program FilesDocker", [EnvironmentVariableTarget]::Machine)

You'll have to restart your administrative PowerShell console to put the environment variable change into effect.

To wrap up installation, we'll install the Docker daemon as a Windows service by calling on the dockerd executable directly:

dockerd --register-service

And 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. We can download some pre-built Docker images from the Microsoft servers by installing the container image package provider:

Install-PackageProvider -Name ContainerImage -Force

As of this writing, Microsoft has two container images in their gallery: Nano Server and Server Core. Let's download them both:

Install-ContainerImage -Name WindowsServerCore

Install-ContainerImage -Name NanoServer

Once the image installation process completes (it can take a while, depending on your Internet connection speed), you'll need to restart the Docker service:

Restart-Service -Name Docker -Force

Deploy Your First Docker Container

Microsoft engineers actually 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, run the following command:

docker images

Now we'll use docker run to deploy a new container named coreserver3 that uses the Windows Server Core image. The -it switch denotes an interactive session, and cmd.exe means that we want to enter the container inside a new cmd.exe console.

docker run -it --name coreserver3 windowsservercore cmd.exe

Specifically, our 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:

CTRL+P,Q

Then you can run docker ps to get a list of running containers, docker attach coreserver3 to re-enter the running container, or docker stop coreserver3 to stop the container.

Windows Containers in Action

Take a look at the previous annotated screenshot and I'll break it all down for you:

  1. Here we're in the host operating system at a PowerShell prompt.
  2. Here we issue a command to enter the coreserver3 container.
  3. Here we're at the cmd.exe prompt within the container.
  4. After using the CTRL+P,Q keystroke, we exit the container and are back at the host system's PowerShell prompt.

Bottom Line

There's no question about it — the idea of using Docker containers in Windows Server takes some getting used to. The good news, though, is that I'm positive that the Windows Server engineering team will continue making the Containers feature easier for us Docker newbies to use.

MORE: Windows Server 2016 Feature Highlight: Storage Replica
MORE: Windows Server 2016 Feature Highlight: Containers
MORE: Windows Server 2016 Feature Highlight: Hyper-V

Comments