Docker中的ASPNETCORE_ENVIRONMENT [英] ASPNETCORE_ENVIRONMENT in Docker

查看:641
本文介绍了Docker中的ASPNETCORE_ENVIRONMENT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在设置 ASPNETCORE_ENVIRONMENT 变量时遇到问题,该变量在 docker容器中运行我的项目。问题是该值始终被设置/覆盖为开发

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

我尝试在中设置环境变量Dockerfile 使用

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 compose选项直接运行该项目

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:\Users\Administrator>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#


推荐答案

通过为 ASPNETCORE_ENVIRONMENT 配置命令 dotnet CoreDocker.dll --environment = X

尝试执行ch愤怒 dockerfile 如下:

Try to change dockerfile like below:

ENTRYPOINT [ dotnet, CoreDocker.dll , --environment = X]

这篇关于Docker中的ASPNETCORE_ENVIRONMENT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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