.Net Core 3.1 MSSQL backend in Docker setup: Docker-compose up producing connection failure

0

docker-compose.yml

version: "3.9"
services:
    web:
        build: .
        ports:
            - "8000:80"
        container_name: web_application
        depends_on:
            - medicalmgr
    
    medicalmgr:
        image: "mcr.microsoft.com/mssql/server:2019-latest"
        environment:
            SA_PASSWORD: Pw@mm2021
            ACCEPT_EULA: Y
        ports:
            - "1433:1433"
        container_name: mmm_mmsql_2019

dockerfile

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /app
COPY . .
RUN dotnet restore && \
    dotnet publish -c Release -o out
    
# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "MedicalManager.dll"]

Startup.cs 's

public void CofigureServices(..){
      ....
       var server = Configuration["DBServer"] ?? "medicalmgr";
       var port = Configuration["DBPort"] ?? "1433";
       var user = Configuration["DBUser"] ?? "SA";
       var password = Configuration["DBPassword"] ?? "Pw@mm2021";
       var database = Configuration["Database"] ?? "MedicalManager";


       //var connectionStr = $"Server={server},{port};Initial Catalog={database};User ID= 
       //{user};Password={password}";
       var connectionStr = 
       @"Server=medicalmgr;Database=MedicalManager;User=sa;Password=Pw@mm2021;";
       services.AddDbContext<MedicalManagerDBContext>(
                options => options.UseSqlServer(connectionStr)
       );}

Error: Application startup exception: 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)

c#
.net
sql-server
docker
connection
asked on Stack Overflow Jan 23, 2021 by Bite-us • edited Jan 23, 2021 by Dan Guzman

1 Answer

0

Try this:

var connectionStr = 
@"Data Source=localhost,1433;Initial Catalog=MedicalManager;User Id=sa;Password=Pw@mm2021";
answered on Stack Overflow May 17, 2021 by frank_lee

User contributions licensed under CC BY-SA 3.0