Docker for Windows: ASP.NET site unable to read en

2019-08-01 04:29发布

I have an ASP.NET site running in a windows docker container. I want to be able to read environment variables defined in the Dockerfile. (And when staring the container.)

I am unable to make it work:

Repro:

  • Install and configure software required to use windows containers.

  • Create a new (empty) folder.

  • Create two files (Dockerfile and default.aspx) in the new folder, with the content described bellow.

  • Navigate to the empty folder and run docker build -t test . (This will create a container image with the name test

  • Start powershell in an interactive container: docker run -it --rm -e FROM_COMMAND_LINE="From command line" --entrypoint powershell test

You will now be in a powershell prompt inside the container. You can now verify that the environment variables are NOT available in the web-site by fetching the web-page:

$(wget http://localhost/default.aspx -UseBasicParsing).Content

Extract of the result:

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx GetEnvironmentVariables
COMPUTERNAME - 540CA54CDB93<br>
PUBLIC - C:\Users\Public<br>
LOCALAPPDATA - C:\Windows\system32\config\systemprofile\AppData\Local<br>
...
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx GetEnvironmentVariables - Process
COMPUTERNAME - 540CA54CDB93<br>
PUBLIC - C:\Users\Public<br>
LOCALAPPDATA - C:\Windows\system32\config\systemprofile\AppData\Local<br>
...
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx GetEnvironmentVariables - User
Path - C:\Windows\system32\config\systemprofile\AppData\Local\Microsoft\WindowsApps;<br>
TEMP - C:\Windows\system32\config\systemprofile\AppData\Local\Temp<br>
TMP - C:\Windows\system32\config\systemprofile\AppData\Local\Temp<br>

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx GetEnvironmentVariables - Machine
PROCESSOR_REVISION - 3f02<br>
...

Note that FROM_COMMAND_LINE and FROM_DOCKERFILE are missing.

In the powershell console you can verify that the variables are NOT missing:

PS C:\> $env:FROM_COMMAND_LINE
From command line
PS C:\> $env:FROM_DOCKERFILE
Value from dockerfile

I have also tested to do a iisreset, but it does not change the behaviour.

  1. Have I done something wrong?

  2. Are there other ways to specify environment variables that would make them available to iis inside the container?


Dockerfile:

FROM microsoft/aspnet
ENV FROM_DOCKERFILE Value from dockerfile

ADD default.aspx c:/inetpub/wwwroot/default.aspx

default.aspx:

<% @ Page Language="C#" Trace="false"%>
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx GetEnvironmentVariables
<%
foreach (DictionaryEntry de in Environment.GetEnvironmentVariables()) {
    Response.Write(string.Format("{0} - {1}<br>\n", de.Key, de.Value));
}
%>
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx GetEnvironmentVariables - Process
<%
foreach (DictionaryEntry de in Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Process)) {
    Response.Write(string.Format("{0} - {1}<br>\n", de.Key, de.Value));
}
%>
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx GetEnvironmentVariables - User
<%
foreach (DictionaryEntry de in Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User)) {
    Response.Write(string.Format("{0} - {1}<br>\n", de.Key, de.Value));
}
%>
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx GetEnvironmentVariables - Machine
<%
foreach (DictionaryEntry de in Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Machine)) {
    Response.Write(string.Format("{0} - {1}<br>\n", de.Key, de.Value));
}
%>

Note: I'm currently using Docker version 1.12.2-rc1-beta27.1 (build: 7538) a0eb77a

2条回答
手持菜刀,她持情操
2楼-- · 2019-08-01 04:46

Environment variables passed to docker through -e are set for the process in the container via CreateProcess and lpEnvironment.

They are not set as SYSTEM Variables which would reside in the registry in the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment hive and hence w3wp.exe won't pick up the changes even when you run iisreset.

You have a couple of workaround options here:

  • You could create a SYSTEM variable by running RUN setx /m VAR val in your Dockerfile. This would be picked by your IIS Worker Process after you run iisreset.
  • Create an environment variable only for that AppPool. RUN appcmd.exe set config -section:system.applicationHost/applicationPools /+"[name='DefaultAppPool'].environmentVariables.[name='VAR',value='val']" /commit:apphost.
查看更多
干净又极端
3楼-- · 2019-08-01 04:56

There is a better option available now. Basically if you base your docker on microsoft/aspnet, it will include the latest "ServiceMonitor.exe" which will inject environment variables defined for local process into IIS AppPool. It will be accessible right away without a need to restart IIS process.

There is one hack however, the application pool must be "DefaultAppPool", so you can't create your custom app pools, which should be fine for more deployments.

You can read more about this update here.

查看更多
登录 后发表回答