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

如何利用Docker和ngrok配置SQL Server连接字符串?API连容器报500错

Docker容器中API连接SQL Server出现500错误

我是一名学生,正在开发需要用到Docker容器与ngrok的MAUI应用/API。目前已将API和SQL Server部署到Docker容器,但API连接SQL Server容器时出现500错误。

我的SQL Server运行在localdb,已创建具备相关权限的管理员用户。容器用的是Linux版本,主机是Windows系统(之前用Windows容器有问题,切换到Linux容器后解决了)。

我试过用完整ngrok URL、部分URL、localhost等多种组合,ngrok认证令牌已经配置在ngrok.yml里,API的launchsettings.json也调用了默认连接字符串。我知道问题应该不难,但查了大量文档、示例和帖子都没解决,求帮忙。

配置文件

docker-compose.yaml

services:
  sql-server:
    image: mcr.microsoft.com/mssql/server:2022-latest
    container_name: sql-server
    environment:
      - ACCEPT_EULA=Y
      - ADMIN_PASSWORD=adminPassword32!
      - SQL_SERVER_URL=6.tcp.ngrok.io,18846
    ports:
      - "1433:1433"  # Exposes SQL Server port to host
    networks:
      - recipe-network

  recipe-api:
    build:
      context: .
      dockerfile: RecipeAPI/Dockerfile
    container_name: recipe-api
    depends_on:
      - sql-server
    environment:
      - RECIPE_API_URL=https://3ed42d472f7e.ngrok.app
      - ConnectionStrings__DefaultConnection="Data Source=sql-server,1433;Database=RecipesDB;User ID=admin;Password=adminPassword32!;TrustServerCertificate=true;"
    ports:
      - "5000:5000"  # Exposes API port to host
    networks:
      - recipe-network
    restart: on-failure

networks:
  recipe-network:
    driver: bridge

volumes:
  android-sdk-volume:

appsettings.json(位于recipe-api中)

{
  "ConnectionStings": {
    "DefaultConnection": "Data Source=sql-server,1433;Database=RecipesDB;User ID=admin;Password=adminPassword32!;TrustServerCertificate=true;"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*"
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 02:22:32