问题描述
我在设置在 docker 容器中运行我的项目的 ASPNETCORE_ENVIRONMENT 变量时遇到问题.问题是该值总是设置/覆盖为开发".
i have problems setting the ASPNETCORE_ENVIRONMENT variable running my project in a docker container. The problem is that the value is always set/overwritten to "Development".
我尝试使用
ENV ASPNETCORE_ENVIRONMENT test
还尝试使用
environment:
- ASPNETCORE_ENVIRONMENT=test
当我设置任何其他环境变量时它可以工作,例如:
When I set any other environment variable it works, for example:
environment:
- OTHER_TEST_VARIABLE=test
我假设 ASPNETCORE_ENVIRONMENT 变量的值在某处被覆盖,但我很难找出在哪里.
I assume that the value for ASPNETCORE_ENVIRONMENT variable is overwritten somewhere but I have difficulties finding out where.
我已向现有项目添加了 Docker 支持,并且正在通过 Visual Studio 的 Docker/Docker 组合选项直接运行该项目
I have added Docker support to an existing project and am running the project directly via Visual Studio's Docker/Docker compose option
该项目在 Asp Net Core 2.1 上运行
The project runs on Asp Net Core 2.1
提前致谢
我的launchSettings.json:
My launchSettings.json:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:53183/",
"sslPort": 0
}
},
"profiles": {
"Docker": {
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://localhost:{ServicePort}/api/values"
}
}
}
我也尝试将环境变量配置添加到launchSettings.json
I also tried adding the environment variable configuration to the launchSettings.json
"Docker": {
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://localhost:{ServicePort}/api/values",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "test"
}
}
我的虚拟主机:
public static IWebHost BuildWebHost(string[] args)
{
return WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((builderContext,config) =>
{
config.AddEnvironmentVariables();
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
logging.AddDebug();
})
.UseStartup<Startup>()
.Build();
}
我的 docker-compose.yml
My docker-compose.yml
version: '3.4'
services:
api:
image: ${DOCKER_REGISTRY}api
build:
context: .
dockerfile: API/Dockerfile
environment:
- ASPNETCORE_ENVIRONMENT=test
我的 Dockerfile:
My Dockerfile:
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY API/API.csproj API/
RUN dotnet restore API/API.csproj
COPY . .
WORKDIR /src/API
RUN dotnet build API.csproj -c Release -o /app
FROM build AS publish
RUN dotnet publish API.csproj -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "API.dll"]
这里是容器中的环境变量列表
Here is a list of the environment variables in the container
C:UsersAdministrator>docker exec -ti d6 /bin/bash
root@d6f26d2ed2c3:/app# printenv
HOSTNAME=d6f26d2ed2c3
ASPNETCORE_URLS=http://+:80
test1=asdasd
test2=dasdasd
test3=dasdasd
PWD=/app
HOME=/root
NUGET_FALLBACK_PACKAGES=/root/.nuget/fallbackpackages
DOTNET_USE_POLLING_FILE_WATCHER=1
ASPNETCORE_VERSION=2.1.3
DOTNET_RUNNING_IN_CONTAINER=true
TERM=xterm
SHLVL=1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ASPNETCORE_ENVIRONMENT=Development
_=/usr/bin/printenv
root@d6f26d2ed2c3:/app#
推荐答案
通过使用命令 dotnet CoreDocker.dll --environment="X"
配置 ASPNETCORE_ENVIRONMENT
对我有用>
It works for me by configuring ASPNETCORE_ENVIRONMENT
with command dotnet CoreDocker.dll --environment="X"
尝试更改dockerfile
,如下所示:
ENTRYPOINT ["dotnet", "CoreDocker.dll", "--environment=X"]
这篇关于Docker 中的 ASPNETCORE_ENVIRONMENT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!