如何在asp.net核心应用的配置中将.env文件用于docker-compose var?

时间:2019-04-04 05:36:06

标签: asp.net-core docker-compose environment-variables

我正在尝试使用.env文件为docker compose中的容器指定基本连接字符串。当我运行docker-compose config时,一切看起来都不错,但是当我运行我的应用程序时,连接字符串不包括.env文件中的变量。

这是我的.env的示例:

BASE_CONNECTION_STRING=Server=sqldb;userid=root;pwd=Pass@word;

这是我的docker-compose示例:

  listings.api:
    environment:
      ASPNETCORE_ENVIRONMENT: Development
      ASPNETCORE_URLS: http://+:80
      ConnectionString: ${BASE_CONNECTION_STRING}Database=RentalListings.Services.ListingsDb;

运行docker-compose config时,我得到正确的结果,即:

ConnectionString: Server=sqldb;userid=root;pwd=Pass@word;Database=RentalListings.Services.ListingsDb;

但是,当我运行我的应用程序时,我没有得到正确的值。运行configuration["ConnectionString"]仅返回Database=RentalListings.Services.ListingsDb;

我尝试将.AddEnvironmentVariables();添加到我的ConfigurationBuilder()中,该确添加了以前通过配置检查时不存在的环境变量,但是没有添加来自.env文件的环境变量。无论如何,我不确定这部分是否重要,因为无论我的ConfigurationBuilder为何,我都认为该变量应由docker编译并传递。

任何帮助将不胜感激:)

更新: 对configuration["ConnectionString"]的呼叫位于我ConfigureServices()中的Startup.cs中:

public void ConfigureServices(IServiceCollection services)
        {
            services
                .AddCustomMVC(Configuration)
                .AddCustomDbContext(Configuration, _loggerFactory)
                .AddAppSettings(Configuration);
        }

具有:

public static IServiceCollection AddCustomDbContext(this IServiceCollection services, IConfiguration configuration, ILoggerFactory loggerFactory)
        {
            ILogger<Startup> logger = loggerFactory.CreateLogger<Startup>();
            logger.LogInformation($"Conn string: {configuration["ConnectionString"]}");
            services.AddDbContext<ListingsContext>(options =>
            {
                options.UseMySql(configuration["ConnectionString"]);
                options.UseLoggerFactory(loggerFactory);
            });

            return services;
        }

这是我的完整docker-compose.yml

version: '3.4'

services:
  sqldb:
    image: mariadb:latest

  listings.api:
    image: ${REGISTRY:-listings}/listings.api:${TAG:-latest}
    build:
      context: .
      dockerfile: src/Services/Listings/Listings.API/Dockerfile
    depends_on:
      - sqldb

  identity.api:
    image: ${REGISTRY:-identity}/identity.api:${TAG:-latest}
    build:
      context: .
      dockerfile: src/Services/Identity/Identity.API/Dockerfile
    depends_on:
      - sqldb

  webmvc:
    image: ${DOCKER:-listings}/webmvc:${TAG:-latest}
    build:
      context: .
      dockerfile: src/WebApps/WebMVC/Dockerfile
    depends_on:
      - listings.api

这是我的docker-compose.override.yml

version: '3.4'

services:

  sqldb:
    environment:
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
      - ACCEPT_EULA=Y
    ports:
      - "5433:1433"

  listings.api:
    environment:
      ASPNETCORE_ENVIRONMENT: Development
      ASPNETCORE_URLS: http://+:80
      ConnectionString: ${BASE_CONNECTION_STRING}Database=RentalListings.Services.ListingsDb;
    ports:
      - "57931:80"

  identity.api:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=http://+:80
      - ConnectionString=${BASE_CONNECTION_STRING}Database=RentalListings.Services.IdentityDb;
    ports:
      - "57932:80"

  webmvc:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=http://+:80
      - ListingsAPIUrl=http://listings.api:80/api/v1
      - HomeUrl=http://localhost:55338
      - ListingsScope=${OIDC_SCOPES_LISTINGS}
      - ClientId=${OIDC_MVC_CLIENT_ID}
      - ClientSecret=${OIDC_MVC_CLIENT_SECRET}
    ports:
      - "55338:80"

这是Listings.API的Dockerfile

FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY src/Services/Listings/Listings.API/Listings.API.csproj src/Services/Listings/Listings.API/
RUN dotnet restore src/Services/Listings/Listings.API/Listings.API.csproj
COPY . .
WORKDIR /src/src/Services/Listings/Listings.API
RUN dotnet build Listings.API.csproj -c Release -o /app

FROM build AS publish
RUN dotnet publish Listings.API.csproj -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Listings.API.dll"]

0 个答案:

没有答案