【发布时间】:2021-02-06 14:27:34
【问题描述】:
我有一个包含 2 个微服务(产品和评论)的解决方案。 2 个 API 项目中的每一个都定义了一个 Dockerfile,这里是一个审查 dockerfile 的示例:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
ENV ConnectionString:Review="Data Source=db,1433;Initial Catalog=Review;uid=sa;pwd=TesT111t!;Integrated Security=false;MultipleActiveResultSets=True"
COPY ["Review.Api/Review.Api.csproj", "Review.Api/"]
COPY ["Review.Data/Review.Data.csproj", "Review.Data/"]
COPY ["Review.Service/Review.Service.csproj", "Review.Service/"]
RUN dotnet restore "Review.Api/Review.Api.csproj"
COPY . .
WORKDIR "/src/Review.Api"
RUN dotnet build "Review.Api.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Review.Api.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Review.Api.dll"]
如您所见,dockerfile 包含一个环境变量 ConnectionString。
我在 review api 项目的启动中有一个断点来检查 Configuration 里面的内容,这是文件的样子:
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ReviewDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Review")), ServiceLifetime.Transient);
services.AddAutoMapper(typeof(FindProductReviews));
services.AddAutoMapper(typeof(FindReview));
services.AddAutoMapper(typeof(ReviewController));
services.AddAutoMapper(typeof(ProductController));
services.AddMediatR(typeof(FindProductReviews.Handler).Assembly);
services.AddMediatR(typeof(FindReview.Handler).Assembly);
services.AddMediatR(typeof(CreateReview.Handler).Assembly);
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapDefaultControllerRoute();
});
}
}
我的解决方案有一个docker-compose 文件,在运行docker-compose up -d 时会遇到断点,但是在查看Configuration 内部时,它们没有像dockerfile 中那样定义的连接字符串。
我觉得我错过了一些小东西,我查看了文档但找不到我错过的东西
【问题讨论】:
-
Linux 环境变量名称必须使用双下划线部分分隔符,而不是冒号
标签: c# docker .net-core docker-compose