Getting Started with Containers (Part 7)

by [Published on 8 Sept. 2016 / Last Updated on 8 Sept. 2016]

This article continues the discussion by explaining how to build a container that can host a Web site.

If you would like to read the other parts in this article series please go to:

So far in this article series, I have shown you how to configure Windows Server 2016 to use containers, and I have shown you how to create a simple container. Even so, we have yet to make our container do anything useful. I want to show you how to put a container to use for a practical purpose, but before I get to that, I need to answer one fundamental question. How do you interact with containers?

In a Windows environment, you will need to use PowerShell to interact with a container. So with that said, let’s work through a simple exercise that will illustrate how this process works.

As you may recall, we previously created two different container images – a Server Core image and a Nano Server image. Neither of these images really do anything by themselves, we are simply using them as base images. My goal for this article is to show you how to create an IIS container using the Server Core base image. Upon doing so, we will interact with create a Website within the container.

So the first thing that we need to do is to create a new image that is based on the Windows Server Core image. This can be accomplished by using the following commands:

Docker run -it windowsservercore cmd

exit

Docker ps -a

Docker commit <container name> my-iis

There are a few things that you need to know about the commands shown above. First off, the first command references windowsservercore. This was the name of my Windows Server Core image. If your image is called something else, then you will need to use the alternative image name.

At this point, Docker will create a Windows Server Core container and will open a command prompt window. The second command, exit, closes the command prompt, but leaves the container running. It is worth noting that you may have to navigate back to the C:\Program Files\Docker container before continuing on.

The third line of code lists all of the currently existing containers. For each container, Windows lists a container name. Make note of the Windows Server Core container name, and then enter it into the fourth command. You will also notice that the fourth command ends with my-iis. My-iis is the name that I am assigning to the container that I am creating, and this name should be entered in lower case.

When you are done, enter the Docker images command to confirm the existence of the my-iis image, as shown below.

Image
You should now have an image called my-iis.

So now we have an image called my-iis, but this image really isn’t any different from the windowsservercore image. It’s time to change that. We need to modify the image to include IIS. To do so, enter the following commands:

Docker run -it my-iis cmd

Powershell.exe

Import-module ServerManager

Add-WindowsFeature Web-Server

This block of code starts out by launching an interactive container based on the my-iis image, and then launches the Command Prompt within the container. The next line of code launches PowerShell. Again, we are still operating within the container. Finally, we are importing the ServerManager module for PowerShell, and using it to install Windows Server’s Web Server feature. You can see all of this in action in the figure below.

Image

We are obviously making progress, but a default, out of the box, Website doesn’t really do us much good. That being the case, let’s create a custom site. The first thing that we will need to do is to determine the default Web page. The easiest way to find out the default document names that IIS is configured to use is to enter the following command:

Get-WebConfigurationProperty -Filter /system.webserver/defaultdocument/files/add -pspath "IIS:\sites\Default Web Site" -name value | Select Value

This command returns a list of default document names, as shown in the figure below:

Image
These are the default document names.

The IIS default Website is located at C:\inetpub\wwwroot. If you navigate to this folder and enter the Get-ChildItem cmdlet, you will see the code that makes up the default Website. By default there are two files, iisstart.htm and iisstart,png. The iisstart.htm file is included on the list of default documents, and makes up the default Web page. The iisstart,png file contains an image that is displayed on the default Website. You can see these files in the figure below.

Image
The C:\inetpub\wwwroot folder contains the default Website.

For the sake of demonstration, go ahead and erase both of these files by using the erase *.* command. If this were a production system, we would now add our Web app code to the folder. Since this is simply a demo however, we will create a very simple Web page that displays the text Hello World. To do so, enter the following command:

New-Item -path c:\inetpub\wwwroot\iisstart.htm -value "Hello World!" -itemtype file

The figure below shows the procedure that I have used to create a new iisstart.htm file, verify the file’s existence, and display the file’s contents.

Image
I have created a new iisstart.htm file

Now we need to commit the change to our image. To do so, enter the following commands:

Exit

Exit

Docker ps -a

Docker commit <container name> my-iis

These commands will permanently update the my-iis image. Remember, before we started, the image did not even include IIS. Now the image has IIS installed, and we also added some custom HTML code. You can see what this process looks like in the figure below:

Image
I have committed the changes to the my-iis image.

The next thing that I want to do is to clear the list of containers. I am only doing this as a way of cleaning up after myself and making things less messy. To remove all of the containers that are not currently running, enter the following command:

Docker rm $(docker ps -a -q)

Finally, let’s create a container from the my-iis image, and see if we can access the Web page within it. To create the container, enter the following command:

docker run -d -p 80:80 my-iis ping -t localhost

This command, which you can see below, maps the host’s port 80 to the container’s port 80. It also runs a continuous ping as a way of keeping the container running.

Image
I have created a Docker container based on the my-iis image.

When you have finished with the container, you can remove it by typing the following command:

Docker rm -f <container name>

In this article, I have shown you how to use Docker to create a Windows Server container that runs IIS and hosts a Website. In the next article in this series, I plan to conclude the discussion by showing you some more techniques for managing containers and the applications running within them.

If you would like to read the other parts in this article series please go to:

See Also


The Author — Brien M. Posey

Brien M. Posey avatar

Brien Posey is an MCSE and has won the Microsoft MVP award for the last few years. Brien has written well over 4,000 technical articles and written or contributed material to 27 books.

Advertisement

Featured Links