KnowHoper
KnowHoper

Reputation: 4642

Cannot connect to container within compose but host connectivity works

Using the docker compose file

  version: '3'
    services:
      sqlserver:
        image: microsoft/mssql-server-linux:2017-latest
        ports:
          - 1401:1433
        volumes:
          - ./db:/tmp/data
        environment:
          - ACCEPT_EULA=Y
          - "MSSQL_SA_PASSWORD='Password99'"
        command:
          - /tmp/data/run.sh
      api:     
          build: 
            context: .
            dockerfile: dockerfile
          depends_on:
                    - sqlserver
          links:
                - sqlserver
          ports:
                - 5000:80

With appsettings.json connection string:

"ConnectionStrings": {
    "Entities": "data source=(local),1401;initial catalog=DB_Test;user id=sa;password=Password99;enlist=False;multipleactiveresultsets=True;connect timeout=30;App=EntityFramework"
  },

I get the error

api_1 | 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)

A connection from a host running api instance to the sql server instance running in docker-compose works fine, so the SQL instance running in the container is fine.

Can anyone help with connectivity between api & sql inside docker?

Thanks!

Upvotes: 0

Views: 793

Answers (1)

user15398983
user15398983

Reputation: 26

Inside docker the hostname for the database will be "sqlserver" not localhost or 127.0.0.1

Upvotes: 1

Related Questions