Docker .Net核心应用程序,试图连接到SQL数据库

在Visual Studio 2017中,我使用Identity创build了一个使用.Net Core 2 MVC项目的应用程序。 VS2017为我build立了dockerfile和撰写文件。 应用程序本身成功build立。 VS也build立了迁移,而不是我所要做的就是运行迁移,并build立并连接到数据库。 默认情况下,它是这样做到“localDB,”是的,它以这种方式成功地工作。 docker中的.Net Core应用程序成功连接到非docker SQL db。 不过,我想要一个泊坞窗内的SQL数据库来处理我的应用程序。

为了做到这一点,我只做了两个改变。 我将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 

和我的连接string:

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

我得到的错误是:

SqlException:build立到SQL Server的连接时发生networking相关或实例特定的错误。 服务器未find或无法访问。 validation实例名称是否正确,并将SQL Serverconfiguration为允许远程连接。 (提供程序:TCP提供程序,错误:35 – 发现内部exception)

  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)