Docker .Net Core应用程序,尝试连接到SQL db

时间:2017-09-27 19:51:41

标签: c# sql visual-studio docker .net-core

在Visual Studio 2017中,我使用带有Identity的.Net Core 2 MVC项目创建了一个应用程序。 VS2017为我构建了dockerfile和compose文件。应用程序本身成功构建。 VS还设置了迁移,而不是,我所要做的就是运行迁移并设置并连接到数据库。默认情况下,它执行“localDB”,是的,它以这种方式成功运行。我的Docker中的.Net Core应用程序成功连接到非docker SQL db。但是,我想在docker中使用SQL DB来处理我的应用程序。

为了做到这一点,我只进行了两次更改。我将docker-compose文件更改为:

version: '3'

services:
  identity.api:
    image: identity.api
    build:
      context: ./Identity.api
      dockerfile: Dockerfile
    networks:
      - myapp

  identityDB:
    image: "microsoft/mssql-server-linux"
    environment:
      SA_PASSWORD: "Password01!"
      ACCEPT_EULA: "Y"
    ports:
      - "1433:1433"
    networks:
      - myapp


networks:
  myapp:
    driver: bridge

我的连接字符串为:

"DefaultConnection": "Server=identityDB;Integrated Security=False;User ID=sa;Password=Password01!;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"

我得到的错误是:

SqlException:建立与SQL Server的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。 (提供者:TCP提供者,错误:35 - 捕获到内部异常)

        An unhandled exception occurred while processing the request.

        System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity
identity, SqlConnectionString connectionOptions, object providerInfo, bool 
redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData
reconnectSessionData, bool applyTransientFaultHandling)

当我将数据库容器移动到identity.api容器之前启动时,我收到此错误:

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)

0 个答案:

没有答案