首页 > 解决方案 > 如何使用 Dockerfile for Windows 镜像的 ARG 指令

问题描述

我想在我的 dockerfile 中传递一个参数来构建我的 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

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

标签: dockerdockerfiledocker-for-windows

解决方案


正如@matt9 建议的那样

$env:FirefoxVersion在 powershell 中使用

%FirefoxVersion%在 cmd.exe 中使用

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

推荐阅读