运行Docker时:无法连接到SQL Server浏览器。确保已启动SQL Server浏览器

时间:2020-09-01 02:11:23

标签: sql-server docker asp.net-core networking deployment

我尝试首次将REST API .NET Core应用程序与Docker一起部署,但是,在创建Docker映像后,当我尝试运行时:

docker run -p 8080:80 mydockerid/dockerapi

我得到一个很大的调用堆栈,其中有错误:

fail: Microsoft.EntityFrameworkCore.Database.Connection[20004]
      An error occurred using the connection to database 'CommanderDB' on server 'HELLO\SQLEXPRESS'.
fail: Microsoft.EntityFrameworkCore.Query[10100]
      An exception occurred while iterating over the results of a query for context type 'Commander.Data.CommanderContext'.
      System.Exception: Cannot connect to SQL Server Browser. Ensure SQL Server Browser has been started.
       ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (00000005, 0xFFFDFFFF): Name or service not known
         at System.Net.Dns.InternalGetHostByName(String hostName)
         at System.Net.Dns.GetHostAddresses(String hostNameOrAddress)
         at System.Net.Sockets.UdpClient.BeginSend(Byte[] datagram, Int32 bytes, String hostname, Int32 port, AsyncCallback requestCallback, Object state)
         at System.Net.Sockets.UdpClient.<>c.<SendAsync>b__56_0(Tuple`2 targetPackedArguments, Int32 targetBytes, Int32 targetPort, AsyncCallback callback, Object 
state)

经过研究,我认为问题可能出在我的SQL Server Express的“服务器名称”上。当我登录SQL Server Management Studio时,我使用'HELLO \ SQLEXPRESS'作为服务器名称:

enter image description here

这是我在appsettings.json中的连接字符串:

"CommanderConnection": "Server=HELLO\\SQLEXPRESS;Initial Catalog=CommanderAPI;User ID=myid;Password=mypassword;"

我的服务器名称应该是其他名称吗?也许有一种方法可以将服务器名称更改为实际的IP地址?

谢谢。

1 个答案:

答案 0 :(得分:0)

默认情况下,SQLEXPRESS不接受远程连接。请按照本手册配置TCP / IP连接。

http://support.webecs.com/kb/a868/how-do-i-configure-sql-server-express-to-allow-remote-tcp-ip-connections-on-port-1433.aspx

然后尝试使用主机名和端口连接到数据库。首先在本地尝试,然后可以用相同的方式配置您的应用。

为了实验起见,您还可以将SQLEXPRESS作为docker容器启动。查看本手册-https://kimsereyblog.blogspot.com/2018/10/docker-compose-asp-net-core-application.html