从 Docker 图像中使用 python jaydebeapi 连接到 MSSQL

Connect to MSSQL using python jaydebeapi from inside a Docker Image

我有一个 Python 代码,它在我的系统上 运行 是正确的(Mac OS Catalina),但是当我在我的 [=26] 中使用它时失败了=] 图片。如果可行的话,我也愿意拥有一个全新的 docker 文件。

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

我的代码旁边有 jar 文件,因此可以正确获取它。

这是我的docker文件:

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"]

我得到的错误是:

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)

请建议我更好的 docker我可以使用的文件。感谢您的帮助:)

你运行这是在苹果硅片上吗mac?

当我构建您的映像时(我使用的是苹果硅 mac),我能够验证 /usr/lib/jvm/java-1.8-openjdk/jre/lib/amd64/libjava.so 不存在。它位于 /usr/lib/jvm/java-1.8-openjdk/jre/lib/aarch64.

但是,如果我构建镜像并将平台设置为 amd 之类的

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

文件在那里。我怀疑这也是你的问题

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

找到解决方案,我需要做的就是将以下行添加到我的 dockerfile 中。

ENV LD_LIBRARY_PATH="/usr/lib/jvm/java-1.8-openjdk/jre/lib/amd64/server"

其余的都是一样的,而且效果很好!我尝试使用 pymssqlpyodbc(FreeTDS 之一),但似乎没有任何效果。