Run a PowerShell script with parameters passed from docker run command

I’m trying to create a docker container that launches PowerShell and runs a .ps1 script which requires parameters. I changed my script to use environment variables $env:xxx, however I cannot figure out how to update the DockerFile so that these parameters can be passed in when the container is launched.

My DockerFile is as follows:

FROM microsoft/windowsservercore
MAINTAINER myself

COPY MyScript.ps1 c:/MyFolder/
COPY Folder1 c:/MyFolder/Folder1
COPY Folder2 c:/MyFolder/Folder2
COPY TestApp c:/MyFolder/TestApp

ENV Parameter1=${parameter1}

ENV Parameter2=${parameter2}

CMD powershell c:/MyFolder/MyScript.ps1

Build and Run commands are as follows:

docker build --tag mycontainer . --build-arg Parameter1 --build-arg Parameter2
docker run -it mycontainer -e Parameter1=‘testp1’ -e Parameter2=‘testp2’

I’ve tried several variations from different posts online.

Any assistance would be greatly appreciated.

Thank you

I had the same issue as you, though I simply put ENV Parameter1=“changeme” in the dockerfile, then in my ‘docker run’, I put the -e Parameter1=‘test1’ BEFORE ‘mycontainer’ and it worked.

I’d also run the powershell script from the ‘docker run’ line or ‘RUN powershell /path/to/script’ instead of CMD (not sure if makes a difference, I didn’t test that)

1 Like