简体   繁体   English

从 docker 容器连接到 SQL Server 数据库

[英]Connect to SQL Server database from a docker container

I have docker for windows installed on my machine.我的机器上安装了适用于 Windows 的 docker。 There is a console application targeting .net core 1.0.0 that tries to access a SQL Server database running on a different VM.有一个面向 .net core 1.0.0 的控制台应用程序试图访问在不同 VM 上运行的 SQL Server 数据库。 I can ping the VM running SQL Server from my machine.我可以从我的机器 ping 运行 SQL Server 的 VM。

When I try to run the console application using dotnet run from the command prompt on my machine it works fine.当我尝试使用dotnet run从我机器上的命令提示符运行控制台应用程序时,它工作正常。 But when the same application is run inside a docker container I get a message但是当同一个应用程序在 docker 容器中运行时,我收到一条消息

A network-related or instance-specific error occurred while establishing a connection to SQL Server.与 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.验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server) (提供程序:TCP 提供程序,错误:40 - 无法打开与 SQL Server 的连接)

I tried using我尝试使用

docker run --add-host sqldemo:<VM running sql server ip here>

but that made no difference.但这没有区别。

Assumptions假设

  • Microsoft SQL Server 2016微软 SQL Server 2016
  • Windows 10 Anniversary Update Windows 10 周年更新
  • Windows Containers视窗容器
  • ASP.NET Core application ASP.NET 核心应用程序

Add a SQL user to your SQL database.将 SQL 用户添加到 SQL 数据库。

  • In MS SQL expand the database在 MS SQL 中扩展数据库
  • Right click on 'Security / Logins'右键单击“安全/登录”
  • Select 'New Login'选择“新登录”
  • Create a user name and password.创建用户名和密码。
  • Assign a 'Server Role(s)'...I used sysadmin since I'm just testing分配“服务器角色”...我使用 sysadmin,因为我只是在测试
  • Under 'User Mapping' I added my new user to my database and used 'dbo' for schema.在“用户映射”下,我将我的新用户添加到我的数据库并使用“dbo”作为模式。

Change SQL Authentication to allow SQL Server Authentication Mode更改 SQL 身份验证以允许 SQL Server 身份验证模式

Right click on your database, select 'Properties / Security / Server Authentication / SQL Server and Windows Authentication Mode' radio button.右键单击您的数据库,选择“属性/安全/服务器身份验证/SQL Server 和 Windows 身份验证模式”单选按钮。 Restart the MS SQL service.重新启动 MS SQL 服务。

Update your appsettings.json with your new user name and password使用您的新用户名和密码更新您的 appsettings.json

Example示例

"ConnectionStrings": {
        "DefaultConnection": "Server=YourServerName;Database=YourDatabaseName;MultipleActiveResultSets=true;User Id=UserNameYouJustAdded;Password=PassordYouJustCreated"
},

Make sure you remove Trusted_Connection=True .确保删除Trusted_Connection=True

Create a Docker file创建一个 Docker 文件

My example Docker file我的示例 Docker 文件

FROM microsoft/dotnet:nanoserver
ARG source=.
WORKDIR /app 
EXPOSE 5000 
EXPOSE 1433 
ENV ASPNETCORE_URLS http://+:5000 
COPY $source .

Publish Application发布应用

Running from the same location as the Docker file in an elevated PowerShell在提升的 PowerShell 中从与 Docker 文件相同的位置运行

dotnet publish

docker build bin\Debug\netcoreapp1.0\publish -t aspidserver

docker run -it  aspidserver cmd

I wanted to run the container and see the output as it was running in PowerShell.我想运行容器并查看它在 PowerShell 中运行时的输出。

Once the container was up and running in the container at the command prompt I kicked off my application.一旦容器在命令提示符下启动并在容器中运行,我就启动了我的应用程序。

dotnet nameOfApplication.dll

If everything went to plan one should be up and running.如果一切按计划进行,则应该启动并运行。

You can run a docker container with network settings set to host .您可以在网络设置设置为host运行 docker 容器。 Such a container will share the network stack with the docker host and from the container point of view, localhost (or 127.0.0.1) will refer to the docker host.这样的容器将与 docker 主机共享网络堆栈,从容器的角度来看,localhost(或 127.0.0.1)将指代 docker 主机。

docker run --net=host ... 

Then you should get the SQL Server database from inside the docker container as you do from your host.然后,您应该像从主机一样从 docker 容器内部获取 SQL Server 数据库。

as in this answer就像在这个答案中一样

SQL Server instance string connection in Linux Docker Linux Docker 中的 SQL Server 实例字符串连接

According to Saurabh Singh from Microsoft: 根据微软的Saurabh Singh 的说法

The Instance name support is available in v 1.1 of .Net Core.实例名称支持在 .Net Core 的 v 1.1 中可用。 In v1.0 of .Net Core, Instance names are not supported on OS other than Windows.在 .Net Core v1.0 中,Windows 以外的操作系统不支持实例名称。

So I don't think you can connect from .Net Core 1.0 running on Linux to an SQL Server using instance name.因此,我认为您无法使用实例名称从 Linux 上运行的 .Net Core 1.0 连接到 SQL Server。

Your choices seem to be:您的选择似乎是:

 don't use instance name wait for .Net Core 1.1 (planned for "Fall 2016") use pre-release version of .Net Core 1.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从 docker 容器连接到 SQL Server - Unable to connect to SQL Server from a docker container 无法从带有 ASP.NET Core 的 Docker 容器连接到 SQL Server 容器 - Can't connect from the Docker container with ASP.NET Core to SQL Server container 无法使用桥接网络模式从 docker 容器连接到远程 SQL 服务器 - Unable to connect to remote SQL server from docker container with bridge network mode 在 docker 容器上连接 Azure Sql 服务器数据库时出现问题 - Problems connecting Azure Sql Server database on docker container 无法连接到部署为 docker 容器的 MQTTNet 服务器 - Unable to connect to MQTTNet server deployed as docker container C# + DockerCompose - 在尝试连接之前等待 MS SQL Server Docker 容器启动 - C# + DockerCompose - Wait for MS SQL Server Docker container to be up before trying to connect 无法从docker windows容器中的asp.net核心应用程序连接到主机数据库 - Not able to connect to host database from asp.net core application which is in docker windows container SQL Server数据库-无法连接到它 - SQL Server database - cannot connect to it 连接到SQL Server数据库错误 - Connect to SQL Server database error 连接到SQL Server CE数据库 - Connect to SQL Server CE database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM