5

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

FROM mcr.microsoft.com/dotnet/framework/aspnet:4.7.2-windowsservercore-ltsc2016

ENV MyEnvVar ${TEST_ARG}
ENV MyEnvVar2 $TEST_ARG
ENV MyEnvVar3 TEST_ARG

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.

Triynko
  • 18,766
  • 21
  • 107
  • 173
  • Running docker version 3.6.0 (67351) with engine 20.10.8. – Triynko Aug 30 '21 at 21:50
  • Wow, ok. This seems to be a sort of "usability nightmare" as all ARG values are reset/cleared after the FROM. You have to re-declare them after the FROM to keep them accessible in the same dockerfile. This is insane: https://github.com/moby/moby/issues/34129#issuecomment-315856425 – Triynko Aug 30 '21 at 22:33

1 Answers1

4

ARG values that appear before the FROM directive are not available after the FROM directive.

ARG values must be redeclared after the FROM directive to make them available after the FROM directive.

ARG TEST_ARG=value_to_insert_in_debug_txt_file

FROM mcr.microsoft.com/dotnet/framework/aspnet:4.7.2-windowsservercore-ltsc2016

ARG TEST_ARG=value_to_insert_in_debug_txt_file

ENV MyEnvVar ${TEST_ARG}
ENV MyEnvVar2 $TEST_ARG
ENV MyEnvVar3 TEST_ARG
...
Triynko
  • 18,766
  • 21
  • 107
  • 173
  • Also beware that comments (starting with '#') have to be on their own line. You cannot place a comment at the end of an ARG or any other command: https://stackoverflow.com/a/42123446/88409 – Triynko Sep 15 '21 at 15:02