I have a Python code that’s running properly on my system (Mac OS Catalina) but is failing when I am using it in my docker image. I am open to having a completely new dockerfile as well if that can work.
import pandas as pd import jaydebeapi import argparse import json from datetime import datetime import os def read_data(): MSSQL_DRIVER = "net.sourceforge.jtds.jdbc.Driver" host = 'server_name' port = '1433' user = 'user' password = 'password' db_url = f"jdbc:jtds:sqlserver://{host}:{port};" connection_properties = { "user": user, "password": password } jar_path = './jtds-1.3.1.jar' connection = jaydebeapi.connect(MSSQL_DRIVER, db_url, connection_properties, jar_path) query = 'SELECT TOP 10 * FROM table_name;' data = pd.read_sql_query(query,connection) print(data) connection.close() if __name__ == "__main__": read_data()
I have the jar file next to my code so it can be picked up properly.
Here is my dockerfile:
FROM alpine:3.7 RUN apk update && apk upgrade && apk add --no-cache bash && apk add --no-cache --virtual=build-dependencies unzip && apk add --no-cache curl && apk add --no-cache openjdk8-jre RUN apk add --no-cache python3 && python3 -m ensurepip && pip3 install --upgrade pip setuptools && rm -r /usr/lib/python*/ensurepip && if [ ! -e /usr/bin/pip ]; then ln -s pip3 /usr/bin/pip ; fi && if [[ ! -e /usr/bin/python ]]; then ln -sf /usr/bin/python3 /usr/bin/python; fi && rm -r /root/.cache RUN apk add make automake gcc g++ subversion python3-dev RUN pip install --trusted-host pypi.python.org flask ENV JAVA_HOME="/usr/lib/jvm/java-1.8-openjdk" EXPOSE 8000 WORKDIR /usr/src/app COPY requirements.txt . RUN pip install -r requirements.txt COPY jtds-1.3.1.jar . COPY server.py . CMD ["python", "server.py"]
The error that I am getting is:
Error occurred during initialization of VM Unable to load native library: Error loading shared library libjvm.so: No such file or directory (needed by /usr/lib/jvm/java-1.8-openjdk/jre/lib/amd64/libjava.so)
Please suggest me better dockerfile that I can use. Thanks for the help 🙂
Advertisement
Answer
Found the solution, all I needed to do was to add the following line to my dockerfile.
ENV LD_LIBRARY_PATH="/usr/lib/jvm/java-1.8-openjdk/jre/lib/amd64/server"
The rest is the same and it worked like a charm! I tried working with pymssql
, pyodbc
(FreeTDS one) and nothing seemed to work for me.