我有一个 ASP.NET Core v3 Web API 服务。它可以正常运行并连接到普通的 Windows 托管的 SQL Server 实例。但是当我尝试将它连接到 Docker Linux 托管的 SQL Server 实例时,它会失败。
以下是一些细节:
- 两个容器都在我的 Windows 10 开发机上运行。 - Web API 服务容器是基于 Linux 的,并且单独使用时运行良好。 - Linux SQL Server 容器单独使用时也可以正常工作。
- 我可以通过 SSMS 连接并运行查询而没有任何问题。
- 两个容器都位于同一个默认的桥接网络中。
- 我运行了 `docker network inspect bridge -f "{{json .Containers }}"` 命令,它列出了两个容器。
这是我的数据库连接字符串:
"Server=localhost;Database=MyDatabase;User Id=MySqlUser;Password=MyPassword"
我还尝试在我的 hosts 文件中添加 localsql 条目设置为 127.0.0.0。 localhost 和 localsql 都可以使用 SSMS 连接到 Docker 数据库而没有问题。
这是实际的错误消息:
"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)"
当我尝试使用 hosts 文件中的 localsql 时,它有一个内部异常,消息为 "Name or service not known"。
以下是一些细节:
- 两个容器都在我的 Windows 10 开发机上运行。 - Web API 服务容器是基于 Linux 的,并且单独使用时运行良好。 - Linux SQL Server 容器单独使用时也可以正常工作。
- 我可以通过 SSMS 连接并运行查询而没有任何问题。
- 两个容器都位于同一个默认的桥接网络中。
- 我运行了 `docker network inspect bridge -f "{{json .Containers }}"` 命令,它列出了两个容器。
这是我的数据库连接字符串:
"Server=localhost;Database=MyDatabase;User Id=MySqlUser;Password=MyPassword"
我还尝试在我的 hosts 文件中添加 localsql 条目设置为 127.0.0.0。 localhost 和 localsql 都可以使用 SSMS 连接到 Docker 数据库而没有问题。
这是实际的错误消息:
"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)"
当我尝试使用 hosts 文件中的 localsql 时,它有一个内部异常,消息为 "Name or service not known"。