Run Azure PowerShell in a Docker Container Image

Run Azure PowerShell in a Docker Container

Yesterday, the Azure PowerShell team announced the Azure PowerShell Docker Container images. In this post, I want to quickly highlight that announcement and show you how you can download, pull, and run Azure PowerShell in a Docker container image from Microsoft.

But first, let’s talk about why you would want to run an Azure PowerShell in a Docker container. Azure is continuously evolving, and the Azure PowerShell team releases a new version of the Azure PowerShell modules every three weeks. This makes it challenging to maintain a production or development environment up to date and ensuring the smooth execution of scripts. With the Azure PowerShell docker container image, you can quickly run scripts against a specific version of Azure PowerShell.

The team highlights the current scenarios:

  • On the same machine, you can run scripts that are using a different version of Az with no conflicts.
  • You can test a script against a different version of Az with no risks.
  • You can run the latest container image interactively.

You can read the full announcement blog here.

How to run Azure PowerShell in a Docker Container

You can run the latest image version of using the following command:

docker run -it mcr.microsoft.com/azure-powershell

You can also run a specific version of Azure PowerShell using the following command:

docker run -it mcr.microsoft.com/azure-powershell:3.6.1-ubuntu-18.04

If you want to just download and pull the images from the official Microsoft Container Registry (MCR) you can just run the docker pull commands.

docker pull mcr.microsoft.com/azure-powershell

Pull a specific version of Azure PowerShell using the following command:

docker pull mcr.microsoft.com/azure-powershell:3.6.1-ubuntu-18.04

Run scripts against different Azure PowerShell versions in Docker Containers

To run a script on your machine inside the container, you can simply mount the volume and run the script. This allows you to run the same script simply against two different versions of Azure PowerShell.

Run Azure PowerShell Script in Docker Container against different versions

Run Azure PowerShell Script in Docker Container against different versions

Run script against version 3.6.1

docker run -it -v C:\Users\thoma\scripts:/scripts mcr.microsoft.com/azure-powershell:3.6.1-ubuntu-18.04 pwsh -file /scripts/myscript.ps1

Run script against version 3.5.0

docker run -it -v C:\Users\thoma\scripts:/scripts mcr.microsoft.com/azure-powershell:3.5.0-ubuntu-18.04 pwsh -file /scripts/myscript.ps1

You can find more about persistent storage in containers on Windows on Microsoft Docs.

Versions and background info

The Azure PowerShell Docker Container is based on different operating systems like Ubuntu 18.04, Debian 9, or CentOS 7 and uses cross-platform PowerShell 7. In my case, I am running these Linux containers on Windows 10 using Docker together with the Windows Subsystem for Linux 2 (WSL 2). Microsoft is also working on adding support for Windows-based container images.

If you want to know more about what’s new in PowerShell 7, check out my blog.

Run Azure PowerShell in a Docker Container Image PowerShell 7

PowerShell and Az module version

The latest container image contains the latest version of PowerShell and the latest Azure PowerShell modules supported with the Az module. You can find more information about the Azure PowerShell module on Microsoft Docs. You can also run the latest version of the Az module in a container in the cloud using Azure Cloud Shell.

You can also access the container running the Visual Studio Code (VS Code) Remote – Containers extension, in a similar way you can use VS Code to connect remotely to a virtual machine using SSH.

Visual Studio Code - Remote Containers Extension

Visual Studio Code – Remote Containers Extension

I hope this was helpful. Let me know if you have any questions in the comments.