Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 918
csamleong Avatar asked Apr 06 '18 06:04

csamleong


People also ask

How do I connect to Microsoft SQL Server Docker?

Here are the steps you can follow to set up and deploy a SQL Server Docker Container seamlessly: SQL Server Docker Setup: Install Docker on your System. SQL Server Docker Setup: Execute and Run Docker. SQL Server Docker Setup: Pull & Run the Docker Image for SQL Server.

How do I know if SQL Server is running Docker?

You can look at the SQL Server setup and error logs in /var/opt/mssql/log. If the container isn't running, first start the container. Then use an interactive command-prompt to inspect the logs. You can get the container ID by running the command docker ps .

How do I allow remote connections to SQL Server?

Using SQL Server Management Studio In Object Explorer, right-click a server and select Properties. Select the Connections node. Under Remote server connections, select or clear the Allow remote connections to this server check box.

Can SQL Server run on Docker?

However, in order to install and use SQL Server on a Mac, you need to run the Linux distribution inside a docker container. This requires the SQL Server Docker image to be downloaded and installed on the local computer before using it.


2 Answers

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
like image 110
csamleong Avatar answered Oct 03 '22 09:10

csamleong


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'
like image 34
Willie Cheng Avatar answered Oct 03 '22 11:10

Willie Cheng