Docker Community Forums

Share and learn in the Docker community.

Connection to sql server using pyodbc failing


(Nischaygowda) #1


I am trying to connect a container to sql server. (which is physically present outside)

I get this error saying-

Microsoft ODBC driver 13 for SQL server- Login timeout expired (SQLDriver Connect)

When I try to run the same program outside the docker, it runs fine.

Has anyone come across this issue? Any help would be appreciated.


Here is the sample python code:

import pyodbc
server = '$DB_HOST'
username = '$DB_USERNAME'
password = '$DB_PASSWORD'

cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL 
Server};SERVER='+server+';PORT=1443;UID='+username+';PWD='+ password)
cursor = cnxn.cursor()

print ('Using the following SQL Server version:')
tsql = "SELECT @@version;"
with cursor.execute(tsql):
    row = cursor.fetchone()
    print (str(row[0]))

(Sam) #2

where did the container get the ip address of the sql server?

and the sql server will allow this machine to connect (firewall issues)…

can u ping the sql server from the container?

(Nischaygowda) #3

Hi @sdetweil, Thanks for your reply.
I have sourced all the 3 the environment variables in the container.
I tried pinging the sql server from the container, it did not respond.
However, pinging from the container worked.

If it was firewall issues, I was able to ping SQL server outside the container (ubuntu terminal) and it worked fine,

(Sam) #4

what kind of system is the docker host, linux, mac, windows?

but at least we know it is a networking issue…

(Nischaygowda) #5

My main OS is windows10. I am running Oracle VM VirtualBox on Ubuntu.
On Ubuntu 16.04, I have installed docker. So the docker host is Ubuntu or windows?
Bit confused.

(Sam) #6

where do you issue docker run command, that is the docker host.
where is the database server? that is the db host