Windows 上的 Docker 从 dotnetcore 应用程序连接到 sql server

标签 docker asp.net-core

我使用 asp.net 核心构建了一个简单的 api,它从 sql server 数据库返回一些数据。

它在 VS 和命令行中运行良好,但是当我在我的 windows 10 机器上构建和运行应用程序作为 docker 容器时,当我调用 api 时,我不断收到此错误

System.Data.SqlClient.SqlException: Connection Timeout Expired. The timeout period elapsed during the post-login phase. The connection could have timed out while waiting for server to complete the login process and respond; Or it could have timed out while attempting to create multiple active connections. The duration spent while attempting to connect to this server was - [Pre-Login] initialization=425; handshake=265; [Login] initialization=5; authentication=9; [Post-Login] complete=14034; ---> System.ComponentModel.Win32Exception: Unknown error 258

我不太确定这是在告诉我什么,就好像它找不到 sql server 机器一样。我是否必须在 docker 配置中或在我运行容器时以某种方式公开端口 1433

最佳答案

正如 Tseng 在他的评论中解释的那样,您的容器在它们自己的网络中,而不是在您主机的网络中。您确实希望公开该端口(或映射到主机上的另一个可用端口),您可以使用 Docker Compose 轻松完成。在下面的示例中(您将在 docker-compose.yml 文件中),sql 是您的数据库容器的名称,1337 是本地端口,并且1433 是容器的端口。

sql:
 ports:
 - "1337:1433"

关于Windows 上的 Docker 从 dotnetcore 应用程序连接到 sql server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40117958/

相关文章:

php - Dockerize 用于生产的 PHP 应用程序

c# - ASP.NET Core2.2 中使用 CancellationToken 取消内部任务

docker - Kubernetes Pod重定向到另一个Pod中的相同端口

django - Docker 容器拒绝连接

c# - 在数据注释验证错误消息中隐藏底层属性名称

c# - 在 Blazor 应用程序中使用 Azure Active Directory 时如何在 asp net core 3.1 中自定义注销页面

asp.net-core - .NET-Core 字节数组到图像

azure - 如何在单个应用程序服务上部署 aspnet core web api 和 aspnet webapi

docker - 无法从 docker-compose logs -f [container name] 退出

docker - Docker-您可以在没有服务的情况下在集群中的节点上创建容器吗?