Home > Software design >  Pass env variable from Powershell to a Powershell script inside Docker image
Pass env variable from Powershell to a Powershell script inside Docker image

Time:12-24

I want to pass env variables from my Powershell on Windows to a Powershell script inside my Docker Image.

From my Windows Powershell I defined them like this:

$env:MY_VAR = 'test'
$env:MY_VAR2 = 'test2'

Then in my Docker image I have this:

FROM  mcr.microsoft.com/windows/servercore:ltsc2019

# some other stuff...

ADD run.ps1 /

ENTRYPOINT ["powershell", "/run.ps1"]

And my run.ps1 script is just:

gci env:* | sort-object name

echo $env:MY_VAR
echo $env:MY_VAR2

I tried something like this:

docker run -t my-image:latest -e $env:MY_VAR=$env:MY_VAR -e $env:MY_VAR2=$env:MY_VAR2

or:

docker run -t my-image:latest -e MY_VAR=$env:MY_VAR -e MY_VAR2=$env:MY_VAR2

My env variables are always empty, I don't find the right syntax where is the mistake ?

CodePudding user response:

Set your -e parameters before the -t in your docker command.

Here is an example:

On my local powershell:

$env:SqlUser
me   

The docker command:

docker run -e SqlUser="$env:SqlUser" -it mcr.microsoft.com/powershell pwsh
PowerShell 7.2.7
Copyright (c) Microsoft Corporation.

https://aka.ms/powershell
Type 'help' to get help.

PS /> $env:SqlUser
me
PS /> New-Item -Type File test.ps1
PS /> Add-Content ./test.ps1 -Value 'Write-Host "$env:SqlUser"'
PS /> ./test.ps1
me
  • Related