Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.6k 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...