title: How do I use .env file for docker-compose vars in config for asp.net core app?
body: <p>I am trying to use a .env file to specify a base connection string for my containers in docker compose. When I run <code>docker-compose config</code> everything looks good, however when I run my app the connection string is not including the variable from the <code>.env</code> file.</p>
  
  <p>This is a sample of my <code>.env</code>:</p>
  
  <pre><code>BASE_CONNECTION_STRING=Server=sqldb;userid=root;pwd=Pass@word;
  </code></pre>
  
  <p>and here is a sample of my docker-compose:</p>
  
  <pre><code>  listings.api:
      environment:
        ASPNETCORE_ENVIRONMENT: Development
        ASPNETCORE_URLS: http://+:80
        ConnectionString: ${BASE_CONNECTION_STRING}Database=RentalListings.Services.ListingsDb;
  </code></pre>
  
  <p>When I run <code>docker-compose config</code> I get the correct result, namely:</p>
  
  <pre><code>ConnectionString: Server=sqldb;userid=root;pwd=Pass@word;Database=RentalListings.Services.ListingsDb;
  </code></pre>
  
  <p>However, when I run my app I don't get the correct value. Running <code>configuration["ConnectionString"]</code> returns just <code>Database=RentalListings.Services.ListingsDb;</code></p>
  
  <p>I tried adding <code>.AddEnvironmentVariables();</code> to my <code>ConfigurationBuilder()</code> which did add environment variables that were not there before when I inspected by configuration before, but did not add the ones from the <code>.env</code> file. Regardless I'm not sure if this part should matter as I would assume that this variable should be compiled and passed in by docker regardless of my <code>ConfigurationBuilder</code>.</p>
  
  <p>Any help would be greatly appreciated :)</p>
  
  <p>UPDATE:
  The call to <code>configuration["ConnectionString"]</code> is in my <code>ConfigureServices()</code> in <code>Startup.cs</code>:</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
          {
              services
                  .AddCustomMVC(Configuration)
                  .AddCustomDbContext(Configuration, _loggerFactory)
                  .AddAppSettings(Configuration);
          }
  </code></pre>
  
  <p>with:</p>
  
  <pre><code>public static IServiceCollection AddCustomDbContext(this IServiceCollection services, IConfiguration configuration, ILoggerFactory loggerFactory)
          {
              ILogger&lt;Startup&gt; logger = loggerFactory.CreateLogger&lt;Startup&gt;();
              logger.LogInformation($"Conn string: {configuration["ConnectionString"]}");
              services.AddDbContext&lt;ListingsContext&gt;(options =&gt;
              {
                  options.UseMySql(configuration["ConnectionString"]);
                  options.UseLoggerFactory(loggerFactory);
              });
  
              return services;
          }
  </code></pre>
  
  <p>Here is my full <code>docker-compose.yml</code>:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Here is my <code>docker-compose.override.yml</code>:</p>
  
  <pre><code>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"
  </code></pre>
  
  <p>And here is the Dockerfile for the Listings.API</p>
  
  <pre><code>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"]
  </code></pre>