我使用 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/