Running a PowerShell script in Dockerfile

Recently we needed to create a Docker image that provided multiple command line tools as part of our CI/CD pipeline. As part of our pipeline we use PowerShell Core to run scripts, triggering changes within our AWS infrastructure, this means installing the AWS SDK module into the Docker image so we don’t have to download it on every single use. Fortunately this is very simple:

ADD setup.ps1 /build/setup.ps1
RUN pwsh -File ‘/build/setup.ps1’ && rm -rf /build

And that’s it for the Dockerfile!  
The setup script (setup.ps1) is very basic, but can be customised to perform any kind of initial setup you require:

Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
Install-Module -Name AWSPowerShell.NetCore -Scope AllUsers 
-Confirm:$False -AcceptLicense

We can then run an interactive shell in the image to verify:

PS /> Import-Module -Name AWSPowerShell.NetCore
PS /> Get-Module
ModuleType VersionPreRelease NameExportedCommands
Binary{Add-AASScalableTarget, Add-ACMCertificateTag, Ad…
Script 0.0ImportGuard{Add-Member, Add-Type, Clear-Variable, Compare-Ob…
Script 2.1.0PSReadLine{Get-PSReadLineKeyHandler, Get-PSReadLineOption, …

You should now have a Docker image you can perform AWS commands on without having to pull down the PS Module.