Docker in WSL - Can't connect to SQL server on 'localhost' but with '127.0.0.1' SQL Server 2017-latest

Vishnu Prasad-FT 5 Reputation points
2023-02-15T08:55:49.01+00:00

Trying to spin up an mssql server with a docker-compose file:

version: "3.7"
services:
  sqlserver:
    image: mcr.microsoft.com/mssql/server:2017-latest
    container_name: sqlserver
    user: root
    ports:
      - "1433:1433"
    environment:
      SA_USERNAME: sa
      SA_PASSWORD: Dev1234!
      ACCEPT_EULA: Y
      MSSQL_AGENT_ENABLED: 'true'
    volumes:
      - ./volumes/data2:/var/opt/mssql/data
      - ./volumes/backup:/var/opt/mssql/Backup

The server is up and running successfully, But when tried to connect with PowerShell script code:

$connectionString = "Data Source=localhost,1433;User ID=sa;Password=Dev1234!;"
$connection = New-Object System.Data.SqlClient.SqlConnection($connectionString)
$connection.Open();

It throws error like:

"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: 0 - The
remote computer refused the network connection.)"

That is, it fails when trying to connect with 'localhost' as host, it is successfully connecting when "127.0.0.1" is specified as host.

The same happens when I tried to connect with SQL server management studio.

enter image description here

I could able to connect with 'localhost' to db with udl file also:

User's image

My docker engine is running in wsl, an ubuntu distro specifically. There is MySQL and different other servers already spun up which I can access with localhost.

Is there anything I need to specify in docker-compose to make this work? When checking documentation, there are no params aligned to it as I see.

Azure ISV (Independent Software Vendors) and Startups
Azure ISV (Independent Software Vendors) and Startups
Azure: A cloud computing platform and infrastructure for building, deploying and managing applications and services through a worldwide network of Microsoft-managed datacenters.ISV (Independent Software Vendors) and Startups: A Microsoft program that helps customers adopt Microsoft Cloud solutions and drive user adoption.
97 questions
{count} vote

1 answer

Sort by: Most helpful
  1. Seeya Xi-MSFT 16,471 Reputation points
    2023-02-16T02:45:54.4633333+00:00

    Hi @Vishnu Prasad-FT ,

    Please check if all the network protocols of SQL Server Configuration Manager are turned on. If they are, please go to properties again and check if the port number is 1433.

    Also, go to your firewall and check if this port is blocked.

    Best regards,

    Seeya


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.