Running java command with Docker exec fails with “no such file or directory”

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:

  • docker ubuntu image uname result
  • Postgres Error running query on database: Could not detect default username
  • “Address already in use” error upon docker-compose up
  • Error when running SparkApp from docker container against Spark running in another container
  • Deploying new versions of an image instantly
  • How to run custom Docker file?
  • 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.

  • supervisor curl command failing with exit 56 error
  • Confused about Docker 1.12 types
  • Docker: Refer to registry by ip address
  • How to access local file when building from Dockerfile?
  • How do I mount a virtual machine folder on a docker container? [closed]
  • Docker architecture for hosting a PHP website
  • One Solution collect form web for “Running java command with Docker exec fails with “no such file or directory””

    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.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.