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
2
  • Have you tried this outside of docker? Check this post that has a detailed explanation for this error: stackoverflow.com/questions/33158503/… Commented Feb 19, 2018 at 4: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)') Commented Feb 19, 2018 at 20:28

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

11 Comments

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.
Hmm yeah this is quite tricky. This question might help you: stackoverflow.com/questions/10056560/…. If it doesn't help feel free to ask another question with your code so far and the exact error message.
The Microsoft help page is also pretty good, yet very detailed: learn.microsoft.com/de-de/sql/database-engine/configure-windows/…
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.
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 learn.microsoft.com/de-de/sql/relational-databases/scripting/…
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.