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. Current version of PowerShell core is 7.1 with 7.2 available under preview. 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 https://hub.docker.com/_/microsoft-powershell or you can also search using the docker command.
There are different images created in order to support the variety of the OS platforms. You can find the appropriate versions as per your underlying OS distribution. The default
latest tag is attributed to ubuntu 18.04 for linux OS and windows server core for Windows OS.
Alternatively, if you are using docker desktop on your machine, you can use docker cli to search for the images suitable for your distribution. Below is the command to use while searching
from a bash prompt with docker cli:
docker search powershell --no-trunc
--no-trunc specifies that output should not be truncated. It should throw an output like below:
mohitgoyal@DESKTOP:~$ docker search powershell --no-trunc NAME DESCRIPTION STARS OFFICIAL AUTOMATED microsoft/powershell PowerShell for every system! 146 [OK] centos/powershell PowerShell is a cross-platform (Windows, Linux and OS X) automation and configuration tool/framework 8 [OK] microsoft/powershell-nightly Nightly builds of PowerShell Core for CI 7 [OK] tylerl0706/powershell-code-server Coder.com's code-server, PowerShell, and the PowerShell extension for vscode 2 scotta01/powershell Powershell on Ubuntu 16.04 2 [OK] pshorg/powershellcommunity PowerShell for every system! Community Images! 2 jess/powershell 2 quickbreach/powershell-ntlm Read more https://blog.quickbreach.io/ps-remote-from-linux-to-windows/ 2 [OK] uleenucks/powershell 1 10thmagnitude/powershell-azure Microsoft's official Docker image of Powershell with the Azure Powershell tools added. 1 [OK] andschwa/powershell PowerShell automated builds. 1 [OK]
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/powershelldocker pull mcr.microsoft.com/powershell:latest
We can now see that docker will start pulling all associated intermediate layers and start creating the image:
mohitgoyal@DESKTOP:~$ docker pull mcr.microsoft.com/powershell:latest latest: Pulling from powershell a70d879fa598: Pull complete c4394a92d1f8: Pull complete 10e6159c56c0: Pull complete 274c216daa46: Pull complete 5ed006f3c539: Pull complete 2e546646cc81: Pull complete Digest: sha256:644c6f2a82c6df1bf30e475994f3a7f97085903e4cb9c8b6727df33b1485627e Status: Downloaded newer image for mcr.microsoft.com/powershell:latest mcr.microsoft.com/powershell:latest
Once its done, we should be able to see the image in docker images list:
mohitgoyal@DESKTOP:~$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE mcr.microsoft.com/powershell latest e94455db57dd 4 weeks ago 329MB
Now, we can create a container by using below command:
docker run -it microsoft/powershell:latest powershelldocker run --name powershell --rm -it mcr.microsoft.com/powershell
mohitgoyal@DESKTOP:~$ docker run --name powershell --rm -it mcr.microsoft.com/powershell PowerShell 7.1.3 Copyright (c) Microsoft Corporation. https://aka.ms/powershell Type 'help' to get help. PS /> Write-Host "Say Hello to PowerShell" Say Hello to PowerShell PS /> exit
As we can see that current version of PowerShell for the image we pulled is 7.1.3. We can now run PS commands as we need.
2 thoughts on “Running PowerShell Core in a docker container”
Instead of this command to run „powershell“ within the container…
docker run -it microsoft/powershell:latest powershell
The latest image is using „pwsh“ so to run powershell use…
docker run -it microsoft/powershell:latest pwsh
Thanks Joe. Post updated to reflect year 2021 !!