Home > Software engineering >  Connect to MSSQL using python jaydebeapi from inside a Docker Image
Connect to MSSQL using python jaydebeapi from inside a Docker Image

Time:05-06

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 :)

CodePudding user response:

Are you running this on an apple silicon mac?

When I build your image, (i'm using an apple silicon mac), I am able to verify that /usr/lib/jvm/java-1.8-openjdk/jre/lib/amd64/libjava.so does not exist. It's instead in /usr/lib/jvm/java-1.8-openjdk/jre/lib/aarch64.

However, if I build the image and set the platform to amd like

FROM --platform=linux/x86_64  alpine:3.7
RUN apk update \
&& apk upgrade \
...

the file is there. I suspect this is your issue as well

alex@laptop ~/r/_/docker_test> docker run --entrypoint /bin/sh -it <image>
WARNING: The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested
/ # ls /usr/lib/jvm/java-1.8-openjdk/jre/lib/amd64
jli                  libj2gss.so          libjawt.so           libnpt.so
jvm.cfg              libj2krb5.so         libjdwp.so           libsplashscreen.so
libattach.so         libj2pcsc.so         libjsdt.so           libsunec.so
libawt.so            libj2pkcs11.so       libjsig.so           libunpack.so
libawt_headless.so   libj2sctp.so         libjsound.so         libverify.so
libawt_xawt.so       libjaas_unix.so      libjsoundalsa.so     libzip.so
libdt_socket.so      libjava.so           libmanagement.so     server
libfontmanager.so    libjava_crw_demo.so  libmlib_image.so
libhprof.so          libjavajpeg.so       libnet.so
libinstrument.so     libjavalcms.so       libnio.so

CodePudding user response:

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.

  • Related