web-api can't connect to sqlserver in Docker composer network

Viewed 67

I'm trying to understand why my simple web-api can't connect with sqlserver via docker-compose.

version: '3.4'

services:
  db:
    container_name: mssql
    ports:
      - 15001:1433
    build:
      context: .
      dockerfile: DB/Dockerfile
    networks:
      - app-network
    
  silo:
    image: ${DOCKER_REGISTRY-}silo
    container_name: orleans
    links:
      - db
    build:
      context: .
      dockerfile: Silo/Dockerfile
    depends_on:
      - db
    networks:
      - app-network
networks:
  app-network:
    driver: bridge

enter image description here

fail: Orleans.Runtime.MembershipService.MembershipTableManager[100646]

Membership failed to start: System.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 System.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)

at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)

at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)

at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)

at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)

at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)

at System.Data.ProviderBase.DbConnectionPool.WaitForPendingOpen()

--- End of stack trace from previous location ---

enter image description here

Both using the loopback address @"Server=127.0.0.1,15001;User ID= ..." and service name @"Server=db,15001;User ID= ...

However sqlserver is available outside container:

enter image description here

And I can run the web-api against the dockerized db via local standard dotnet run:

enter image description here

enter image description here enter image description here

I suspect that it is something wrong in my docker-compose networking, but I can't figure out what... What am I doing wrong?

As suggested by @Facty, changed connection string accordingly to container_name:

var connectionString = @"Server=mssql,1433;User ID=...

and cleaned up the compose file:

version: '3.4'

services:
  db:
    image: ${DOCKER_REGISTRY-}db
    container_name: mssql
    ports:
      - 15001:1433
    build:
      context: .
      dockerfile: DB/Dockerfile
    
  silo:
    image: ${DOCKER_REGISTRY-}silo
    container_name: orleans
    build:
      context: .
      dockerfile: Silo/Dockerfile
    depends_on:
      - db

now it works as expected, thanks!

0 Answers
Related