我在 Scala 项目中使用Docker Java 客户端以编程方式创建图像并使用卷启动容器,然后当这一切完成后,还在附加卷之一中可用的 jar 文件中执行 Java 类。
// Constructing the container and getting it's ID
val containerID = dockerClient
.createContainerCmd(imageID)
.withName(s"${namePrefix}_${RandomStringUtils.randomAlphanumeric(12)}")
.withTty(true)
.withVolumes(volume)
.withBinds(new Bind("/home/core/docker-dependencies", new Volume("/opt/dependencies")))
.exec()
.getId
// Starting the container
dockerClient
.startContainerCmd(containerID)
.exec()
我试图运行的命令:
val command = s"""bash -c "java -cp /opt/dependencies/Platforms-assembly-0.2.4.jar com.org.test.platforms.common.Endpoint param1 param2}""""
命令中引用的 jar 文件在容器创建时绑定的卷中可用。
我尝试运行不同版本的命令,例如简单的 Java 命令(不bash -c
带
// Preparing the command for execution
val executionID = dockerClient
.execCreateCmd(containerID)
.withCmd(command)
.exec()
.getId
// Starting the execution
dockerClient
.execStartCmd(executionID)
.withTty(true)
.exec(new ExecStartResultCallback(System.out, System.err))
.awaitCompletion()
我得到的错误:
com.github.dockerjava.api.exception.NotFoundException: {"message":"rpc error: code = 2 desc = oci runtime error: exec failed: exec: \"bash -c \\\"java -cp /opt/dependencies/Platforms-assembly-0.2.4.jar com.org.test.platforms.common.Endpoint param1 param2\\\"\": stat bash -c \"java -cp /opt/dependencies/Platforms-assembly-0.2.4.jar com.org.test.platforms.common.Endpoint param1 param2\": no such file or directory"}
如果我复制上述命令,附加到正在运行的容器并运行它,那么它会完美执行,这就是我在这个问题上迷失的主要原因。据我了解,该卷在初始执行时应该可用,当我附加到容器时它肯定可用。