Containers for ITPros – PowerShell and Dockerfile

This post has been republished via RSS; it originally appeared at: Containers articles.

Hello folks,

Before we jump into the content, I wanted to take a moment to explain this blog series: As container's popularity continues to increase, I see more and more ITPros (particularly Microsoft focused ITPros) struggle to use it and even understand how some settings and configurations work for containers. This happens either because many container-focused tech was created with developers in mind or because the documentation is not exactly focused on Windows scenarios. With that in mind, I wanted to use this channel to share my learning on using Containers. Here I'll be sharing tips and tricks for Microsoft ITPros using containers, from small things to deep dives... My hope is that this helps more and more people start their journey with containers!

 

If you have no idea on what containers are, fear not! We have a great set of documentation to help you get started. Later this year, we'll also be sharing a few things from our Ignite content here. Stay tuned!

Let's get to it!

 

PowerShell and Dockerfile

When creating a new container image, you'll probably be using a dockerfile file. A dockerfile is a declarative way to tell docker how to prepare the container image you want to create. What you have to do is include all the instructions in the file and docker does the rest.

One of the best practices to create a docker file is to create a new containers, run the commands you want to use to prepare the container image and then use these commands in your dockerfile. That reduces the chances of errors when creating your dockerfile.

To run a new container based on, let's say, the Server Core base container image you can use:

 

docker run --entrypoint powershell -it --name testcontainer mcr.microsoft.com/windows/servercore:ltsc2019

 

We'll save the details on the command above for another blog post. For now, you need to know that this command will create a new container based off the Server Core base container image and will open an interactive PowerShell session. This way you can run all the commands you want to validate if 1) they run as expected and 2) prove that you can run a series of commands to prepare the image the way you want.

 

Escaping double quotes

One of the issues in doing the process above, is that on PowerShell you'll use double quotes " to indicate a value that has spaces in between the words. For example, if you're managing an IIS website via PowerShell, you'll use:

 

New-WebApplication "TestWebApp" -Site "Default Web Site" -ApplicationPool "DefaultAppPool" -PhysicalPath "C:\TestWebApp"

 

However, when you transfer this to the dockerfile, you need to change the double quotes "Default Web Site" to single quotes 'Default Web Site'. Otherwise, the docker build command will exit with an error saying Web Site" is not a recognized command.

New-WebApplication "TestWebApp" -Site 'Default Web Site' -ApplicationPool "DefaultAppPool" -PhysicalPath "C:\TestWebApp"

 

I confess I had to dedicate some time to understand what was happening when I did this the first time. The problem is that everything works fine on PowerShell, even inside an interactive session in the container, but fails on the dockerfile when docker build is executed. In fact, Docker documented it here.

 

I hope this helps you folks out there trying containers for the first time! I have much more to share so stay tuned!

Leave a Reply

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

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.