3

I'm building Docker Desktop for Windows image. I try to pass a variable to a Powershell command, but it does not work.

Dockerfile

# escape=`
FROM microsoft/windowsservercore

SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

RUN $someVar="2.60.3" ; echo $someVar

Docker build

Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM microsoft/windowsservercore
 ---> 2c42a1b4dea8
Step 2/3 : SHELL powershell -Command $ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';
 ---> Using cache
 ---> ebd40122e316
Step 3/3 : RUN $someVar="2.60.3" ; echo $someVar
 ---> Running in dd28b74bdbda
 ---> 94e17242f6da
Removing intermediate container dd28b74bdbda
Successfully built 94e17242f6da
Successfully tagged secrets:latest

Exprected result

I can workaround this by using ENV variable and, possibly, a multistage build to avoid keeping this variable:

# escape=`
FROM microsoft/windowsservercore

SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

ENV someVar="2.60.3" 
RUN echo $env:someVar

Sending build context to Docker daemon  2.048kB
Step 1/4 : FROM microsoft/windowsservercore
 ---> 2c42a1b4dea8
Step 2/4 : SHELL powershell -Command $ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';
 ---> Using cache
 ---> ebd40122e316
Step 3/4 : ENV someVar "2.60.3"
 ---> Running in 8ac10815ff6d
 ---> 9073ec3256e0
Removing intermediate container 8ac10815ff6d
Step 4/4 : RUN echo $env:someVar
 ---> Running in 43a41df36f92
2.60.3
 ---> 09e48901bea9
Removing intermediate container 43a41df36f92
Successfully built 09e48901bea9
Successfully tagged secrets:latest
2
  • 2
    I could be wrong, but i suspect this is due to how Docker handles double-quotes with the RUN command. Try escaping them like so: \". Commented Aug 31, 2017 at 20:36
  • 1
    @Persistent13 Indeed, that was the case! Can you post this as an answer so I can accept it? Commented Sep 1, 2017 at 8:41

1 Answer 1

7

Double-quotes need to be escaped for them to work as expected, like so someVar=\"2.60.3\".

Sign up to request clarification or add additional context in comments.

Comments

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.