1

I cannot get the ARGs in a dockerfile be populated for the life of me. Is this at all supported on the Windows platform?

My Docker file:

>type Dockerfile
FROM microsoft/nanoserver
ARG arg1
RUN echo "${arg1}"

My image build command:

docker image build -t argtest  . --build-arg arg1=ValueForArg1

and the results:

Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM microsoft/nanoserver
 ---> a6688cd24441
Step 2/3 : ARG arg1
 ---> Using cache
 ---> 97770e203c82
Step 3/3 : RUN echo "${arg1}"
 ---> Running in ca6545d4bd0e
"${arg1}"
Removing intermediate container ca6545d4bd0e
 ---> c296d654afef
Successfully built c296d654afef
Successfully tagged argtest:latest
Mike Harris
  • 1,487
  • 13
  • 20
Jeff Saremi
  • 2,674
  • 3
  • 33
  • 57
  • The issue might be that the `echo` command doesn't work in Windows like it does in other OSes, in particular with variables. Try some combination of `%arg1%` in your echo command. See this question: https://stackoverflow.com/questions/10552812/declaring-and-using-a-variable-in-windows-batch-file-bat – Mike Harris Jun 22 '18 at 01:14
  • Mike, could you please paste your comment as an answer? That was the problem not only for the echo command but in general retrieving the value can only be done by %MYARG% in Windows. SOmeone should let the docker team know to correct their documentation. thanks a lot man! – Jeff Saremi Jun 22 '18 at 01:22

2 Answers2

2

The problem is really how Windows represents variables in its scripting/batch language. As a result, the echo command (and any other command) doesn't work in Windows like it does in most other OSes. Since the Dockerfile is run in the host OS (i.e., Windows), not the container, it will use Windows' syntax for commands and variables.

If you use

RUN echo "%arg1%"

(or whatever command you're running), it should work for you.

Mike Harris
  • 1,487
  • 13
  • 20
1

When using powershell in a Dockerfile you will need to use the powershell equivalent of the environment variable notation: $env:variable

# RUN echo "${arg1}"
RUN echo $env:arg1

Using ${arg1} in a powershell will just render an empty string.

myeongkil kim
  • 2,465
  • 4
  • 16
  • 22
DockerUser
  • 11
  • 1