Another Simple Windows Container Image and Instance Example

Posted by Brian Keyes

Apr 18, 2017 11:09:09 AM Docker, Docker Containers, windows containers


Once we have created the Windows 2016 server, we can logg into the Azure Portal and connect to the instance via RDP.  For our example, we will be creating a folder structure where at the root of C:/, there is a folder called “dockerlabs”. Under the c:/dockerlabs folder, we will also create the folder “env”.In this blog post, we will be outlining the concept “Dockerfile” and create a simple example to highlight the difference between container images and instances.  Of course, before we first need to make sure that we are using a Windows 2016 Server with containers feature enabled.  These are the steps shown in my previous article - "Deploying a Simple Windows Container in Azure".

 

folder structure .jpg

At this point, we change directory to env and open a new text document.  Next, we'll enter the following into the new text document we created:

 

                                                   FROM microsoft/dotnet:nanoserver-core

                                                   COPY get-name.ps1 /

                                                   CMD ["powershell","c://get-name.ps1"]

 

 Save this as “Dockerfile” with no extension (with a capital D).  You will have something that looks like the image below.

 

Docker file contents and folder structure .jpg

 

Now we can create a very simple PowerShell script to get the hostname of the instance. To do this, create a Powershell script called “get-name.ps1” in the C://dockerlabs/env directory.

In order to make sure the 'RUN' command is successful, we need to make sure the folder structure matches what the DockerFile definition is expecting.  This includes making sure the 'get-name.ps1' file is located in the same directory as the Dockerfile and the name matches what is specified.

We'll do a simple example where we will retrieve the name of the container instance.  To do this, the “get-name.ps1” file should have a single command “$env:COMPUTERNAME”.  The figure below shows the folder structure and ps1 file contents.

env computername .jpg

                                             

Now we will build our image, making sure to change directories to the correct directory - 'c:\dockerlabs\env'.  Next, we'll run the command “docker build .” (don’t forget the trailing dot ).  If this is the first time that we've run the build, we'll see some output where docker is downloading the base image of nanoserver-core as shown in the figure below. 

Docker build.jpg

 

Once the image is built, we will see a message that shows the success status as well as the image hash id, as shown below.  

built .jpg

Now you have built the docker image, you can run "docker images" and see the image that was built with our Dockerfile . Note the IMAGE ID it should be the same as what was reported during the successful build. In my example the first 3 letters of my IMAGE ID are "bb9" yours will be a different value .

 

Now we run a container based on that image with the command "docker run bb9" (we only need to reference the first 3 characters of the IMAGE ID ).  

run name .jpg

 

This results in the running of a container instance that was assigned the hostname of 'B4E3E138544D'.   If we run the exact same command, we will get a new instance of the container with a new hostname, as shown in the image below.

run a few times .jpg

We can run the exact same command 3 more times using the same image ID (bb9) but each container name is different .

In this example, we showed how each container instance is a different environment.  The single image that we created using the Dockerfile has instructions  to get the computer name and display it. Each time you run the image, it creates a new unique container and reports it's name to the screen.  This shows how one base image can be used to create multiple container instances.  

Posted by Brian Keyes

Brian has over 20 years of experience as a Systems Engineer with a strong background in VMware, storage, networking and security. Brian has worked for variety of US Govt. entities over the years;DOD,NGA,NNSA and others. Currently focused on Docker technologies

Find me on:

    

Posts by Topic

see all
Request a Complimentary Docker Consultation

An Open View

Vizuri Blog

Subscribing to our blog is a great way to stay up to date with the latest information from Vizuri, as well as our strategic partners. We focus on providing a range of content that is practically useful and relevant from both a technical and business perspective.

We promise to respect your privacy.

×