You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker部署ASP.NET API连接MSSQL失败,Fluent Migrator报错求助

Docker容器中ASP.NET API连接MSSQL数据库失败(Fluent Migrator报错)

我尝试通过Docker容器启动ASP.NET API与MSSQL数据库,但Fluent Migrator始终提示数据库连接失败。以下是相关配置及错误信息:

Dockerfile

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /src

COPY *.sln "./"
COPY ["Desk.API/Desk.API.csproj", "Desk.API/"]
COPY ["Desk.Migrations/Desk.Migrations.csproj", "Desk.Migrations/"]
RUN dotnet restore "Desk.API/Desk.API.csproj"

COPY . .
WORKDIR "/src/Desk.API"
RUN dotnet publish "Desk.API.csproj" -c Release -o /app --no-restore

FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=build-env /app .

EXPOSE 80

ENTRYPOINT ["dotnet", "Desk.API.dll"]

docker-compose.yml

version: '3.9'

services:
  sqldata:
    image: mcr.microsoft.com/mssql/server:2022-latest
    environment:
      - SA_PASSWORD=vV5r9tn0M4@
      - ACCEPT_EULA=Y
    ports:
      - "1450:1433"

  api:
    build:
      context: .
      dockerfile: Desk.API/docker/Dockerfile
    ports:
     - "8080:80"
    depends_on:
      - sqldata

连接字符串

"ConnectionStrings": {
    "Default": "Data Source=sqldata;Initial Catalog=Desk;Persist Security Info=True;User ID=sa;Password=vV5r9tn0M4@"
},

异常信息

fail: FluentMigrator.Runner.Processors.SqlServer.SqlServer2016Processor[0]
There was an exception checking if table VersionInfo in (null) exists
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: Could not open a connection to SQL Server)


解决方案
  • SQL容器未完全就绪API就发起连接
    depends_on仅保证sqldata容器先启动,但不保证SQL Server服务已完成初始化。API启动时SQL可能还在加载,导致连接失败。
    修复方式:

    1. 加入启动等待逻辑:在API容器中使用wait-for-it工具等待SQL端口就绪,修改Dockerfile的ASP.NET阶段:
      RUN apt-get update && apt-get install -y wget
      RUN wget -O /wait-for-it.sh https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh
      RUN chmod +x /wait-for-it.sh
      
      ENTRYPOINT ["/wait-for-it.sh", "sqldata:1433", "--", "dotnet", "Desk.API.dll"]
      
    2. 或者在API代码中添加连接重试:在Program.cs初始化数据库前,循环尝试连接SQL Server,失败则间隔重试几次。
  • 目标数据库Desk不存在
    连接字符串指定了Initial Catalog=Desk,但SQL Server初始化时不会自动创建该库,Fluent Migrator无法连接不存在的数据库执行迁移。
    修复方式:

    1. 先连接到master库,再创建目标库:修改连接字符串为
      "Default": "Data Source=sqldata;Initial Catalog=master;Persist Security Info=True;User ID=sa;Password=vV5r9tn0M4@"
      
      然后在Fluent Migrator迁移脚本开头添加创建Desk数据库的逻辑,或者在API启动时执行创建库的SQL语句。
  • 验证网络连通性
    默认docker-compose会为所有服务创建同一网络,sqldata作为服务名可直接解析。若自定义了网络,需确保两个服务在同一网络内。
    可进入API容器执行ping sqldata测试解析,或查看SQL容器日志,确认出现SQL Server is now ready for client connections字样,说明服务已就绪。

内容的提问来源于stack exchange,提问作者Persei

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 19:18:17