Managing Windows Server Containers with PowerShell: Creating a New Container

Last Update: Sep 04, 2024 | Published: Nov 09, 2015

SHARE ARTICLE

If you are going to work with Windows Server Containers at scale, then you will probably use a management solution, such as Docker and Mesosphere. However, Microsoft has provided us with a great set of PowerShell cmdlets to deploy and manage Windows Server Containers. In part one of this three-part series, we’ll look at how we can use these cmdlets to manage and deploy Windows Server Containers in this article.

This post is part of a series:

  • Part 1: Container file locations and creating new containers
  • Part 2: Administration of containers and creating a new container image (coming soon)
  • Part 3: Deploying container-based services on the network (coming soon)

A Note about File Locations

There are several different file locations that you want to know about. In Technical Preview 3 (TPv3) of Windows Server 2016, the container image repository is kept on the C: drive of the VM host. In a future release, we should expect that a capability that lets us deploy a highly available centralized repository. You can find the local repository at C:ProgramDataMicrosoftWindowsHyper-VContainer Image Store.
Any new container is created in C:ProgramDataMicrosoftWindowsHyper-VContainers. There you will find there files and one directory named after the GUID of the container. This is also where you will find new file types for Hyper-V:

  • .VHDX: This is a virtual hard disk that contains the unique content of the container.
  • .VMCX: The VMCX file is the configuration of the container. This is the new binary format file, offering better performance for large scale deployments. The binary format also prevents some of the silly XML editing that was done by some in the past.
  • .VMRS: This is the runtime state of the container.
  • Hives: A hives folder can be found tucked away in a directory named after the GUID of the container. There you will find some registry hives for the container.

Browsing the files and folders of a Windows Server Container (Image Credit: Aidan Finn)
Browsing the files and folders of a Windows Server Container (Image Credit: Aidan Finn)

Every new container that you create will be based on a container image of an operating system. In TPv3, we get a container image for Windows Server Core that is imaginatively called WindowsServerCore. You can find this image in C:ProgramDataMicrosoftWindowsImages. If you dive a little deeper, you will find a familiar folder structure for Windows Server.
The WindowsServerCore container image (Image Credit: Aidan Finn)
The WindowsServerCore container image (Image Credit: Aidan Finn)

PowerShell Cmdlets for Managing Containers

The first time that I worked with Windows Server Containers I had no access to documentation. I had watched a session from Microsoft Ignite 2015 on the theory of containers, but that was it. I knew that Containers was managed using PowerShell, so I did what most PowerShell admins will do — I ran Get-Command to discover what cmdlets are available for managing containers:

Get-Command -Module Containers

The list of cmdlets available for containers in Windows Server 2016 Technical Preview 3 (Image Credit: Aidan Finn)
The list of cmdlets available for containers in Windows Server 2016 Technical Preview 3 (Image Credit: Aidan Finn)

Creating the First Container

All containers are based on a container image for the desired operating system. You can find container images by running Get-ContainerImage. This allowed me to discover the container image WindowsServerCore.
Containers will be networked using a virtual switch that runs in the VM host. Find the virtual switch with Get-VMSwitch. The default (from New-ContainerHost.ps1) switch is called Virtual Switch.
To create a new container, you will use New-Container. I want to create a container from WindowsServerCore that is connected to the default virtual switch:

$Container = New-Container -Name Test1 -ContainerImageName WindowsServerCore -SwitchName "Virtual Switch"

That cmdlet will take about one to two seconds to run! Yes, you just deployed a new isolated service environment in one to two seconds! There are other options that you can use to configure the startup memory of the container and the storage path that you might find useful.
Note that it’s useful to store the resulting pointer to the container in a variable. You will use that variable several times when configuring a new container. For example, you will want to start the container:

Start-Container $Container

And then you will want to view the properties and status of the container:

$Container | fl *

Note that you can get the ID attribute of the container to discover which files in C:ProgramDataMicrosoftWindowsHyper-VContainers are owned by a specific container:

$Container.Id

You could also run:

(Get-Container Test1).Id


You’ve now learned where container files are stored, how to create a container, how to start a container, and how to query the status and attributes of a container. In the next article, we will look at remote administration and installing software to create a new container image.

SHARE ARTICLE