简体   繁体   中英

Can't connect to SQL Server express from .net core app running on docker

I have SQL Server 2016 Express, docker for Windows and IIS installed on my Windows 10 machine.

SQL Server Express is configured to listen on 1455 port. The Northwind sample database is there.

Windows Firewall is disabled.

Ipconfig shows the following:

Ethernet adapter vEthernet (DockerNAT) 2:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::70f1:3323:a7a8:b7a5%21
   IPv4 Address. . . . . . . . . . . : 10.0.75.1
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . :

I created a very simple .net core 2 console app in Visual Studio 2017:

using System;
using System.Data.SqlClient;
using System.Net.Http;

namespace ConnectSqlServer
{
    class Program
    {
        static void TestHttp()
        {
            using (var client = new HttpClient())
            {
                var response = client.GetAsync("http://10.0.75.1").Result;
                var content = response.Content.ReadAsStringAsync().Result;
                Console.WriteLine(content);
            }
        }

        static void Main(string[] args)
        {
            TestHttp();
            var cn = new SqlConnection("Data Source=10.0.75.1\\SQLEXPRESS,1455;Initial Catalog=Northwind;User ID=docker;Password=SomeStrongPassword;");
            cn.Open();
        }
    }
}

The app runs successfully when running on Windows from Visual Studio. But when I add Docker Support and run it, the TestHttp method works ok, but I cannot connect to SQL Server, I get the following exception:

System.Data.SqlClient.SqlException
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)

This is my Dockerfile :

FROM microsoft/dotnet:2.0-runtime
ARG source
WORKDIR /app
COPY ${source:-obj/Docker/publish} .
ENTRYPOINT ["dotnet", "ConnectSqlServer.dll"]

And this is my docker-compose.yml :

version: '3'

services:

  connectsqlserver:
    image: connectsqlserver
    build:
      context: ./ConnectSqlServer
      dockerfile: Dockerfile

What should I do to connect to local SQL Server Express running on the Windows host from a .net core 2.0 app running on a docker container?

EDIT:

Answer: Be careful when disabling Windows Firewall.

I turned off Windows Firewall, but only on Domain.networks. Windows sees the DockerNAT adapter as a public.network. So Windows firewall was blocking the connection to SQL Server.

I turned off Windows Firewall on public.networks and I can connect now.

The problem was I didn't disable Windows Firewall correctly.

I turned off Windows Firewall, but only on Domain networks. Windows sees the DockerNAT adapter as a public network. So Windows firewall was blocking the connection to SQL Server.

I turned off Windows Firewall on public networks and I can connect now.

You can try to start the container using network mode as host. In that case the container can connect to applications running on the host machine using localhost as a DNS name.

version: '3'

services:
  connectsqlserver:
    image: connectsqlserver
    build:
      context: ./ConnectSqlServer
      dockerfile: Dockerfile
    network_mode: "host"

对我来说,诀窍是在连接字符串中设置服务器属性,IP,端口。

Server=10.10.0.27,1433;Database=database;User id=user;password=pwd;

Well what worked for me is, changing localhost to 127.0.0.1. I didn't have to disable any of the Firewall.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM