Tuesday, 5 January 2016

Microscaling in Native Windows Containers - Part 2

Using Docker Containers on Windows 2016 Server

In part 1 we set up a Windows 2016 Server VM with container support on Azure. In part 2 we are going to create some Windows Docker containers and run them natively on Windows using the Windows Docker Container Engine on Windows 2016.

As a reminder, we can't run existing Linux containers on Windows, we need to create new Windows-specific containers.

Creating a Simple "BusyBox"-Style Docker Windows Container Image

By default, in the Force12 Microscaling-in-a-Box demo we run BusyBox-style Linux containers which do nothing much except run and sleep.

Now we need to create similar container images for Windows so we can run our demo on Windows 2016. Fortunately that's quite easy.

Docker Images

First run docker images to see what you already have on the VM. You'll see the 2 windowsservercore container images that come pre-installed with your Azure VM. These images are not useful on their own but they can be used as a base to build your own Windows containers. That's what we'll do.

We'll create a simple container using the windowsservercore image as a base and running our own batch file.

First create a simple batch file

Create a subfolder: temp. We'll do everything in there (because you seem to get errors if you place a dockerfile in your home directory for some reason)
  • md temp
  • cd temp
Now we'll create a batch job to run in our "BusyBox" style container. Create and open a new file in the temp directory

  • run notepad busyrun.bat
  • You can put anything you like in this batch file but we'll start with a very simple job that loops forever, pinging a nonexistent ip address 
  • just cut and paste the code below into notepad and save it

REM just do something forever
@echo off
goto loop

Building a Docker Container Image

Now we need to build this batch file into a Docker Windows container image. First we need to create a dockerfile by running notepad dockerfile and pasting in the code below (you can change the maintainer to you)

FROM windowsservercore
MAINTAINER me "me@myaddress.com"
# batch job that just runs forever occasionally echoing
ADD busyrun.bat /
ENTRYPOINT ["c:\\busyrun.bat"]

Finally we can run docker build from the temp directory to create an image with busyrun.bat in it that will execute busyrun.bat on creation (note the final full stop in the commands below)
  • docker build -t busyrunning1 .
I need 2 container images with different names so I'll do this twice 
  • docker build -t busyrunning2 .
Check this completed successfully by running docker images again, you should see your new images

You can now run these images using the standard docker run command. It works just like normal docker run, so you can look at the standard docker run reference for ideas.

Before you run this command, run start cmd.exe to start a second command prompt on your host. You'll need it in a few seconds.
  • docker run --name myfirstwindowscontainer -it busyrunning1 cmd
Executing this command will start the container and change your current command prompt to be pointing at the c:\Windows\system32 directory in your new container, where you'll see the output from the busyrun.bat file. 

Below, container running busyrun.bat

You can kill your container from the second (host) command prompt you just created using 
  • docker kill myfirstwindowscontainer
You can also use the second command prompt to run docker ps to see your new container running on your host

Below, using docker ps to view running containers on host and then docker kill to stop one

You can clean up your images using docker rmi. I.e. most of the normal docker management commands will work. Try them out.

You've now created a very simple Windows Docker container, run it and stopped it.

If you want to try more, you can see the Microsoft Docker Windows Containers Guide here

In part 3 we'll look at getting our Microscaling-in-a-box application up and running in this environment

No comments:

Post a comment