Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.5k views
in Technique[技术] by (71.8m points)

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

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...