从Docker容器连接到SQL Server数据库 [英] Connect to SQL Server database from a docker container

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

问题描述

我在计算机上安装了用于Windows的docker。有一个针对.net core 1.0.0的控制台应用程序,该应用程序尝试访问在其他VM上运行的SQL Server数据库。我可以从我的计算机ping运行SQL Server的VM。

I have docker for windows installed on my machine. There is a console application targeting .net core 1.0.0 that tries to access a SQL Server database running on a different VM. I can ping the VM running SQL Server from my machine.

当我尝试使用 dotnet run 在我的计算机上的命令提示符下运行正常。但是,当同一应用程序在docker容器中运行时,我收到一条消息

When I try to run the console application using dotnet run from the command prompt on my machine it works fine. But when the same application is run inside a docker container I get a message


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

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)

我尝试使用

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

但这没什么区别。

推荐答案

假设




  • Microsoft SQL Server 2016

  • Windows 10周年更新

  • Windows容器

  • ASP.NET Core应用程序

  • Assumptions

    • Microsoft SQL Server 2016
    • Windows 10 Anniversary Update
    • Windows Containers
    • ASP.NET Core application

      • 在MS SQL中,展开数据库

      • 右键单击安全性/登录名

      • 选择新登录名

      • 创建用户名和密码。

      • 分配一个服务器角色 ...由于我只是在测试,所以我使用sysadmin

      • 在用户映射下,我已将新用户添加到数据库中

      • In MS SQL expand the database
      • 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
      • Under 'User Mapping' I added my new user to my database and used 'dbo' for schema.

      右键单击数据库,选择属性/安全性/服务器身份验证/ SQL Server和Windows身份验证模式单选按钮。重新启动MS SQL服务。

      Right click on your database, select 'Properties / Security / Server Authentication / SQL Server and Windows Authentication Mode' radio button. Restart the MS SQL service.

      示例

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

      请确保删除 Trusted_Connection = True

      我的示例Docker文件

      My example Docker file

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



      发布应用程序



      从同一运行在提升的PowerShell中作为Docker文件的位置

      Publish Application

      Running from the same location as the Docker file in an elevated PowerShell

      dotnet publish
      
      docker build bin\Debug\netcoreapp1.0\publish -t aspidserver
      
      docker run -it  aspidserver cmd
      

      我想运行该容器并查看其在PowerShell中运行时的输出。

      I wanted to run the container and see the output as it was running in 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.

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

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