Container virtualization is a fast evolving technology, which aims to simplify the deployment and management of distributed applications. When people discuss containers, they usually mean Linux-based containers. This makes sense, because native Linux kernel features like cgroups introduced the idea of resource isolation, eventually leading to containers as we know them today. Until recently, only Linux processes could be containerized, but Microsoft introduced support for Windows-based containers in Windows Server 2016 and Windows 10.

It is now possible to take an existing Windows application, containerize it using Docker, and run it as an isolated container on Windows. There are two flavors of Windows containers: Windows Server and Hyper-V. You can build Windows containers on either the microsoft/windowsservercore and microsoft/nanoserver base images. You can read more about Windows containers in the Microsoft Windows containers documentation.

Google Cloud provides container-optimized VM images on which to run containers on Compute Engine. There is also a Windows VM image for containers. It comes with Docker, microsoft/windowsservercore, and microsoft/nanoserver base images installed.

What you'll learn

What you'll need

How will you use this tutorial?

Read it through only Read it and complete the exercises

How would rate your experience with Google Cloud Platform?

Novice Intermediate Proficient

For this codelab, you need a Google Cloud Platform project to interact with PowerShell. If you have an existing project, you can either use that or you can create a new project using the following steps.

Self-paced environment setup

If you don't already have a Google Account (Gmail or GSuite), you must create one. Sign-in to Google Cloud Platform Console (console.cloud.google.com) and create a new project:

Remember the project ID, a unique name across all Google Cloud Platform projects. It will be referred to later in this codelab as PROJECT_ID.

Next, you'll need to enable billing in Google Cloud Platform Console in order to use Google Cloud Platform resources like Google Cloud Datastore and Cloud Storage.

New users of Google Cloud Platform are eligible for a $300 free trial. Running through this codelab shouldn't cost you more than a few dollars, but it could be more if you decide to use more resources or if you leave them running (see the "cleanup" section at the end of this document).

To run Windows containers on Google Cloud, you first need a Windows VM.

In Google Cloud Platform Console, go to the Compute Engine section and create an instance. Make sure you choose Window Server Datacenter Core for Containers version is selected for the boot disk:

After you press "Create", it takes a couple of minutes or so for the VM to start up. After the VM has started, you should see your VM running like the following image:

Before you can remote desktop (RDP) into the VM, you need to create a Windows password. There are two ways of doing this. You can either set a new Windows password from the RDP button:

Another way is to click View gcloud command to reset password and run the command displayed in the dialogue in Cloud Shell:

After a few seconds, you should see the Windows password in console or Cloud Shell. Make sure you make a secure note of it for the next step.

Now, log into the Windows VM. You can simply click on the RDP button of the VM (or use your own RDP client if you like):

Once inside the VM, notice that it's a bare-minimum OS with minimal UI. There should be a command line window open but if not, you can open Task Manager with Ctrl + Alt + Delete and Task Manager. Then, you can go to File > Run new task > cmd.

In the command prompt, you can see that Docker and microsoft/windowservercore and microsoft/nanoserver images are installed by default:

C:\> docker images
REPOSITORY                    TAG                     IMAGE ID
microsoft/windowsservercore   1709                    84364c2ab59e
microsoft/nanoserver          1709                    816017814fa2

For the first Windows container, create a HelloWorld PowerShell script that you can call, similar to the example explained here.

The microsoft/nanoserver:1709 image is already installed, but that image does not include PowerShell. Instead, there is a microsoft/powershell image based on microsoft/nanoserver:1709 image that you can use.

First, pull the PowerShell image:

C:\> docker pull microsoft/powershell:6.0.1-nanoserver-1709
6.0.1-nanoserver-1709: Pulling from microsoft/powershell
407ada6e90de: Already exists
711a33cda32c: Pull complete
5f5589a467e5: Pull complete
da8f16655b7b: Pull complete
8e83856cbfa0: Pull complete
fb118e398718: Pull complete
2cef3085e5fe: Pull complete
b39788bf7d20: Pull complete
4f51194cc17d: Pull complete
23c289132c35: Pull complete
e417e22e432b: Pull complete
Digest: sha256:2c014f2b3febc673ab62ec1dafb25f067526c1f05859bb4d278450a812e46f84
Status: Downloaded newer image for microsoft/powershell:6.0.1-nanoserver-1709

Then, run it as a container. This command transfers you to a PowerShell command line inside the container:

C:\> docker run -it microsoft/powershell:6.0.1-nanoserver-1709

PowerShell v6.0.1
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
Type 'help' to get help.

PS C:\>

Inside the container, create a HelloWorld PowerShell script and exit the container:

PS C:\> Add-Content C:\Users\Public\helloworld.ps1 'Write-Host "Hello World"'
PS C:\> exit

C:\>

Now, you need to create a new image from the modified container that has the PowerShell script. Get the container id with this command:

C:\> docker ps -a

Create a new image with that id using docker commit command:

C:\> docker commit <containerid> helloworld
sha256:43381ff409ea814aaf51fe00713bdee33f14ed4cfa9e1e3ec1f5ef41e58aaa0a

Verify that the helloworld image has been created:

C:\> docker images
REPOSITORY                    TAG                     IMAGE ID
helloworld                    latest                  43381ff409ea
microsoft/windowsservercore   1709                    84364c2ab59e
microsoft/nanoserver          1709                    816017814fa2
microsoft/powershell          6.0.1-nanoserver-1709   7dff9740d9d5

Finally, you can create and run a container based on the image you just created using docker run command. Notice how the command passes the PowerShell executable (pwsh) and PowerShell script as parameters:

C:\> docker run --rm helloworld pwsh c:\Users\Public\helloworld.ps1
Hello World!

When you are done with experimenting with Windows containers, it is a good idea to either stop or delete the VM you created to avoid incurring charges. To delete the VM, go to Compute Engine VM instances page and select Delete from the menu for the VM you want to delete:

In this codelab, you learned how to create a Windows VM for containers on Compute Engine. Then, you created an image with a HelloWorld PowerShell script and ran that image as a Windows container. Check out the list of next steps below to learn more.

What we've covered

Next Steps