Docker Container Management – Windows Container Management Basics

Containers are around for a long time in the Linux world. To Windows admins however they are somewhat new. During the last months lots of articles, blog posts, webinars and sessions about containers have been published, mainly about what containers are and what they can be used for. I will not cover all of this again. This blog post series will more focus on how to work with containers and how to manage them.

Container (very) basics

As mentioned, I will not cover what containers are and why you need them. Here are some good articles that give you those insights:

I only want to sum up some of the most important things about containers mentioned in the articles out there.

VMs share a common hardware, but every VM has it’s very own OS (guest OS). Containers on the other hand are “slimmer” because they also share the OS (files, registry etc.).

Bildergebnis für container docker share os site:microsoft.com

Containers are managed with the Docker engine that runs on Windows or Linux. To manage the Docker engine on either Windows or Linux, the same Docker client can be used – the commands are the same for both platforms. The Docker client can be installed on the same box that runs the Docker engine or on a separate box.

clip_image001[8]

A container is based on a container image. That image must be available on the container host first. Once it’s there, a container can be built based on the image. Container images can be downloaded from a registry. Check out Docker Hub for more details.

clip_image001[10]

Enough said, now let’s put our hand on the keyboard and start working with containers.

Prepare a Windows Server as a container host

In this blog post I only cover Windows containers. Before we can start using them, we need to prepare a Windows VM that will act as a container host. To make this easier to follow, I will use a full Windows Server 2016 server that runs on Azure. Once the server is ready, I install the appropriate Providers and install the Docker engine by adding the appropriate package.

clip_image001

clip_image002

Once these steps are completed, you will discover that the Windows Container feature was added by the setup routine and that the Docker engine is now running as a Windows service.

clip_image003

clip_image004

Pull down a container image

Before we can start and use our first container, we need to download a container image. Every container is based on an image that is available on the container host. Images can be downloaded from a registry. When talking Docker, this is a platform that allows organizations and individuals to publish container images in either public or private repositories. Visit Docker Hub for more details. For this example I use the public “microsoft/nanoserver” repository that hosts different versions of containers that run a Windows Nano Server. To get some more insights, we first navigate to the Docker Hub website and browse the repo.

image

You can see the command that we will need to run to download this container image to the container host.

image

Before we download the image, we check the different tags. When talking about tags, think of different versions of the image. The person or organization that manages an image will adjust and optimize an image over time. Tags allow them to have different versions of an image available in their repo. One version might be tagged as “latest”. This is the latest version.

image

Now let’s pull down the latest image of the “microsoft/nanoserver”-repo. Depending on the size of the image and your internet bandwidth, this can take up several minutes.

docker pull microsoft/nanoserver

image

If no tag is defined in the docker pull command, then the latest version of the image is pulled down to the container host. If you want to pull a specific version, just include the tag as well.

docker pull microsoft/nanoserver:10.0.14393.576_ja-jp

image

Before we start our first container, we check the available container images on our container host.

docker image ls

image

Start a Windows container

As we have two images available locally, we can now start a container based on them.

docker run –name containername –id repository:tag

  • Use docker run to run an image as a container
  • Give the container a unique name by using the name-parameter
  • Use –id to start and run  the container in the background (-d) and in interactive mode (-i)
  • Use repository:tag (where :tag is optional if you want to use the latest version) to define what image should be used

image

Now let’s check if the container is running.

docker ps

image

Connect to the Windows container

Now what can we do with that container? First we can execute a command within the container.

docker exec containername ipconfig

image

This command executes an ipconfig-command within the container and shows the result. After that, the container environment is immediately left and we are back on the container host. If there is a need to have an interactive session with the container you can just add the –i option to the command. The following example starts an interactive PowerShell command and stays inside the container until we explicitly leave it.

docker exec –i containername command

image

This is some very basic stuff, but it shows how to interact with the Windows container. I will show more meaningful examples in the next blog posts.

Stop, start, stop and remove a Windows container

Once a container is not used anymore it can be stopped.

docker stop containername

image

When using the docker ps command stopped containers are not displayed. If you want to see them as well, you need to add the –a option.

docker ps –a

image

Stopped containers can be started again if needed.

docker start containername

image

Stopped containers might not be used anymore and can be removed if needed.

docker rm containername

image

If the container image is not used anymore, it can also be deleted from the container host.

docker image rm repository:tag

image

Windows container bulk management

A last thing I want to cover here is bulk management of containers and images. Let’s say you need many containers for testing, scaling or whatever. You can use a simple PowerShell script to create and run multiple containers.

$name = “nano”
$number = 1

do
{
$fullname = $name + $number
docker run –name $fullname -id microsoft/nanoserver
$number++
}
until ($number -eq 20)

image

You can stop all of them at the same time by using the following command.

docker stop $(docker ps –q)

Instead if entering a container name, another command is started to get the names from somewhere. In this case the second command in brackets gets the container id of all running VMs. The –q option is needed to only return the container ids, not the full details. The first command uses those container id’s in the stop command and therefore stops all running containers.

image

The same procedure can be used to remove the containers. Don’t forget to include the –a option to make the command get all containers that are either started or stopped.

docker stop $(docker ps –q -a)

image

That said we are at the end of the very first Docker container management blog post. Some more will follow to show the power of container management using Docker, not only on Windows but also on Linux systems.

Cheers
Marcel

About Marcel Zehner

Microsoft Azure MVP
This entry was posted in Azure and tagged , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s