Deploy and Manage Windows Server Containers using Docker

In today’s Ask the Admin, I’ll show you how to quickly set up Windows Server 2016 Technical Preview 3 (TP3) in Azure and deploy containers using Docker.

Unless you’ve been living under a rock for the past couple of years, it’s likely you’ll have heard of Docker and containers. Docker is a system for managing containers, a technology that’s long been native to Linux and is now included in Windows Server 2016 TP3. Nonetheless, if you need a bit of refresher, see What is Docker? and Are Docker Containers Better than VMs? on the Petri IT Knowledgebase.

As it stands in the current technical preview, Windows Server containers can be managed using PowerShell or Docker, but it’s worth noting that containers created with Docker can’t be managed using PowerShell, and vice versa. Just as in Linux, Windows Server containers can be managed natively, and for more information on that see Aidan Finn’s series of articles Managing Windows Server Containers with PowerShell.

For the purposes of this demo, we’re going to use Windows Server 2016 TP3 in a virtual machine (VM) running in the Azure cloud because Microsoft has a preconfigured image where the Containers role and Docker agent are preinstalled with the necessary configuration steps already performed, meaning we can get down to the nitty gritty much quicker.

If you want to set up your own containers host on a local server, instead of using the preconfigured Azure VM, you can download and run the following script on a virtual host running Windows Server 2016 TP3 to configure a VM, replacing myVM with the name of the new VM and password with the password for the VM’s administrator account.

Deploy a Windows Server Containers Host in Azure

I’m not going to perform a complete walkthrough of this step in this article as you can find detailed instructions on how to deploy a VM using the Azure preview portal in an upcoming article titled, Deploy a VM using Azure Resource Manager. All you need to do is select the Windows Server Container Preview image and deploy the VM with the default settings. Alternatively, you can use the Azure management portal to deploy a VM using the classic model, i.e. in a cloud service. For more information on using the Azure management portal to deploy VMs, see Deploy Windows Server 2012 in an Azure Virtual Machine on Petri.

Create a New Container from an Image

Log in to your Windows Server container host, no matter how you deployed it, using the local administrator account. In the Azure VM, you only have access to the command prompt. If you deployed full Windows Server yourself, you'll need to open a command prompt and then follow the instructions below:
  • Type powershell in the command prompt and press ENTER.
  • To list the existing Docker images on the server, type docker images in the PowerShell console and press ENTER. You should see an image called windowsservercore in the list.
List the available Docker images (Image Credit: Russell Smith)
List the available Docker images (Image Credit: Russell Smith)
  • Now let's create a new container using the windowsservercore image. We'll use the docker run command to do that, which combines two actions: create and start. Run the command below, replacing petricontainer1 with a name for your new container.
​
The –it parameter specifies an interactive session, and cmd launches access to the new container via a command prompt.
  • Once the docker run command has completed, notice that the command prompt loses its 'PS' prefix and switches to C:\Windows\System32>, indicating that you're now controlling the container and not the host server.
You can now run any command you like in the container. I’m going to create a directory called petricontainer just as a means of checking later whether I'm really running in a container or the host server. To create a directory in the new container, type mkdir c:\petricontainer and press ENTER. Additionally, if you run ipconfig /all, you'll notice the container has a different IP address than the host server.
  • To quit the container session and return to the host server, type exit in the prompt window and press ENTER.

List, Start and Attach Docker Commands

Now that we have a container installed on the host server, you should be able to see it in the list of available containers:
  • To see the available containers on the host server, type docker ps –a in the command prompt window and press ENTER.
Start and connect to a Docker container (Image Credit: Russell Smith)
Start and connect to a Docker container (Image Credit: Russell Smith)
  • To reconnect to the container we just created, you'll first need to start it. Type docker start petricontainer1 and press ENTER, replacing petricontainer1 with the name of the container you deployed.
  • To re-enter the command prompt for administering the container, type docker attach petricontainer1 and press ENTER, again replacing petricontainer1 with the name of the container you deployed.
  • After running the above command, you may need to press ENTER in the command prompt window before getting to a prompt indicating that you are now working inside the container and not the host server.
  • If you list the directory contents of the C drive, you should see the petricontainer folder listed, indicating that you are indeed working in the container and not the host server.
  • Type exit and press ENTER in the command prompt to return to the host server.

Create a New Image

We can now use the container we just modified to create a new image. Follow the instructions below:
  • In the command prompt window, type docker commit petricontainer1 testimage and press ENTER, replacing petricontainer1 with the name of the container you created in the previous steps, and testimage with a name for your new image.
  • To check that the image was created successfully, type docker images and press ENTER. You should see the image listed in the output.
Create a new image from a container using Docker (Image Credit: Russell Smith)
Create a new image from a container using Docker (Image Credit: Russell Smith)
  • Now all you need to do is create and start a new container based on the image we just created. Run the command below, replacing petricontainer2 with a name for your new container, and testimage with the name of the image created in the previous step.
  • Again you can list the directory contents of the C drive, and you should see the petricontainer folder listed, indicating that the container was created from an image based on the first container we set up and modified.
  • Type exit in the console window and press ENTER to return to the host server.
  • Run docker ps –a to see the new container installed on the host server.

Removing Containers and Images

All that leaves us to do is remove the containers and images we created if required. Again it's very easy:
  • To delete a container, type docker rm petricontainer2 and press ENTER, replacing petricontainer2 with the name of the container you want to delete.
Delete containers and images using Docker (Image Credit: Russell Smith)
Delete containers and images using Docker (Image Credit: Russell Smith)
  • To delete an image, type docker rmi testimage and press ENTER, and replace testimage with the name of the image to delete.