5

I'm using the Docker Java client in a Scala project to programmatically create images and start containers with volume(s), then when this is all done also execute a Java class within a jar file that is available in one of the attached volumes.

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

The command I'm trying to run:

val command = s"""bash -c "java -cp /opt/dependencies/Platforms-assembly-0.2.4.jar com.org.test.platforms.common.Endpoint param1 param2}""""

The jar file referenced in the command is available in the volume that was bound at container creation.

I tried running different versions of the command, for example as simple Java command (without bash -c), I also tried different location like the file's location on my local PC or the path on the machine where Docker is running, with the same result sadly.

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

The error I'm getting:

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

If I copy the aforementioned command, attach to the running container and run it, then it executes perfectly, which is the main reason why I'm lost on this issue. It is my understanding that the volume should be available at the time of the initial execution, it's certainly available when I attach to the container.

Lii
  • 11,553
  • 8
  • 64
  • 88
barney.balazs
  • 630
  • 7
  • 19
  • Did you install java inside your container ? and does /opt/dependencies/Platforms-assembly-0.2.4.jar exists inside the container ? – Wee Jun 30 '17 at 10:01
  • Yes, `java` is installed and the JAR file is available on that location. – barney.balazs Jun 30 '17 at 10:11

1 Answers1

1

Running java directly would assume it's in the $PATH, which isn't usually a safe assumption.

A safer approach would be to use the fully qualified path, e.g., /usr/bin/java.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • I didn't copy my Dockerfile definition here, but `java` is actually available in the `$PATH`, so sadly this did not fix my issue. Also I'm guessing that if this was the issue then the same command would not work from inside the container either, no? – barney.balazs Jun 30 '17 at 09:25
  • I'm also struggling with the same problem. I tried you suggestion, but Java in not present in `/usr/bin` or any normal bin-dir. Java is installed in this dir: `/layers/paketo-buildpacks_bellsoft-liberica/jre`. But I don't want to use that path; it seem implementation specific and prone to change. In the Docker file this command works: `ENTRYPOINT [ "java", ... ]`. Do you know how Java is located by that command? – Lii Jun 11 '21 at 13:39