title: Volume mount and environment variable access for a Window service running in a container in kubernetes
body: <p>An application that runs as a Windows service in a Windows container (without any overrides for run-as user - <code>New-Service -Name x -BinaryPath someservice.exe</code> step in the container build) is expecting an environment variable set on the container as well as read a file mounted in the container.  I know that I can run applications directly via the entrypoint and they are able to read the env variable and file from mount, but as a service I am getting errors indicating it isn't.</p>
  <p>Are environment variables scoped to user by default, would something like a RunAs configuration in security context be needed, or some other mechanism? Or would there be any limitations on access to file mounts by the service?</p>
  <p><strong>edit</strong>
  Investigate the environment variables a bit more, seems like this might be the part that's missing. I tried to echo a var based on specific scopes:</p>
  <pre><code>PS C:\dir&gt; echo ([System.Environment]::GetEnvironmentVariable(&quot;varname&quot;,&quot;User&quot;))
  PS C:\dir&gt; echo ([System.Environment]::GetEnvironmentVariable(&quot;varname&quot;,&quot;Machine&quot;))
  PS C:\dir&gt; echo ([System.Environment]::GetEnvironmentVariable(&quot;varname&quot;,&quot;Process&quot;))
  expected_value
  </code></pre>
  <p>So I suspect the service doesn't have access to the process. Am going to try rescoping the variable:
  <code>[System.Environment]::SetEnvironmentVariable('varname',$env:varname,[System.EnvironmentVariableTarget]::Machine)</code></p>