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

查看:18
本文介绍了无法从 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 端口.Northwind 示例数据库在那里.

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 提供程序,错误: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

我应该怎么做才能从在 docker 容器上运行的 .net core 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天全站免登陆