<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>

Brett
<p>The issue was related due to environment variable scoping - the environment variables passed to the container are <code>Process</code> scoped. However, if a Windows service is started in the container, it does not have access to the scope (the entrypoint process does).</p>
<p>In order to resolve this, I set entrypoint to a powershell wrapper for the app that copied the Process scope environment variable to the Machine scope before starting the Windows service:</p>
<pre><code>[System.Environment]::SetEnvironmentVariable('varname',$env:varname,[System.EnvironmentVariableTarget]::Machine)
# start service command.
</code></pre>
