【发布时间】:2019-08-07 10:01:11
【问题描述】:
我在尝试为我的应用程序设置 sql docker 容器化时遇到以下错误:-
An unhandled exception occurred while processing the request.
SqlException: Cannot open database "xyzDatabase" requested by the login. The login failed.Login failed for user 'sa'.
WebMVC: Docker 文件配置:-
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build
WORKDIR /src
COPY ["src/Web/WebUI/WebUI.csproj", "src/Web/WebUI/"]
COPY ["src/Core/Application/Application.csproj", "src/Core/Application/"]
COPY ["src/Infrastructure/Persistence/Persistence.csproj", "src/Infrastructure/Persistence/"]
RUN dotnet restore "src/Web/WebUI/WebUI.csproj"
COPY . .
WORKDIR "/src/src/Web/WebUI"
RUN dotnet build "WebUI.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "WebUI.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "WebUI.dll"]
启动:-
public static IServiceCollection AddSqlServerDatabase(this IServiceCollection services, IConfiguration configure)
{
services.AddEntityFrameworkSqlServer()
.AddDbContext<CatalogContext>(options =>
{
options.UseSqlServer(configure["ConnectionString"],
sqlServerOptionsAction: resilient =>
{
//resilient.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
resilient.EnableRetryOnFailure(maxRetryCount: 10, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
});
}, ServiceLifetime.Scoped);
services.AddEntityFrameworkSqlServer()
.AddDbContext<AppIdentityDbContext>(options =>
{
options.UseSqlServer(configure["ConnectionString"],
sqlServerOptionsAction: resilient =>
{
resilient.EnableRetryOnFailure(maxRetryCount: 10, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
});
}, ServiceLifetime.Scoped);
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<AppIdentityDbContext>()
.AddDefaultTokenProviders();
return services;
}
docker-compose.yml 文件:-
version: '3.4'
services:
sql.data:
image: microsoft/mssql-server-linux:2017-latest
webui:
image: ${DOCKER_REGISTRY-}webui
build:
context: .
dockerfile: src/Web/WebUI/Dockerfile
depends_on:
- sql.data
docker-compose.override.yml
version: '3.4'
services:
sql.data:
environment:
- SA_PASSWORD=Pass@word
- ACCEPT_EULA=Y
ports:
- "5433:1433"
webui:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=http://0.0.0.0:80
- ConnectionString=Server=sql.data;Database=eWebShopDatabase;User=sa;Password=Pass@word
ports:
- "5107:80"
volumes:
- ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
我知道 Volumes 是什么意思,但我真的不明白它试图在这行中说什么:-
卷:${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro,
当我们从项目中添加“Container Orchestrator Support”时会自动添加。
我真的不明白我是否需要在将 sql 图像容器与 asp.net 核心应用程序附加之前创建一个用户名,或者它是否根据代码中实现的值默认创建。
appsettings.json 文件:-
{
"ConnectionString": "Server=tcp:127.0.0.1,5433;Database=xyzDatabase;User=sa;Password=Pass@word;"
}
docker 构建并创建了容器映像文件,但它没有运行或无法为数据库播种,因为它无法登录可能是用户不存在或无法创建。
可能有人遇到过这样的问题,希望对这个问题有一些见解!
谢谢!
【问题讨论】:
-
我假设数据库
xyz存在于服务器上?但是,实际上,您不应该将sa帐户用于sa职责以外的任何事情;当然它不应该被应用程序使用。您应该创建一个单独的登录,仅具有所述应用程序所需的权限。
标签: sql-server asp.net-mvc asp.net-core docker-compose dockerfile