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

xiozqbni  于 12个月前  发布在  Docker
关注(0)|答案(1)|浏览(206)

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.

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.

hjqgdpho

hjqgdpho1#

I had the same problem. The workaround to "solve" is disable IPV6 on all adapters.

相关问题