在RUN语句

时间:2018-05-31 03:04:53

标签: docker dockerfile

我有以下Dockerfile,它可以工作:

FROM someimage
ARG transform

COPY Web*.config /inetpub/wwwroot/
RUN powershell /Build/Transform.ps1 -xml "/inetpub/wwwroot/web.config" -xdt "/inetpub/wwwroot/web.debug.config"

但是,我想在构建时将web.debug.config文件作为参数传递。所以我把最后一行改为:

RUN powershell /Build/Transform.ps1 -xml "/inetpub/wwwroot/web.config" -xdt "/inetpub/wwwroot/${transform}"

当我这样做时,${transform}参数不会被插值并转换为空字符串。我已经确认transform参数正确传递,因为我能做到:

COPY ${transform} /inetpub/wwwroot/

...并且文件被复制。是否有其他方法使用RUN命令在字符串中插入参数?

我在Windows 10上使用Docker 18.03.1-ce。

3 个答案:

答案 0 :(得分:3)

好吧,我找到了一个有效的解决方案,但它绝对不理想。看来变量根本没有在RUN语句中扩展(至少在Windows上,没有尝试过Linux)。但是,COPY声明将扩大它们。因此,我可以将我的文件复制到具有硬编码名称的临时文件,然后使用:

COPY ./ /inetpub/wwwroot/
COPY ${transform} /inetpub/wwwroot/Web.Current.config
RUN powershell -executionpolicy bypass /Build/Transform.ps1 -xml "/inetpub/wwwroot/web.config" -xdt "/inetpub/wwwroot/Web.Current.config"

在这种特殊情况下,它对我有用。

更新:找到另一种有效的方法

这可能是使用环境变量的更好方法:

FROM someimage
ARG transform
ENV TransformFile ${transform}

COPY ./ /inetpub/wwwroot/
RUN powershell -executionpolicy bypass /Build/Transform.ps1 -xml "/inetpub/wwwroot/web.config" -xdt "/inetpub/wwwroot/$ENV:TransformFile"

在这种情况下,Docker将评估参数transform并将其设置为环境变量TransformFile。当我在PowerShell脚本中使用它时,它不再是Docker评估参数,而是Powershell本身。因此,必须使用用于插值环境变量的Powershell语法。

答案 1 :(得分:0)

shell form of Dockerfile RUN应该允许arg扩展。

检查引号是否与

不相符
RUN powershell ... -xdt "/inetpub/wwwroot/"${transform}

注意:您可以使用$var代替${var}

Microsoft documentation powershell命令中查看

答案 2 :(得分:0)

试试这个 dockerfile 从某些图像 ENV trans transform

COPY $ {trans} / inetpub / wwwroot / RUN powershell /Build/Transform.ps1 -xml" /inetpub/wwwroot/web.config" -xdt" / inetpub / wwwroot / $ {trans}"

运行命令 docker build --build-arg transform =" web.debug.config" -t sample:latest。