microsoft / mssql-docker

Official Microsoft repository for SQL Server in Docker resources

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Impossible to connect to MSSQL Server 2019 on docker container

cyanezf opened this issue · comments

Please first make sure you have looked at:

Environment

To diagnose, we usually need to know the following, including version numbers. On Windows, be
sure to specify 32-bit Python or 64-bit:

  • Python: 3.11.2 64-bit

  • pyodbc: 4.0.35

  • OS: Python on Windows 10 Pro x64

  • DB: MSSQL Server 2019 on docker container - image: mssql/server:2019-latest

  • driver: ODBC Driver 17 for SQL Server

  • On Container: unixODBC 2.3.11

Issue

I have the docker container in Synology NAS (AMD 64), IP, example: 192.169.0.3 port for container 2443

  1. When I try to connect with driver:

import pyodbc
conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=192.169.0.3,2443;UID=SA;PWD=mypassword')

Error:
Traceback (most recent call last):
File "c:\Users\vm\Desktop\SQL Server\sql-connection.py", line 10, in
connection = odbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=192.169.0.3,2443;UID=SA;PWD=mypassword')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: The wait operation timed out.\r\n (258) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0); [08001] [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. (258)')

  1. When I try to connect with DSN (DSN is configured in container, MSSQLServerDatabase is the name of DSN):

import pyodbc
conn = pyodbc.connect('DSN=MSSQLServerDatabase;UID=SA;PWD=mypassword')

Error:
Traceback (most recent call last):
File "c:\Users\vm\Desktop\SQL Server\sql-connection.py", line 9, in
connection = odbc.connect('DSN=MSSQLServerDatabase;UID=SA;PWD=mypassword')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pyodbc.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')

How you can see, the odbc cannot connect, but when I connect to database with VSCode or SQL Server Management Studio I connect without problems.

Please, can you help me?

Thanks in advance!

Hi there.
Did you run the container using --network=host?
Your IP of the server seems to be in your local network. The container can not see your local network, unless you run it with the command above.
Cheers

@retooooo

I too have a similar issue so I thought I would try your suggestion, so in my Power Shell 7.3 terminal

docker run -e 'ACCEPT_EULA=Y' -e '<Password>' `
-p 1433:1433 --name sql2 --hostname sql2 --network=host`
-d `
 mcr.microsoft.com/mssql/server:2019-latest
9febe27bd08ef325f1f56ef9b47ef66ce2b5ba1afc7ce62ca91fd74235b45110
docker: Error response from daemon: network host
not found.

By contrast I have SQL1 running without the network setting, I just can't connect to it 😒

@retooooo
Do you have the firewall turned on for your NAS? I too have a Synology NAS with sql server running in a docker container and have no issues. I also run it locally for development with no issues.