如何使用Dockerfile的ARG指令获取Windows映像

时间:2018-11-12 18:33:22

标签: docker dockerfile docker-for-windows

我想在dockerfile中传递一个参数来构建我的docker镜像。我已经在其他帖子和Docker手册中看到了如何执行此操作,但在我的情况下不起作用。 这是我使用参数的代码摘录:

ARG FirefoxVersion
RUN powershell -Command iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'));
RUN choco install -y firefox --version $FirefoxVersion --ignore-checksums

我使用powershellPrompt中的此命令构建映像:

docker build -t myimage --build-arg FirefoxVersion=61.0.1 .

最后我遇到这个错误:

 '$FirefoxVersion' is not a valid version string.
 Parameter name: version
 The command 'cmd /S /C choco install -y firefox --version $FirefoxVersion  -- ignore-checksums' returned a non-zero code: 1

有人知道我的代码有什么问题吗? 谢谢。

3 个答案:

答案 0 :(得分:1)

按照@ matt9的建议

在Powershell中使用$env:FirefoxVersion

在cmd.exe中使用%FirefoxVersion%

FROM microsoft/windowsservercore:ltsc2016
ARG FirefoxVersion
#if using powershell
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop';"]
RUN Write-Host Powershell: $env:FirefoxVersion
#if using CMD
SHELL ["cmd", "/S", "/C"]
RUN echo cmd.exe: %FirefoxVersion%

内部版本:docker build -t myimage --build-arg FirefoxVersion=61.0.1 .

结果

Powershell: 61.0.1
cmd.exe: 61.0.1

答案 1 :(得分:0)

尝试这样:

传递诸如 $ {FirefoxVersion}

之类的参数
ARG FirefoxVersion
RUN powershell -Command iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'));
RUN choco install -y firefox --version ${FirefoxVersion} --ignore-checksums

构建docker映像:

docker build -t --build-arg FirefoxVersion=61.0.1 myimage .

答案 2 :(得分:0)

(此答案是我的comment的正式版本。)

尝试使用%FirefoxVersion%

ARG FirefoxVersion
RUN powershell -Command iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'));
RUN choco install -y firefox --version %FirefoxVersion% --ignore-checksums

原因:

错误消息“命令'cmd / S / C choco install ...'返回的非零代码:1” 指示choco install命令在cmd上执行.exe(Windows命令提示符)。 Dockerfile的ARG值可以视为环境变量。在cmd.exe上,%...%代表环境变量。