'Failed to connect to docker hosted MSSQL

I have a problem followng [this tutorial](https://hub.docker.com/r/microsoft/mssql-server-linux/ ) where I try to connect to my docker hosted MSSQL via sqlcmd.

I executed the following in PowerShell from windows:

docker run -e 'ACCEPT_EULA=Y' --name mssql -e \
 'SA_PASSWORD=yourStrong(!)Password' -p 1433:1433 -it \
 -d microsoft/mssql-server-linux:latest /bin/bash

Note: "-it" and "/bin/bash" is added due to docker will be stopped automatically if there is no any activity detected.

I ran docker container ls -a to verify it is running:

docker container Is -a 
CONTAINER ID      IMAGE                               COMMAND       CREATED          STATUS          PORTS                    NAMES 
92cfc504ab70      microsoft/mssql-server-linux:latest "/bin/bash"   27 minutes ago   Up 27 minutes   0.0.0.0:1433->1433/tcp  mssql 

I ran telnet local-ip:1433 on my host, it is working fine.

Problem lies when I do the following:

docker exec -it mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U sa \
 -P yourStrong(!)Password

Error:

Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired. Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2749. Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..

I also tried to connect in using powershell via my host Link:https://docs.microsoft.com/en-us/sql/linux/quickstart-install-connect-docker

Command:

sqlcmd -S 192.168.0.110,1433 -U SA -P yourStrong(!)Password

Note: 192.168.0.110(got this from running ipconfig in host machine.)

Any help ?



Solution 1:[1]

I found out the problems after some trials and errors, and re-reading the documents. I should use double quotes for the arguments when I executed my command in PowerShell.

I was looking into the wrong direction. Initially I executed the command:

docker run -e 'ACCEPT_EULA=Y' --name mssql -e \
 'SA_PASSWORD=yourStrong(!)Password' -p 1433:1433 -d \
 microsoft/mssql-server-linux:latest

Container stopped automatically by itself every time it starts. Then, I did some googling and found:

docker run -e 'ACCEPT_EULA=Y' --name mssql -e \
 'SA_PASSWORD=yourStrong(!)Password' -p 1433:1433 -it -d \
 microsoft/mssql-server-linux:latest /bin/bash

It seemed fine on the surface. It got executed successfully in PowerShell. It didn't stop automatically anymore.If I dig deeper using

docker container logs mssql

to see the log for mssql. No error given, just that I don't see a lots of info given, which led me to think that there were no problems in my command.

But the right way to run these commands is using double quotes. Link: https://hub.docker.com/r/microsoft/mssql-server-linux/ IMPORTANT NOTE: If you are using PowerShell on Windows to run these commands use double quotes instead of single quotes.

E.g.

docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=YourStrong!Passw0rd" -p 1401:1433 --name sql1 -d microsoft/mssql-server-linux:2017-latest

I am also able to login using SSMS with:

  • Server name: Hostip,1401
  • Username: sa
  • Password:yourpassword

Solution 2:[2]

Try 127.0.0.1 or 0.0.0.0 instead of localhost

For example :

docker exec -it mssql /opt/mssql-tools/bin/sqlcmd -S 127.0.0.1 -U sa -P 'yourStrong(!)Password'

Solution 3:[3]

docker run command syntax is the following:

Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

When you execute the command:

docker run -e 'ACCEPT_EULA=Y' --name mssql -e 'SA_PASSWORD=yourStrong(!)Password' -p 1433:1433 -it -d microsoft/mssql-server-linux:latest /bin/bash

/bin/bash in the end overrides CMD layer defined in the Dockerfile of microsoft/mssql-server-linux image.

So, just start a container without any additional command in the end:

$ docker run -e 'ACCEPT_EULA=Y' --name mssql -e 'SA_PASSWORD=yourStrong(!)Password' -p 1433:1433 -it -d microsoft/mssql-server-linux:latest

And now you are able to access a MSSQL:

$ docker exec -it mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P 'yourStrong(!)Password'
1> 

Solution 4:[4]

I'm new to Docker and I also I had the same issue when I try to connect to the SQL Server container from my application(or sqlcmd app container from Microsoft) which is also running in another Docker container. It looks like each container gets its own subnet IP address, so 'localhost' would never work if you're trying to connect to the SQL from another container. The command below will give you the full list of IP addresses in the bridge network. You can specify the IP directly in the connection string.

docker network inspect bridge

Solution 5:[5]

From your message, it looks the server is not configured to access remotely. Can you follow the way mentioned below to enable it?

Using SSMS (SQL Server Management studio): In Object Explorer, right-click a server and select Properties.

Click the Connections node.

Under Remote server connections, select the Allow remote connections to this server check box.

Thanks,

Ananda Kumar J.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stefan Crain
Solution 2
Solution 3 nickgryg
Solution 4 Sangwoo Oh
Solution 5 Ananda Kumar Jayaraman