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!