无法从Docker容器连接到SQL Server [英] Unable to connect to SQL Server from a docker container

查看:659
本文介绍了无法从Docker容器连接到SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个ASP.NET Core 2.2应用程序,它的后端数据库完美地托管在单独的服务器上。所有CRUD操作均完美无缺。我们对应用程序进行了docker化处理,但开始收到以下错误消息:

We have an ASP.NET Core 2.2 application that worked with its backend database hosted on a separate server perfectly. All CRUD operations worked flawlessly. We dockerized the application but we started getting the following error message:


建立与网络的连接或特定于实例的错误时, SQL Server。服务器未找到或无法访问。验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (提供者:SQL网络接口,错误:26-错误指定服务器/实例的位置)

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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

泊坞窗组成文件类似于以下代码:

The docker compose file looks like the following code:

version: '3.4'

services:
  services.webapi:
    image: ${DOCKER_REGISTRY-}serviceswebapi
    build:
      context: .
      dockerfile: Services.WebApi\Dockerfile
      network: host

可能

UPDATE 1 :SQL Server未进行Docker化。

UPDATE 1: the SQL Server is not dockerized.

UPDATE 1 p>

更新2 :应用程序从其appsettings.json文件中读取SQL连接字符串。重命名服务器名称和凭据后,这是连接字符串:

UPDATE 2: The application reads the SQL connection string from its appsettings.json file. This is the connection string after renaming the server name and credentials:

Server=WIN-MSSQL1\\MSSQLSERVER2017;Initial Catalog=ServiceDB;User ID=cruduser;Password=foo;Connect Timeout=60;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False


推荐答案

在正常情况下,您可以通过以下方式通过composer文件将sql主机名添加到容器的/ etc / hosts文件中来进行修复:

In a normal situation you could fix by adding the sql hostname in the /etc/hosts file of the container via composer file like this:

version: '3.4'

services:
  services.webapi:
    image: ${DOCKER_REGISTRY-}serviceswebapi
    build:
      context: .
      dockerfile: Services.WebApi\Dockerfile
      network: host
    extra_hosts:
      - "WIN_MSSQL1:192.168.0.1"

不幸的是,由于此问题在composer文件中添加extra_hosts无效,我希望它们能尽快解决该问题。

Unfortunately due this issue adding the extra_hosts in the composer file will not work, I hope they fix the issue soon.

因此,让连接工作用IP地址代替主机名SQL Server的数量:

So to let connection work substitute the hostname with the ip address of SQL server:

从此处:

Server=WIN-MSSQL1\\MSSQLSERVER2017;Initial Catalog=ServiceDB;User ID=cruduser;Password=foo;Connect Timeout=60;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False

为此:

Server=192.168.0.1\\MSSQLSERVER2017;Initial Catalog=ServiceDB;User ID=cruduser;Password=foo;Connect Timeout=60;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False

这篇关于无法从Docker容器连接到SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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