Running PowerShell Core in a docker container

PowerShell core is the edition of PowerShell built on top of .NET Core. It is sometimes simplified to “CoreCLR”, though it technically includes CoreFX as well.

PowerShell Core is cross-platform, available on Windows, macOS, and Linux, thanks to the cross-platform nature of .NET Core. On PowerShell Core, $PSVersionTable.PSEdition is set to Core.

Do note that  while PowerShell Core 6.0 is cross-platform, there is also a PowerShell Core 5.0/5.1 released exclusively as part of Nano Server. In this blog post, we’ll learn how to run PowerShell core in a docker container.

First of all, we need to search for PowerShell core related images that are available. So you can search by https://hub.docker.com or you can also search using the docker command.

Below is the command to use while searching from a bash prompt:

docker search powershell --no-trunc

where –no-trunc specifies that output should not be truncated. It should throw an output like below:

Searching for PowerShell related containers
Searching for PowerShell related containers

Here, we can see that many types of images are available. We would choose to go with the image named microsoft/powershell, because 1) In description we can see that it is officially release by Microsoft itself 2) It has highest number of stars rating.

Now, to pull the specified image, we can use below command:

docker pull microsoft/powershell

We can now see that docker will start pulling all associated intermediate layers and start creating the image:

Pulling PowerShell core image
Pulling PowerShell core image

Once its done, we should be able to see the image in docker images list:

Verifying image is pulled successfully
Verifying image is pulled successfully

Now, we can create a container by using below command:

docker run -it microsoft/powershell:latest powershell
Initiating a container based on powershell core image
Initiating a container based on powershell core image

If we now run command $PSVersionTable as shown above, we can clearly see that PSEdition is set to core. From now on, you can run the regular powershell core cmdlets.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s