Robin
Robin

Reputation: 521

Run Azure PowerShell script in Docker container

I would like to run an Azure-PowerShell script in a Docker container. As Docker is quite new to me, maybe I just do not understand some concepts correctly.

As far as I understand, the Dockerfile below is based on Linux, and has PowerShell and Azure PowerShell installed?

FROM mcr.microsoft.com/azure-powershell:latest
COPY . /app
SHELL ["cmd", "/S", "/C"]    
RUN "& ./app/WriteHostTest.ps1" #For testing purposes I just want to fire a really simple .ps1 file.

I tried various modifications, but currently I get this error:

container_linux.go:349: starting container process caused "exec: \"cmd\": executable file not found in $PATH"

In a "Windows container" it is working, however, here I just cannot manage to install the Az PowerShell module. I also tried a lot of modifications, but basically:

The Dockerfile:

## Create first layer: Windows image
FROM mcr.microsoft.com/windows/servercore:ltsc2019
LABEL Robin Bette

## Create second layer: copy everything from the CURRENT DIRECTORY to /app location
COPY . /app

## Download the tools 
SHELL ["cmd", "/S", "/C"]
ADD "https://dist.nuget.org/win-x86-commandline/v5.8.0/nuget.exe" "C:\TEMP\nuget.exe"

## Install NuGet
RUN "C:\TEMP\nuget.exe" install NuGet.Build -Version 2.12.1

## Install PowerShellGet
SHELL ["powershell", "-ExecutionPolicy", "Bypass", "-Command"]
RUN Install-Module -Name PowerShellGet -RequiredVersion 2.2.1 -Force

# Install Azure PowerShell
RUN Install-Module -Name Az -Force

## Run the PowerShell script
RUN PowerShell ./app/Az-Script.ps1


## Stuff I tried:
# Install PS7 (did not help)
# RUN iex "& { $(irm https://aka.ms/install-powershell.ps1) } -UseMSI" => Error on the ampersand
#ADD "https://github.com/PowerShell/PowerShell/releases/download/v7.1.0/PowerShell-7.1.0-win-x64.msi" "C:\TEMP\PowerShell-7.1.0-win-x64.msi"
#RUN "C:\TEMP\PowerShell-7.1.0-win-x64.msi"

#RUN Install-PackageProvider -Name NuGet -Force
#RUN Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://aka.ms/install-powershell.ps1'))

Install-PackageProvider : No match was found for the specified search criteria
for the provider 'NuGet'. The package provider requires 'PackageManagement'
and 'Provider' tags. Please check if the specified package has the tags.
Exception calling "ShouldContinue" with "2" argument(s): "Object reference not set to an instance of an object."

Here, I just keep getting the error (for which I tried also various solutions like updating PowerShell to version 7):

Exception calling "ShouldContinue" with "2" argument(s): "Object reference not set to an instance of an object." At...\PowerShellGet\1.0.0.1\...

Any thoughts? Thanks in advance!

Upvotes: 1

Views: 2234

Answers (1)

PMental
PMental

Reputation: 1188

For the Linux container the issue is that you're trying to start cmd.exe, which is the Windows command line interpreter and doesn't exist in Linux. Try using pwsh instead (without options).

So the SHELL line should look something like:

SHELL ["pwsh"]

instead.

Upvotes: 1

Related Questions