.NET Core API not connecting to SQL Server running on Docker

0

I have this error when trying to execute request to my Rest API running on Docker:

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: 35 - An internal exception was caught) ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (00000005, 0xFFFDFFFF): Name or service not known

This is my connection string:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "DefaultConnection": "server=sqlserver,1433;Database=Test;Trusted_Connection=False;ConnectRetryCount=0;User Id=sa;Password=Password3784"
  }
}

This is my docker-compose.yml:

version: '3.4'

services:
  test:
    image: ${DOCKER_REGISTRY-}test
    build:
      context: .
      dockerfile: Test/Dockerfile

  sqlserverService:  
    image: microsoft/mssql-server-linux:2017-latest  
    hostname: 'sqlserver'  
    environment:  
      ACCEPT_EULA: Y  
      SA_PASSWORD: "Password3784"  
    volumes:  
      - ./data/mssql:/var/opt/mssql3  
    ports:  
      - '1433:1433'  
    expose:  
      - 1433  

Using SQL Server Management Studio on my computer login with this credentials works fine. localhost,1433 sa Password3784

sql-server
docker
.net-core
asked on Stack Overflow Dec 17, 2020 by Robert • edited Dec 17, 2020 by Dale K

2 Answers

0

Weel, I found the solution.

I was creating a new image with a SQL Server separatly from my API.

What I should do is create the SQL Server on the same docker-compose.

For that I have read this article (it's in spanish): https://dev.to/ebarrioscode/que-demonios-es-docker-docker-compose-y-como-dockerizar-dotnet-core-webapi-y-sql-server-en-un-ambiente-de-desarrollo-ideal-95a

answered on Stack Overflow Dec 18, 2020 by Robert
0

Best solution for this is just to add your localhost IP address or Server IP like:

Server=192.168.0.12\\MSSQLSERVER,1433;Database=x;Trusted_Connection=False; User Id=x;Password=x;MultipleActiveResultSets=true

With the connection string in your appsettings.json like this it should work fine.

answered on Stack Overflow Jan 30, 2021 by error505

User contributions licensed under CC BY-SA 3.0