0

I'm trying to make a simple MS SQL Server call from Python by using Docker. The SQL connection is able to establish if I run the python execute script, but it won't work from Docker.

My code is below

Dockerfile

from python:3

WORKDIR /code

COPY requirements.txt .


RUN apt-get update \
 && apt-get install unixodbc -y \
 && apt-get install unixodbc-dev -y \
 && apt-get install freetds-dev -y \
 && apt-get install freetds-bin -y \
 && apt-get install tdsodbc -y \
 && apt-get install --reinstall build-essential -y


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



#Pip command without proxy setting
RUN pip install -r requirements.txt

COPY src/ .

CMD ["python", "./producer.py"]

producer.py

import pyodbc



connP = pyodbc.connect('driver={FreeTDS};'
                       'server={MYSERV01\SQLEXPRESS};'
                       'database=ABCD;'                       
                       'uid=****;'
                       'pwd=*****')

requirement.txt

kafka-python
pyodbc==4.0.28

Error message enter image description here

I referred to this article and did. I searched online for resolutions and tried several steps, but nothing helped. I'm pretty new to Docker and no experience in Python, so any help would be really good. Thanks in advance!

superachu
  • 823
  • 3
  • 16
  • 34

2 Answers2

1

In your pyodbc.connect try to give the server as '0.0.0.0' instead of any other value.

If you want to debug it from inside the container, then comment the last CMD line of your Dockerfile.

Build your Docker container

docker build -f Dockerfile -t achu-docker-container .

Run your Docker Container

docker run -it achu-docker-container /bin/bash

This will place you inside the container. This is like, ssh to a different machine.

Go to your WORKDIR

cd code
python ./producer.py

What do you get the above above? (If you install any editor using apt-get install vim you will be able to interactively edit the producer.py file and fix your problem from inside the running container.

Then you can move your changes to your source Dockerfile and build a new image and container with it.

Senthil Kumaran
  • 54,681
  • 14
  • 94
  • 131
  • I did follow the steps and gave this error when ran inside the cmd. connP = pyodbc.connect('driver={FreeTDS};' pyodbc.OperationalError: ('08S01', '[08S01] [FreeTDS][SQL Server]Unable to connect: Adaptive Server is unavailable or does not exist (20009) (SQLDriverConnect)') – superachu Dec 16 '20 at 02:25
  • 1
    The FreeTDS server is not running inside the container. See if you verify in command line that hte server is running. Ref - https://stackoverflow.com/questions/8511369/adaptive-server-is-unavailable-or-does-not-exist-error-connecting-to-sql-serve – Senthil Kumaran Dec 16 '20 at 05:58
  • Thank you Mr Kumaran - with this link and so many other articles I was able to figure it out. Posted the answer below. – superachu Jan 04 '21 at 07:24
1

I was trying to connect to the local SQL Server database. I referred many articles and figured out that the following code works:

the server should have host.docker.inter,<port_no> -- this was the catch. When it comes to dedicated database where the sql server is different from the docker image, the server name is provided directly, but when both image and database are in same server, the following code works. Please check the port number in the SQL Configuration TCP Address (IP4All)

enter image description here

superachu
  • 823
  • 3
  • 16
  • 34