title: Variable handling during windows docker build
body: <p>How can i define variables in the <code>Dockerfile</code> for a windows image so they are accessible in subsequent <code>RUN</code> statements?</p>
  
  <p>My test <code>Dockerfile</code> looks like this</p>
  
  <pre><code>FROM microsoft/windowsservercore
  
  SHELL ["powershell", "-Command"]
  
  ARG viaArg=value
  ENV viaEnv="""value"""
  RUN $viaShell=\"value\"
  RUN echo """viaArg: $env:viaArg"""
  RUN echo """viaEnv: $env:viaEnv"""
  RUN echo """viaShell: $myusername"""
  RUN echo """viaShell+env: $env:viaShell"""
  </code></pre>
  
  <p>when building the image via</p>
  
  <blockquote>
    <p>PS > docker build -t myImage .</p>
  </blockquote>
  
  <p>only <code>viaArg</code> and <code>viaEnv</code> show (a) value.</p>
  
  <p>The first example <a href="https://stackoverflow.com/q/45857825/7926064">here</a> puts multiple commands in one line like</p>
  
  <p><code>RUN $someVar=\"2.60.3\" ; echo $someVar</code></p>
  
  <p>but surely there must be a different way.</p>
  
  <p>Is using the <code>$env</code> namespace the only possible way?</p>
  
  <p>How does <a href="https://github.com/moby/moby/issues/22017#issuecomment-345272420" rel="nofollow noreferrer"><code>setx</code></a> take part in all of this?</p>
  
  <p>Note: im not trying to pass <a href="https://stackoverflow.com/q/39978342/7926064">variables from commandline</a>, so <a href="https://stackoverflow.com/a/46495294/7926064"><code>ARG</code></a> may already be used in a wrong manner. And i dont want to <a href="https://stackoverflow.com/a/44925489/7926064">set the variables</a> permanently, they should only exist during the build run, so i dont need them <a href="https://github.com/moby/moby/issues/25988#issuecomment-248436165" rel="nofollow noreferrer">in the registry</a>. </p>
  
  <p>Used versions are</p>
  
  <pre><code>PS &gt; docker -v
  Docker version 17.12.0-ce, build c97c6d6
  PS &gt; [System.Environment]::OSVersion.Version    
  Major  Minor  Build  Revision
  -----  -----  -----  --------
  10     0      14393  0
  PS &gt; (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ReleaseId).ReleaseId
  1607
  </code></pre>