Windows的Docker:ASP.NET站点无法读取Dockerfile中定义的环境变量 [英] Docker for Windows: ASP.NET site unable to read environment variables defined in Dockerfile

查看:96
本文介绍了Windows的Docker:ASP.NET站点无法读取Dockerfile中定义的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows docker容器中运行一个ASP.NET站点。
我希望能够读取Dockerfile中定义的环境变量。 (并且在凝视容器时。)

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

我无法使其工作:

复制:


  • 安装和配置使用Windows容器所需的软件。

  • Install and configure software required to use windows containers.

创建一个新的(空)文件夹。

Create a new (empty) folder.

在新文件夹中创建两个文件(Dockerfile和default.aspx),其内容描述如下。

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

导航到空文件夹并运行 docker build -t test。(这将创建一个名称为test

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

在交互式容器中启动powershell: docker run -it --rm -e FROM_COMMAND_LINE =来自命令行 --entrypoint powershell测试

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

您现在将在容器内的powershell提示符中,现在可以验证环境变量不能通过获取网页在网站上找到该网址:

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

结果提取:

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

请注意,缺少FROM_COMMAND_LINE和FROM_DOCKERFILE。

Note that FROM_COMMAND_LINE and FROM_DOCKERFILE are missing.

在Powershell控制台中,您可以验证变量是否不丢失:

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

我也测试过进行iisreset,但是

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


  1. 我做错了什么吗?

  1. Have I done something wrong?

是否还有其他方法可以指定
环境变量,以使其可用于容器中$ ii $? / li>

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






Dockerfile:


Dockerfile:

FROM microsoft/aspnet
ENV FROM_DOCKERFILE Value from dockerfile

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

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));
}
%>

注意:我当前使用的是Docker 1.12.2-rc1-beta27.1版(内部版本: 7538)a0eb77a

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

推荐答案

现在有更好的选择。基本上,如果您将docker基于 microsoft / aspnet ,它将包括最新的 ServiceMonitor.exe,它将为本地进程定义的环境变量注入IIS AppPool。可以立即访问,而无需重新启动IIS进程。

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.

有一个hack,但是,应用程序池必须是 DefaultAppPool,因此您无法创建您的自定义应用程序池,应该适合更多部署。

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.

这篇关于Windows的Docker:ASP.NET站点无法读取Dockerfile中定义的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆