Home:ALL Converter>Connecting to SQL Server through Python in Docker

Connecting to SQL Server through Python in Docker

Ask Time:2018-02-15T00:08:14         Author:dfader2

Json Formatter

I've been attempting to setup a Docker container/image which can connect to a SQL Server db, based on Luis' pyodbc repo found here: https://hub.docker.com/r/lbosqmsft/mssql-python-pyodbc/

I am running Docker toolbox on a Window's PC, and have tested running the Python code locally on the PC and verified that it can establish a connection:

Connecting to server
DRIVER={ODBC Driver 13 for SQL Server};SERVER=$SERVER_NAME;DATABASE=$DB_NAME;UID=$SERVICE_ID;PWD=$PASSWORD
Got connection

From within the (Ubuntu) container, I have tested the connection using sqlcmd and been able to successfully connect and run queries without issue:

root:/app# sqlcmd -S $SERVER_NAME,$PORT -U $SERVICE_ID -P $PASSWORD
1> select SYSTEM_USER
2> GO

----------------------------------------------------------------------------------------------------------------------------
$SERVICE_ID

But for some reason, when I run the Python code from within the container, the connection will not get established and fails with a timeout error:

Connecting to server
DRIVER={ODBC Driver 13 for SQL Server};SERVER=$SERVER_NAME;DATABASE=$DB_NAME;UID=$SERVICE_ID;PWD=$PASSWORD
WARNING:root:Database connection error. ('HYT00', u'[HYT00] [unixODBC][Microsoft][ODBC Driver 13 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')

I have verified that both connection strings are identical, and I am attempting to connecting use an SQL service ID (not Windows authentication). I have also verified that for the established Windows connection, it is indeed using the service id (by running a select SYSTEM_USER query).

Has anyone else run into similar behavior?

Author:dfader2,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/48791520/connecting-to-sql-server-through-python-in-docker
yy