title: In a Windows dockerfile, how can I pass an ARG value to a RUN command?
body: <p>I cannot find any syntax that works. Even suggested solutions on stackoverflow do not work. Running them shows blank output where values are expected.</p>
  <p>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).</p>
  <p>Neither the ARG nor the ENV value seems to be accessible within the RUN command sent to powershell.</p>
  <p>Contents of 'testdockerfile':</p>
  <pre><code>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 [&quot;powershell&quot;,&quot;-Command&quot;]
  
  RUN &quot;\&quot;baseline\&quot; | Out-File c:\debug.txt&quot;
  
  RUN &quot;\&quot;%TEST_ARG%\&quot; | Out-File c:\debug1.txt&quot;
  RUN &quot;\&quot;$TEST_ARG\&quot; | Out-File c:\debug2.txt&quot;
  RUN &quot;\&quot;${TEST_ARG}\&quot; | Out-File c:\debug3.txt&quot;
  RUN &quot;\&quot;${Env:TEST_ARG}\&quot; | Out-File c:\debug4.txt&quot;
  
  RUN &quot;\&quot;%MyEnvVar%\&quot; | Out-File c:\debug5.txt&quot;
  RUN &quot;\&quot;$MyEnvVar\&quot; | Out-File c:\debug6.txt&quot;
  RUN &quot;\&quot;${MyEnvVar}\&quot; | Out-File c:\debug7.txt&quot;
  RUN &quot;\&quot;${Env:MyEnvVar}\&quot; | Out-File c:\debug8.txt&quot;
  RUN &quot;\&quot;$Env:MyEnvVar\&quot; | Out-File c:\debug9.txt&quot;
  
  RUN &quot;\&quot;%MyEnvVar2%\&quot; | Out-File c:\debug10.txt&quot;
  RUN &quot;\&quot;$MyEnvVar2\&quot; | Out-File c:\debug11.txt&quot;
  RUN &quot;\&quot;${MyEnvVar2}\&quot; | Out-File c:\debug12.txt&quot;
  RUN &quot;\&quot;${Env:MyEnvVar2}\&quot; | Out-File c:\debug13.txt&quot;
  RUN &quot;\&quot;$Env:MyEnvVar2\&quot; | Out-File c:\debug14.txt&quot;
  
  RUN &quot;\&quot;%MyEnvVar3%\&quot; | Out-File c:\debug15.txt&quot;
  RUN &quot;\&quot;$MyEnvVar3\&quot; | Out-File c:\debug16.txt&quot;
  RUN &quot;\&quot;${MyEnvVar3}\&quot; | Out-File c:\debug17.txt&quot;
  RUN &quot;\&quot;${Env:MyEnvVar3}\&quot; | Out-File c:\debug18.txt&quot;
  RUN &quot;\&quot;$Env:MyEnvVar3\&quot; | Out-File c:\debug19.txt&quot;
  </code></pre>
  <p>Run the following docker command</p>
  <pre><code>docker build -f testdockerfile --no-cache -t test-app --build-arg &quot;TEST_ARG=value_to_insert_in_debug_txt_file&quot;
  </code></pre>
  <p>Every one of those files is created successfully, but (aside from baseline) they are either empty, or contain one of the supplied literals:</p>
  <pre><code>debug.exe contains &quot;baseline&quot; (expected)
  debug1.txt contains &quot;%TEST_ARG%&quot;
  debug10.txt contains &quot;%MyEnvVar2%&quot;
  debug15.txt contains &quot;%MyEnvVar3%&quot;
  debug18.txt contains &quot;TEST_ARG&quot;
  debug19.txt contains &quot;TEST_ARG&quot;
  debug5.txt contains &quot;%MyEnvVar%&quot;
  all other files exist but are empty
  </code></pre>
  <p>It seems impossible to get any supplied ARG value into a powershell script in the dockerfile.</p>