0

我正在尝试使用 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.

为什么会存在这样的差异?

4

2 回答 2

0

根据@Hajed.Kh 的回答,我尝试了这个并且成功了:

    String[] commands = {
            "/bin/sh",
            "-c",
            command
    };
于 2021-03-17T09:34:20.830 回答
0

连接字符串的语法对PostgreSQL/PostGIS很重要,可能问题出在 Process 实例Java Runtime.exec中。PG 命令必须以与您直接在容器中相同的方式运行,这应该适合您:

String connection = "\"host=host.docker.internal user=postgres dbname=test password=postgres\"" ;
String[] commands = {"bash","ogrinfo","PG:"+connection};
Process process = Runtime.getRuntime().exec(commands);
于 2021-03-16T14:48:34.037 回答