无法从Docker上运行的.net核心应用连接到SQL Server Express [英] Can't connect to SQL Server express from .net core app running on docker

查看:60
本文介绍了无法从Docker上运行的.net核心应用连接到SQL Server Express的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows 10计算机上安装了SQL Server 2016 Express,适用于Windows的docker和IIS。

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

SQL Server Express配置为侦听1455端口。那里是罗斯文示例数据库。

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

Windows防火墙已禁用。

Windows Firewall is disabled.

Ipconfig 显示以下内容:

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 . . . . . . . . . :

我在Visual Studio 2017中创建了一个非常简单的.net core 2控制台应用程序:

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();
        }
    }
}

该应用在运行时成功运行在Windows上从Visual Studio。但是,当我添加Docker Support并运行它时,TestHttp方法可以正常运行,但无法连接到SQL Server,但出现以下异常:

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

建立与SQL Server的连接时发生了与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (提供者:TCP Provider,错误:40-无法打开与SQL Server的连接)

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)

这是我的 Dockerfile

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

这是我的 docker-compose.yml

version: '3'

services:

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

我应该怎么做,才能从运行在.net核心2.0应用程序上的Windows主机上连接到本地SQL Server Express?

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?

编辑:

答案:禁用Windows防火墙时要小心。

Answer: Be careful when disabling Windows Firewall.

我关闭了Windows防火墙,但仅在域网络上。 Windows将DockerNAT适配器视为公共网络。因此Windows防火墙阻止了与SQL Server的连接。

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.

我在公用网络上关闭了Windows防火墙,现在可以连接了。

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

推荐答案

问题是我没有正确禁用Windows防火墙。

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

我关闭了Windows防火墙,但仅在域网络上。 Windows将DockerNAT适配器视为公共网络。因此Windows防火墙阻止了与SQL Server的连接。

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.

我在公用网络上关闭了Windows防火墙,现在可以连接了。

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

这篇关于无法从Docker上运行的.net核心应用连接到SQL Server Express的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆