Running PowerShell Core in a container

Running PowerShell Core in a container

Creating PowerShell modules is fun, rewarding and a great way to learn more PowerShell. But when you want to test your code, you want to run it in a clean environment to make sure that you don’t have anything interfering with your awesome module. It would suck to get a thing working, publish it to powershellgallery and then realize that it won’t run for most people because they don’t have that one unique setting that you use.

Instead of trying to keep your own PowerShell Core clean, it would be preferable to get the newest release and run it in an isolated environment. For this, we can use Docker locally. In this

Setting up docker

We don’t have to create our own Docker image for this. Microsoft has created one all ready, and it’s available on Docker Hub. Let’s start off by pulling the image to our local machine.

docker pull mcr.microsoft.com/powershell

Now that we have the container image, we want to create the container. I prefer to create it with a name that will be easy to remember. An example:

docker run --name pwsh -it mcr.microsoft.com/powershell

This will create the container, with a friendly name of pwsh and open a pseudo-TTY to the container and then you’re in and ready to PowerShell.

This is of course the bare minimum and if you’re running other containers that you want this one to interact with, you might want to read up on docs.docker.com for how to go about setting that up.

Quick tip; If you write exit while in the container, it will actually shut down. That means that if you want to continue to work in this container you have to do a docker start first, then a docker attach. Docker has a shortcut to convert your session from interactive to daemon. The shortcuts are CTRL+PCTRL+Q. Then you can return to it by running docker attach pwsh.

Leave a Comment