问题
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
回答1:
Double-quotes need to be escaped for them to work as expected, like so someVar=\"2.60.3\"
.
来源:https://stackoverflow.com/questions/45857825/how-to-read-powershell-variable-inside-dockerfile