2

I am having a really hard time getting my docker container to access the MS SQL server. I have tried following this guide here, but to no avail.

Here is my Base Dockerfile:

FROM python:3
ADD ./odbcinst.ini /etc/odbcinst.ini
RUN apt-get update && apt-get install gcc
RUN apt-get install -y tdsodbc unixodbc-dev
RUN apt-get install unixodbc-bin -y
RUN apt-get clean -y
RUN apt-get update && apt-get install -y gcc unixodbc-dev mssql-python-pyodbc

RUN pip install pyodbc
RUN pip install plaster_pastedeploy pyramid pyramid_jinja2 pyramid_debugtoolbar waitress yagmail pyodbc

And here is the other docker file that extends it:

FROM pyodbc
COPY . .
RUN pip install -e companalysis/
CMD [ "pserve", "companalysis/development.ini" ]
EXPOSE 8081

and here is my sql connection string:

strconn = 'DRIVER=
{FreeTDS};SERVER=192.168.0.6;
DATABASE=xxxx;UID=xxxx;PWD=xxx'

and no matter what I do I get this error:

Error: ('01000', u"[01000] [unixODBC][Driver Manager]Can't open lib 'FreeTDS' : file not found (0) (SQLDriverConnect)")

I would love some assistance on this. EDIT: I have gotten it to install with this in my dockerfile:

I got it to properly install the driver with this in my dockerfile:

RUN echo "[FreeTDS]\n\
Description = FreeTDS unixODBC Driver\n\
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so\n\
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so" >> /etc/odbcinst.ini
RUN export PYMSSQL_BUILD_WITH_BUNDLED_FREETDS=1
RUN apt-get update && apt-get install -y unixodbc unixodbc-dev freetds-dev freetds-bin tdsodbc
Marcus Mann
  • 321
  • 2
  • 12
  • Have you tried this outside of docker? Check this post that has a detailed explanation for this error: https://stackoverflow.com/questions/33158503/pyodbc-cant-find-freetds-driver/46793344#46793344 – sxm1972 Feb 19 '18 at 04:19
  • I have the docker container throwing a new error now, after successfully (I think) installing FreeTDS. Here is the error: `pyodbc.OperationalError: ('08001', '[08001] [unixODBC][FreeTDS][SQL Server]Unable to connect to data source (0) (SQLDriverConnect)')` – Marcus Mann Feb 19 '18 at 20:28

1 Answers1

2

I solved this issue by editing /etc/odbcinst.ini like so:

RUN echo "[FreeTDS]\n\
Description = FreeTDS unixODBC Driver\n\
Driver = /usr/lib/arm-linux-gnueabi/odbc/libtdsodbc.so\n\
Setup = /usr/lib/arm-linux-gnueabi/odbc/libtdsS.so" >> /etc/odbcinst.ini

Paths to libtdsodbc.so and libtdsS.so might be different depending on your architechture, as already pointed out in the linked questions.

If in doubt about the paths run the docker container, get into it (docker exec -ti <hash> bash) and search for the correct path.


Depending on your OS and architecture you could also use the Microsoft ODBC driver instead of the FreeTDS driver. For detailed instructions see the docs.

Phonolog
  • 6,321
  • 3
  • 36
  • 64
  • This got me a bit further along, but I'm now having trouble connecting to the actual database. I have it hooked up via an SSH tunnel, and I can access it from my SSMS, but can't actually connect from pyodbc from inside the docker container. – Marcus Mann Feb 19 '18 at 20:39
  • Hmm yeah this is quite tricky. This question might help you: https://stackoverflow.com/questions/10056560/connect-to-remote-sql-database-using-excel. If it doesn't help feel free to ask another question with your code so far and the exact error message. – Phonolog Feb 20 '18 at 07:34
  • The Microsoft help page is also pretty good, yet very detailed: https://learn.microsoft.com/de-de/sql/database-engine/configure-windows/troubleshoot-connecting-to-the-sql-server-database-engine – Phonolog Feb 20 '18 at 07:36
  • I have putty forwarding 1433 from the remote host to 127.0.0.2:1433, and it works with SSMS, so I am not sure why it isn't working with my Docker container. I have tried pretty much every verison of `TDS_VERSION` to no avail. – Marcus Mann Feb 20 '18 at 14:49
  • I'm connecting to my server like so: `pyodbc.connect(driver='{FreeTDS}', server='144.66.144.52\SQLEXPRESS', uid='sa', pwd='...')`. If you're not sure about the connection string you can try to connect via sqlcmd https://learn.microsoft.com/de-de/sql/relational-databases/scripting/sqlcmd-connect-to-the-database-engine – Phonolog Feb 20 '18 at 15:01
  • Maybe start of from the machine where the SQL server is running, then try it from your remote machine... – Phonolog Feb 20 '18 at 15:01
  • Specifing port 1433 should not be necessary as it's the standard port... – Phonolog Feb 20 '18 at 15:04
  • Ok, I'll give it a shot without specifying the port – Marcus Mann Feb 20 '18 at 15:32
  • When I don't specify the port, it gives me this error: `pyodbc.OperationalError: ('08001', '[08001] [unixODBC][FreeTDS][SQL Server]Unable to connect to data source (0) (SQLDriverConnect)')` – Marcus Mann Feb 20 '18 at 15:35
  • It keeps giving me this error when the string is properly formatted... `pyodbc.OperationalError: ('08S01', '[08S01] [unixODBC][FreeTDS][SQL Server]Unable to connect: Adaptive Server is unavailable or does not exist (20009) (SQLDriverConnect)')` – Marcus Mann Feb 20 '18 at 15:48
  • I'd still advocate for creating a new question. Add the connection strings you tried and everything else you've checked. Like: Have you tried to ping the machine with the SQL server? Have you made sure the instance is running on port 1433? – Phonolog Feb 21 '18 at 09:07