[英]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.但这没有区别。
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 服务。
Example示例
"ConnectionStrings": {
"DefaultConnection": "Server=YourServerName;Database=YourDatabaseName;MultipleActiveResultSets=true;User Id=UserNameYouJustAdded;Password=PassordYouJustCreated"
},
Make sure you remove Trusted_Connection=True
.确保删除
Trusted_Connection=True
。
My example Docker file我的示例 Docker 文件
FROM microsoft/dotnet:nanoserver
ARG source=.
WORKDIR /app
EXPOSE 5000
EXPOSE 1433
ENV ASPNETCORE_URLS http://+:5000
COPY $source .
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.