LoginSignup
0
0

More than 3 years have passed since last update.

docker上のSQLサーバーに接続できない。 ASP.NET CORE

Posted at

VisualStudio2019ASP.NETCORE APIアプリを作成中。
docker-composeでSQLサーバーも作成しAPIからDBを読みだすとエラー。

Microsoft.Data.SqlClient.SqlException (0x80131904): 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)
at Microsoft.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String accessToken, DbConnectionPool pool, SqlAuthenticationProviderManager sqlAuthProviderManager)
at Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
at Microsoft.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
at Microsoft.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at Microsoft.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)

SQLマネジメントツールで確認したら繋がる。
asp側のdockerからPINGを打っても繋がる。名前解決の問題ではなさそう。

原因はDocker-compose.override.ymlにあるDB接続のポート番号。
DefaultConnectionの1433部分。
Dockerで他のポートにマッピングしていたんだけど、コンテナー同士の通信では外部マッピングの物ではなく、内部での1433を設定しないと通信できない。

environment:
  - ASPNETCORE_ENVIRONMENT=Development
  - ASPNETCORE_URLS=https://+:443;http://+:80
  - ASPNETCORE_HTTPS_PORT=84303
  - DefaultConnection=Server=db-conainer-name,1433;Database=database-name;MultipleActiveResultSets=true;User Id=said;password=strongpassword;

検索しても解決がなかなか載ってなくて苦労した。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0