Python 脚本无法访问 Docker 图像中的 .jar 文件
Python script can't reach .jar file inside Docker image
我正在尝试 docker 化我创建的一些 Flask 应用程序。
我需要在我创建的 Python 脚本中使用 startJVM() 来访问 java 文件。
下面的代码在我的本地终端上运行良好,它可以检测到扩展名为“.jar”的文件的路径。
ZEMBEREK_PATH = os.path.abspath("zemberek-full.jar")
startJVM(getDefaultJVMPath(), '-ea', f'-Djava.class.path={ZEMBEREK_PATH}', convertStrings=False)
但是当我在docker图像中运行这个时,我猜测路径变量找不到扩展名为“.jar”的文件,所以程序报错。
Traceback (most recent call last):
File "app.py", line 28, in <module>
startJVM(getDefaultJVMPath(), '-ea', f'-Djava.class.path={ZEMBEREK_PATH}', convertStrings=False)
File "/usr/local/lib/python3.7/site-packages/jpype/_core.py", line 337, in getDefaultJVMPath
return finder.get_jvm_path()
File "/usr/local/lib/python3.7/site-packages/jpype/_jvmfinder.py", line 160, in get_jvm_path
jvm = method()
File "/usr/local/lib/python3.7/site-packages/jpype/_jvmfinder.py", line 215, in _get_from_known_locations
for home in self.find_possible_homes(self._locations):
File "/usr/local/lib/python3.7/site-packages/jpype/_jvmfinder.py", line 120, in find_possible_homes
for childname in sorted(os.listdir(parent)):
FileNotFoundError: [Errno 2] No such file or directory: '/usr/lib/jvm'
我检查了 Docker 图像中的文件。 "zemberek-full.jar" 无缝包含在图像中。
你们有什么解决方案可以解决我遇到的这个问题吗?
提前感谢所有花时间解决这个问题的人。 :)
这是我的Docker文件
FROM python:3.7
ARG DEBIAN_FRONTED=noninteractive
RUN apt-get update && apt-get install -y --no-install-recommends apt-utils > /dev/null
RUN apt-get install -y build-essential tcl
RUN apt-get install -y systemd-sysv
RUN apt-get update > /dev/null
RUN apt-get install -y wget > /dev/null
RUN apt-get install -y zip > /dev/null
RUN apt-get install -y libaio1 > /dev/null
RUN apt-get update > /dev/null
RUN apt-get install -y alien > /dev/null
WORKDIR /
RUN mkdir /app
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
您的 Docker 实例中没有安装 JVM。将此行添加到您的 Docker 文件以进行安装:
RUN apt-get install -y openjdk-11-jdk;
我正在尝试 docker 化我创建的一些 Flask 应用程序。
我需要在我创建的 Python 脚本中使用 startJVM() 来访问 java 文件。 下面的代码在我的本地终端上运行良好,它可以检测到扩展名为“.jar”的文件的路径。
ZEMBEREK_PATH = os.path.abspath("zemberek-full.jar")
startJVM(getDefaultJVMPath(), '-ea', f'-Djava.class.path={ZEMBEREK_PATH}', convertStrings=False)
但是当我在docker图像中运行这个时,我猜测路径变量找不到扩展名为“.jar”的文件,所以程序报错。
Traceback (most recent call last):
File "app.py", line 28, in <module>
startJVM(getDefaultJVMPath(), '-ea', f'-Djava.class.path={ZEMBEREK_PATH}', convertStrings=False)
File "/usr/local/lib/python3.7/site-packages/jpype/_core.py", line 337, in getDefaultJVMPath
return finder.get_jvm_path()
File "/usr/local/lib/python3.7/site-packages/jpype/_jvmfinder.py", line 160, in get_jvm_path
jvm = method()
File "/usr/local/lib/python3.7/site-packages/jpype/_jvmfinder.py", line 215, in _get_from_known_locations
for home in self.find_possible_homes(self._locations):
File "/usr/local/lib/python3.7/site-packages/jpype/_jvmfinder.py", line 120, in find_possible_homes
for childname in sorted(os.listdir(parent)):
FileNotFoundError: [Errno 2] No such file or directory: '/usr/lib/jvm'
我检查了 Docker 图像中的文件。 "zemberek-full.jar" 无缝包含在图像中。
你们有什么解决方案可以解决我遇到的这个问题吗?
提前感谢所有花时间解决这个问题的人。 :)
这是我的Docker文件
FROM python:3.7
ARG DEBIAN_FRONTED=noninteractive
RUN apt-get update && apt-get install -y --no-install-recommends apt-utils > /dev/null
RUN apt-get install -y build-essential tcl
RUN apt-get install -y systemd-sysv
RUN apt-get update > /dev/null
RUN apt-get install -y wget > /dev/null
RUN apt-get install -y zip > /dev/null
RUN apt-get install -y libaio1 > /dev/null
RUN apt-get update > /dev/null
RUN apt-get install -y alien > /dev/null
WORKDIR /
RUN mkdir /app
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
您的 Docker 实例中没有安装 JVM。将此行添加到您的 Docker 文件以进行安装:
RUN apt-get install -y openjdk-11-jdk;