In a Windows dockerfile, how can I pass an ARG value to a RUN command?

  args, docker, environment-variables, windows

I cannot find any syntax that works. Even suggested solutions on stackoverflow do not work. Running them shows blank output where values are expected.

ENV variables are not persisted in the built image (i.e. running ‘set’ from the command line in the running container shows that the variable is not set).

Neither the ARG nor the ENV value seems to be accessible within the RUN command sent to powershell.

Contents of ‘testdockerfile’:

ARG TEST_ARG=value_to_insert_in_debug_txt_file



SHELL ["powershell","-Command"]

RUN ""baseline" | Out-File c:debug.txt"

RUN ""%TEST_ARG%" | Out-File c:debug1.txt"
RUN ""$TEST_ARG" | Out-File c:debug2.txt"
RUN ""${TEST_ARG}" | Out-File c:debug3.txt"
RUN ""${Env:TEST_ARG}" | Out-File c:debug4.txt"

RUN ""%MyEnvVar%" | Out-File c:debug5.txt"
RUN ""$MyEnvVar" | Out-File c:debug6.txt"
RUN ""${MyEnvVar}" | Out-File c:debug7.txt"
RUN ""${Env:MyEnvVar}" | Out-File c:debug8.txt"
RUN ""$Env:MyEnvVar" | Out-File c:debug9.txt"

RUN ""%MyEnvVar2%" | Out-File c:debug10.txt"
RUN ""$MyEnvVar2" | Out-File c:debug11.txt"
RUN ""${MyEnvVar2}" | Out-File c:debug12.txt"
RUN ""${Env:MyEnvVar2}" | Out-File c:debug13.txt"
RUN ""$Env:MyEnvVar2" | Out-File c:debug14.txt"

RUN ""%MyEnvVar3%" | Out-File c:debug15.txt"
RUN ""$MyEnvVar3" | Out-File c:debug16.txt"
RUN ""${MyEnvVar3}" | Out-File c:debug17.txt"
RUN ""${Env:MyEnvVar3}" | Out-File c:debug18.txt"
RUN ""$Env:MyEnvVar3" | Out-File c:debug19.txt"

Run the following docker command

docker build -f testdockerfile --no-cache -t test-app --build-arg "TEST_ARG=value_to_insert_in_debug_txt_file"

Every one of those files is created successfully, but (aside from baseline) they are either empty, or contain one of the supplied literals:

debug.exe contains "baseline" (expected)
debug1.txt contains "%TEST_ARG%"
debug10.txt contains "%MyEnvVar2%"
debug15.txt contains "%MyEnvVar3%"
debug18.txt contains "TEST_ARG"
debug19.txt contains "TEST_ARG"
debug5.txt contains "%MyEnvVar%"
all other files exist but are empty

It seems impossible to get any supplied ARG value into a powershell script in the dockerfile.

Source: Windows Questions