如何在Dockerfile中读取Powershell变量? [英] How to read Powershell variable inside Dockerfile?

查看:242
本文介绍了如何在Dockerfile中读取Powershell变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建适用于Windows的Docker桌面映像. 我尝试将变量传递给Powershell命令,但是它不起作用.

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

# 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

正确的结果

我可以通过使用ENV变量和可能的多阶段构建来解决此问题,以避免保留此变量:

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

推荐答案

双引号必须为一样.

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

这篇关于如何在Dockerfile中读取Powershell变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆