【发布时间】:2019-04-04 05:36:05
【问题描述】:
我正在尝试使用 .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() 在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"]
【问题讨论】:
-
如果你使用它,你能发布你的 docker-compose.yml 文件和任何 Dockerfile 吗?您在构建或运行阶段使用“ConnectionString”吗?我感觉你在做 docker-compose build 时试图访问它。
-
@Mihai 添加了完整的 docker-compose 和 dockerfiles。还添加了获取我的 Startup.cs 中的 ConnectionString 的代码
-
对于你有错误的设置很奇怪。我确实有一个想法,我会提出解决方案,但在此之前你可以回答另一个问题:你希望 ConnectionString 填充在“docker-compose build”还是“docker-compose up”中?
-
@Mihai 我不太确定。我正在使用 Visual Studio 运行应用程序(根据这篇文章:scrum-tips.com/2017/12/27/…)似乎运行 docker-compose up -build
-
您能否将它作为环境变量添加到您的 docker-compose.override.yml 中的 ConnectionString 旁边:“- BASE_CONNECTION_STRING=${BASE_CONNECTION_STRING}”。在这两个地方添加它。根据您描述问题的方式,“ConnectionString”以某种方式在服务器上进行评估,然后它会查找 BASE_CONNECTION_STRING。根据您在此处发布的设置,这非常奇怪。所以通过这样做,我正在研究这个理论。如果这不起作用,我们需要添加更多调试消息以了解发生了什么。告诉我。
标签: asp.net-core docker-compose environment-variables