2

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 ?

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the answer, did you try to define a script file inside the docker image, because It doesnt work like this for me
changed the answer to add a script and print the env var inside the container. Yes worked
I just found the error, in addition to the answer you gave, it was also the order of the parameters I put : -t the image before the -e values... instead of: -e values and then -t the image. You can edit your answer to solve this :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.