user3432478
user3432478

Reputation: 95

asp.net core 3.0 docker image failing to create

I am using asp.net core 3.0 with sql server, trying to create microservice using docker. However when I tried to run docker-compose I am getting

"An error occurred while migrating or seeding the database." error. I have entity-framework 3.1 migrations in code, when the new database container spins-up I believe it is trying to run migrations but may be database doesn't exist.

My Startup.cs

 public void ConfigureServices(IServiceCollection services)
 {
            var server = Configuration["DBServer"] ?? "192.168.0.3";
            var port = Configuration["DBPort"] ?? "1432";
            var user = Configuration["DBUser"] ?? "SA";
            var password = Configuration["DBPassword"] ?? "Passw0rd1!";
            var database = Configuration["Database"] ?? "AFE-Sales";

        services.AddDbContext<InsuranceContext>(options => options.UseSqlServer($"Server={server},{port};Initial Catalog={database};User ID = {user}; Password={password}"));

Dockerfile

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["src/WebUI/WebUI.csproj", "src/WebUI/"]
COPY ["src/Application/Application.csproj", "src/Application/"]
COPY ["src/Domain/Domain.csproj", "src/Domain/"]
COPY ["src/Infrastructure/Infrastructure.csproj", "src/Infrastructure/"]
RUN dotnet restore "src/WebUI/WebUI.csproj"
COPY . .
WORKDIR "/src/src/WebUI"

RUN dotnet build "WebUI.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "WebUI.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Insurance.WebUI.dll"]

Docker-compose

    version: '3.4'
    services:
        ms-sql-server:
        image: "mcr.microsoft.com/mssql/server:2017-latest-ubuntu"
        environment:
        ACCEPT_EULA: "Y"
        SA_PASSWORD: "Passw0rd1!"
        MSSQL_PID: Express
    ports:
        - "5432:5432"
        - "1433:1433"
  webui:
    image: ${DOCKER_REGISTRY-}webui
    container_name: mortgage
    build:
      context: .
      dockerfile: src/WebUI/Dockerfile
    ports:
        - 5000:80
    environment:
        DBServer: "192.168.0.3"
        DBPort: "1432"
        DBUser: "SA"
        DBPassword: "Passw0rd1!"
        Database: "mortgage"
    depends_on:
        - ms-sql-server

DBContext

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
            if (!optionsBuilder.IsConfigured)
            {
            optionsBuilder.UseSqlServer("Server=192.168.0.3,1432;Database=AFE-Sales;User ID=SA;Password=Passw0rd1!");
            
            }
            base.OnConfiguring(optionsBuilder);
}

appsettings.json

{
  "UseInMemoryDatabase": true,
  "ConnectionStrings": {
   "Default": "Server=192.168.0.3,1432;Database=mortgage;User ID=SA;Password=Passw0rd1!",

Error I am getting

"mortgage | fail: Insurance.WebUI.Program[0] mortgage | An error occurred while migrating or seeding the database. mortgage | Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server) mortgage | at Microsoft.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String accessToken, DbConnectionPool pool, SqlAuthenticationProviderManager sqlAuthProviderManager)"*

Upvotes: 0

Views: 406

Answers (1)

Durga Prasad
Durga Prasad

Reputation: 989

Your service is not able to resolve the ip address provided in connection string for sql server inside of docker server while running the containers.

I would suggest you to follow the steps provided inline to connect your sql server with .net core application

compose asp.net core service with sql server via docker compose

Upvotes: 0

Related Questions