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
Community
  • 1
  • 1
Skyblade
  • 2,354
  • 4
  • 25
  • 44
  • 2
    I could be wrong, but i suspect this is due to how Docker [handles double-quotes](https://github.com/StefanScherer/dockerfiles-windows/tree/master/quotes) with the `RUN` command. Try escaping them like so: `\"`. – Persistent13 Aug 31 '17 at 20:36
  • 1
    @Persistent13 Indeed, that was the case! Can you post this as an answer so I can accept it? – Skyblade Sep 01 '17 at 08:41

1 Answers1

7

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

Persistent13
  • 1,522
  • 11
  • 20