Create PowerShell Core Docker Image in Ubuntu with Az Module Pre-Installed for Azure Operations

Hello everyone, in the last six months I have seen lots of talks regarding administrating the Azure from Ubuntu and many were asking about the steps to create a docker image with PowerShell Core installed and Az module pre-installed in it. So I have created this post for them to go through the below steps. After following these steps you can create a docker image which you can use later and can bundle the required PowerShell day to day operations scripts in it.

To understand this post you should have the basic knowledge of the docker container.

Picture Credit: Pexels.com

And here we start.

Prerequisite: Assuming you already have an Azure VM built with Ubuntu image in your subscription and already have installed the docker in it.

In Ubuntu do the following.

cat > Dockerfile

docker build . -t powershell6-az-jan2020

# Download the Microsoft repository GPG keys

# Register the Microsoft repository GPG keys

sudo dpkg -i packages-microsoft-prod.deb

# Update the list of products

sudo apt-get update

# Enable the “universe” repositories

sudo add-apt-repository universe

# Install PowerShell

sudo apt-get install -y powershell

# Start PowerShell

Pwsh

Here are the steps.

docker run ubuntu

docker run -it ubuntu bash

apt-get install wget

wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb

dpkg -i packages-microsoft-prod.deb

apt-get update

apt-get install software-properties-common

apt-get update

apt-get install -y powershell

A standard image is below.

FROM ubuntu

run bash

run apt-get update

run apt-get install wget -y

run wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb

run dpkg -i packages-microsoft-prod.deb

run apt-get update

run apt-get install software-properties-common -y

run apt-get update

run apt-get install -y powershell -y

run pwsh -c “&{Install-Module -Name Az -AllowClobber -Scope AllUsers -Force}”

You may install .NET Core (If required)

dpkg –purge packages-microsoft-prod && sudo dpkg -i packages-microsoft-prod.deb

apt-get update

apt-get install {the .NET Core package}

For PowerShell 7.0 you can use this.

You can run docker build command next.

You can run a HelloWorld script as shown below once it’s build.

docker run powershell6-az-jan2020 pwsh -c “&{/devps/Helloworld.ps1}”

A Final Image

FROM ubuntu

run bash

run apt-get update

run apt-get install wget -y

run wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb

run dpkg -i packages-microsoft-prod.deb

run apt-get update

run apt-get install software-properties-common -y

run apt-get update

run apt-get install -y powershell -y

run pwsh -c “&{Install-Module -Name Az -AllowClobber -Scope AllUsers -Force}”

run mkdir devps

copy /Documents/Powershell/SQlBackupreport.ps1 /devps/SQlBackupreport.ps1

In the last two lines above you can copy the existing ps1 script to the docker container. That’s all for today. You have a good day ahead.

Now alternately you can also pull the latest PowerShell ready image from Docker and run your scripts. I have published another post for this here.

0.00 avg. rating (0% score) - 0 votes