0
votes

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

2
ports already exposes ports to containers in the swarm so expose seems redundant. Does it improve if you only use ports 1433:1433? - AlwaysLearning
Does it work by IP? - Randy in Marin
I already removed the ports 1433:1433 but no change. @AlwaysLearning - Robert
How can I know the IP stablished by Docker? This is my first time using it. @RandyinMarin - Robert

2 Answers

0
votes

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

0
votes

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.