我正在尝试使用 GDAL 包(用于 ogr2ogr 命令)对 Java 应用程序进行 dockerize。
我的 Dockerfile 是:
FROM openjdk:10
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
RUN apt-get update
RUN apt-get -y install python3-gdal
RUN apt-get -y install libgdal28
RUN apt-get -y install libgdal-perl-doc
RUN apt-get -y install libgdal-perl
RUN apt-get -y install libgdal-dev
RUN apt-get -y install gdal-data
RUN apt-get -y install gdal-bin
CMD ["java", "-jar", "/app.jar"]
在容器上运行的 Java 代码中有一个片段:
String command = "ogrinfo PG:\"host=host.docker.internal user=postgres dbname=test password=postgres\"";
Process process = Runtime.getRuntime().exec(command);
然后,输出为:
Unable to open datasource `PG:"host=host.docker.internal' with the following drivers.
但是,当我尝试直接从 bash 对容器运行命令时,它会成功:
root@7b5fb10431cf:/# ogrinfo PG:"host=host.docker.internal user=postgres dbname=test password=postgres"
INFO: Open of `PG:host=host.docker.internal user=postgres dbname=test password=postgres'
using driver `PostgreSQL' successful.
为什么会存在这样的差异?