Windows镜像如何使用Dockerfile的ARG指令
How to use the ARG instruction of Dockerfile for Windows image
我想在我的 docker 文件中传递一个参数来构建我的 docker 图像。我在其他 post 和 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
有人知道我的代码有什么问题吗?
谢谢
这样试试:
传递参数,例如 ${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 .
(这个答案是我的的形式化版本。)
尝试使用 %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
原因:
错误信息"The command 'cmd /S /C choco install ...' returned a non-zero code: 1"表示choco install
命令是在cmd.exe上执行的(Windows'命令提示符) . Dockerfile 的 ARG
值可以被视为环境变量。在 cmd.exe 上,%...%
代表环境变量。
正如@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
补充一下@Andy Joiner 的回答:
我无法让 $env:<arg name>
工作,关键是移动 ARG
声明 AFTER SHELL
命令。
我想在我的 docker 文件中传递一个参数来构建我的 docker 图像。我在其他 post 和 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
有人知道我的代码有什么问题吗? 谢谢
这样试试:
传递参数,例如 ${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 .
(这个答案是我的
尝试使用 %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
原因:
错误信息"The command 'cmd /S /C choco install ...' returned a non-zero code: 1"表示choco install
命令是在cmd.exe上执行的(Windows'命令提示符) . Dockerfile 的 ARG
值可以被视为环境变量。在 cmd.exe 上,%...%
代表环境变量。
正如@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
补充一下@Andy Joiner 的回答:
我无法让 $env:<arg name>
工作,关键是移动 ARG
声明 AFTER SHELL
命令。