Getting Started with Containers (Part 6)

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

In the previous article in this series, I showed you how you could install some container images, and then apply some tags to those images. In this article, I want to continue the discussion by showing you how to create a Docker container.

As you may recall, I created two different Docker images in the previous article. One of those images was a Nano server image, while the other was a Windows Server Core OS image. However, these aren’t the only base images that you can install. You can build your own base images from either of the images that we deployed in the previous article, or you can download some pre-built images from Microsoft.

If you want to check out Microsoft’s container image library, then all you have to do is to open PowerShell and enter the following command:

Docker search microsoft

As you can see in Figure A, Microsoft provides a number of images that you can download. Some of these images are based on Nano server, while others are based on Windows server core.

Figure A: Microsoft provides an extensive image library.

For the sake of demonstration, suppose that after looking at the list you decide that you want to download the MySQL server image that is based on Nano server. You can easily do so by using the Docker pull command, followed by the name of the image that you want to install. In this case, the command would be:

Docker pull microsoft/sample-mysql:nanoserver

You can see what this process looks like in Figure B.

Figure B: You can use the Docker pull command to download images from Microsoft.

Once the download completes, you can verify that the image has been added to your server by using the Docker Images command, as shown in Figure C.

Figure C: The new image has been installed onto my Docker server.

Of course making a base image available for use on your server is really only a starting point. The real goal is to create a container that is based on an image. Fortunately, it’s relatively easy to create Docker containers.

The command that is used to deploy a Docker container is Docker Run. When issuing this command, there are a few parameters that you are going to need to use.

The first parameter is -d. The -d parameter runs the command in detached mode. Detached mode is typically used when running commands manually, as we are in this instance.

Another parameter that we might use (depending on the type of container that we are creating) is -p. The -p parameter is the Ports parameter. It is used for mapping a container’s ports to the host’s port. In the case of a Web server for example, you might use -p 80:80 to map the container’s port 80 to the host’s port 80.

The -t parameter indicates that you want to use TTY. This concept isn’t super well documented, but essentially, it causes a command’s output to be logged. I will show you an example of this later on.

So with that said, let’s go ahead and create a server core container. You can do so by using this command:

Docker run -d -p 80:80 windowsservercore ping -t localhost

You probably don’t actually need the -p switch in this case, but I went ahead and used it anyway so that you could see how it works.

The -ping -t localhost portion of the command reflects a command that will be run inside of out container. This will cause the container to continuously ping itself, and the -t parameter will cause the ping output to be written to a log file.

When you create a container, Docker provides a really large hexadecimal number that acts as the container ID. You can see an example of this in Figure D.

Figure D: This is how you create a Docker container.

Now that a container has been created, let’s take a look at the container. To do so, type the following command:

Docker ps

As you can see in Figure E, this command shows you a portion of the container ID, the container image, and some other information.

Figure E: The Docker ps command displays the containers that currently exist.

Now that I have created a container, I want to backtrack just a bit and talk a little more about the -t parameter that I mentioned earlier. If you look at the command that I used to create the container, you will notice that -t was specified in the middle of a ping command. The command was:

Ping -t localhost

I mentioned that -t was used for logging, but you can think of it as being a mechanism for redirecting output. In this case, I appended -t to the ping command as a way of redirecting Ping’s output to a log file.

So how do we access the log file? In order to do so, you will need to know the first few characters of the container ID. In this case, the first few characters of my container ID are: 394f7. As such, I can view the log file by using this command:

Docker logs 394f7

You can see the log output in Figure F.

Figure F: You can use the Docker logs command to display the log contents.

So what if you want to see a live view of the container instead of just looking at the logs? To do so, simply type Docker attach, followed by the first few characters of the container ID. You can see what this looks like in Figure G.

Figure G: I am now attached to the container.

In a static screen capture, this does not really look any different than when we were examining the log files. However, this is a live view of the container and the console displays pings in real time.

My plan for the next article in this series is to show you some more things that you can do with containers. For right now though, I want to clean up my mess by getting rid of the sample container that I created. Normally, the first step in doing so is to stop the container. To do so, enter the following command:

Docker stop 394f7

You would of course substitute your own container ID for 394f7. Once the command completes, you can use the Docker ps command to check the container. If you look at Figure H however, you can see that the Docker stop command didn’t just stop the container, it removed it. The reason for this is that we created the container using the -d switch, which creates a detached container. The container was removed automatically as soon as the process stopped.

Figure H: Stopping the container causes it to be removed.


In this article, I have shown you an example of a Docker container. In the next article, I want to continue the discussion by showing you how to make a container do something more useful.

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

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top