0

I cannot figure out the correct way of arranging the configuration files needed to connect to an external database from a Docker container. The whole thing is running on Windows, and the Docker is Debian, and I'm using FreeTDS as per this tutorial to make a connection. I've also been following suggestions in this SO question.

With the very helpful Flask debugger, I'm able to tell that I'm connecting to the server with the following connection string:

'DSN=server_name; DATABASE=db_name; UID=usr; PWD=pw'

These are the contents of my configuration files:

# odbcinst.ini in /etc/ and /
[FreeTDS]
Description = FreeTDS driver
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so

# odbc.ini in /etc/ and /root/.odbc.ini
[data_source]
Driver = FreeTDS
Servername = server_name
Port = 1433
Database = db_name

# freetds.conf in /etc/freetds/
[global]
tds version = 7.4

[server_name]
host = server_ip
port = 1433

[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnect)

However, I can connect to the database with the command line using tsql -H server_ip -p 1433 -U usr -P pw. isql -v data_source username pwd works too. osql gives the following error:

path/libtdsodbc.so is not an executable file

osql: error: no driver found for data_source

I can't figure out the problem.. Doesn't help I'm pretty inexperienced with Linux as well. Any help is greatly appreciated!

Community
  • 1
  • 1
Felix
  • 2,548
  • 19
  • 48
  • Is your database dockerized as well ? When you say you're able to connect to the database, is it from within the Docker container ? – Mornor Aug 14 '18 at 13:05
  • @Mornor I see my explanation was very ambigous, fixed now. Sorry. – Felix Aug 14 '18 at 13:11
  • Did you expose the port `:1433` of the Docker image? So, in a Dockerfile: `EXPOSE 1433` and the Docker command: `docker run -p 1433:1433 [...] ` – Mornor Aug 14 '18 at 13:28
  • Could the down- and closevoter provide some insight as to how might this post be improved? Or is it not on topic? – Felix Aug 14 '18 at 13:36
  • @Mornor Exposed, didn't help.. – Felix Aug 14 '18 at 13:41

1 Answers1

0

I managed to figure it out with the help of this answer on the linked question. It wasn't a straight-out fix, because the configuration is still not working, but it's a bypass and good enough.

The trick was to call pyodbc.connect with keyword arguments instead of a connection string. To copy from the link:

import pyodbc

conn = pyodbc.connect(
    driver = 'FreeTDS',
    TDS_Version = '7.4',
    server = '<hostname or ip address>',
    port = 1433,
    database = '<database>',
    uid = '<uid>',
    pwd = '<pwd>'
)

The fix was particularly strange as I don't think the official Wiki for PyODBC mentions the keyword argument version at all.

In addition, now only the odbcinst.ini file is needed for configuration, so that saves at least some hassle with the container build.

Felix
  • 2,548
  • 19
  • 48