0

I am trying to create 3 containers and have them run all on the same network, but I am struggling with my Python Program to establish a successful connection to a SQL server on any of these containers.

To describe my current work flow:

I have one Microsoft SQL Image Container called server_1. I have another Microsoft SQL Image Container called server_2. Finally, I have another container called python-connect containing a Python Image called python-file.

To show you how I created each of these containers this is what I did.

First, I created a network called databases-net as follows:

docker network create --driver bridge databases-net

Next, I proceeded to create two containers with SQL servers:

docker run -dit -d --name server_1 --network databases-net -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=StrongPassword123#' -p 1433:1433 mcr.microsoft.com/mssql/server:2019-latest
docker run -dit -d --name server_1 --network databases-net -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=StrongPassword123#' -p 1002:1433 mcr.microsoft.com/mssql/server:2019-latest

Then, I created a DockerFile as shown below for my Python Program:

FROM python:3.9.2-slim-buster

ADD dbconnTest.py .

RUN apt-get update \
  && apt-get -y install gcc \
  && apt-get -y install g++ \
  && apt-get -y install unixodbc unixodbc-dev \
  && apt-get clean

RUN pip install pandas pyodbc

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

This is the python program I am using. Currently, I am only trying to access the data inside the server_1 container:

import pandas as pd
import pyodbc

cnxn = pyodbc.connect(
       'DRIVER={ODBC Driver 17 for SQL Server}' +
       ';SERVER=' + 'localhost, 1433' + ';UID=' + 'sa' +
       ';PWD=' + 'StrongPassword123#' +
       ';database=' + 'tempdb')

df = pd.read_sql('SELECT * FROM Addresses', cnxn)

cnxn.commit()
cnxn.close()

print(df)

To build this image and running it on a container I ran:

docker build -t python-file .
docker run -dit --name python-connect --network databases-net python-file

Once it was run, I got this error:


Traceback (most recent call last):

  File "//./dbconnTest.py", line 4, in <module>

    cnxn = pyodbc.connect(

pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server' : file not found (0) (SQLDriverConnect)")

It seems like the python program is not able to access any of the SQL containers I created although they are on the same network. I am quite new to Docker and after looking up many tutorials, I did not see anyone try doing this. Is it even possible to access SQL servers on containers separate from the container the python program is on? If so, how do you do so?

I've been stuck on this for a week, and would really appreciate all the help I can get on this effort. Thanks!

Mohit Jain
  • 87
  • 1
  • 1
  • 5
  • Hi, the error says clearly that you are missing the odbc driver to connec to your database. the issue has nothing to deal with your containers connectivity. try to install python odbc dependencies in your docker file – Djebarri Amazigh Oct 29 '21 at 08:13
  • there is another similar post to yours, take a look at it: [stackoverfow post](https://stackoverflow.com/a/46446438/11231474) – Djebarri Amazigh Oct 29 '21 at 08:27
  • Once you get your ODBC driver installation sorted out... `localhost,1433` will only work like that if you have an SQL Server instance installed there inside the Python Docker container. If you think of containers as a kind of VM guest then it will make sense that `localhost` only points to the container itself. Try `host.docker.internal,1433` instead, which will connect to open ports on the Docker host machine. – AlwaysLearning Oct 29 '21 at 08:37
  • Since the containers are all on the same `docker run --net` you can use the other containers' `docker run --name` as host names; `server_1,1433` and `server_2,1433`. This works even if `host.docker.internal` isn't defined (on native Linux) or if the `docker run -p` published ports are different or missing. – David Maze Oct 29 '21 at 13:24

0 Answers0