首页 > 解决方案 > 如何在 asp.net 核心应用程序的配置中为 docker-compose vars 使用 .env 文件?

问题描述

我正在尝试使用 .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文件中的环境变量。不管我不确定这部分是否重要,因为我认为这个变量应该由 docker 编译和传递,而不管我的ConfigurationBuilder.

任何帮助将不胜感激 :)

更新:调用configuration["ConnectionString"]在我ConfigureServices()的 in 中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"]

标签: asp.net-coredocker-composeenvironment-variables

解决方案


推荐阅读