我正在构建Windows版Docker桌面版镜像。 我尝试将变量传递给PowerShell命令,但它不起作用。
Dockerfile
# escape=`
FROM microsoft/windowsservercore
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
RUN $someVar="2.60.3" ; echo $someVar
Docker构建
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
预期结果
我可以通过使用环境变量和可能的多阶段构建来解决此问题,以避免保留此变量:
# 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
RUN
命令中处理双引号的方式。请尝试像这样转义它们:\"
。 - Persistent13