Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the ARG instruction of Dockerfile for Windows image

I would like to pass an argument in my dockerfile to build my docker image. I've seen in other post and docker manual how to do this but it doesn't work in my case. Here is an extract of my code where i use my argument:

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

I build my image with this command in powershellPrompt :

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

Finally I have this error :

 '$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

Is someone know what is wrong with my code? Thanks.

like image 368
M.Eva Avatar asked Nov 12 '18 18:11

M.Eva


2 Answers

As @matt9 suggested

Use $env:FirefoxVersion in powershell

Use %FirefoxVersion% in 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%

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

Result

Powershell: 61.0.1
cmd.exe: 61.0.1
like image 180
Andy Joiner Avatar answered Oct 24 '22 01:10

Andy Joiner


(This answer is the formalized version of my comment.)

Try to use %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

Reason:

The error message "The command 'cmd /S /C choco install ...' returned a non-zero code: 1" indicates that the choco install command is executed on cmd.exe (Windows' Command Prompt). Dockerfile's ARG value can be treated as an environment variable. On cmd.exe, %...% stands for env var.

like image 5
matt9 Avatar answered Oct 23 '22 23:10

matt9