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:

  • Fail join node to docker swarm
  • How to upgrade Openshift Origin Docker to latest version?
  • How to copy external project, get data (files) of it's test branch, execute it's pom.xml, execute it's .war or .jar with Dockerfile
  • Docker base image running slowly Mac OS X
  • Eclipse Che does not start: 'java.lang.ClassNotFoundException org.apache.juli.ClassLoaderLogManager'
  • advantages VM's have over lightweight containers (docker) [closed]
  • 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.

  • Docker Django 404 for web static files, but fine for admin static files
  • Is it possible to do parallel imports for predictionIO?
  • How to replace memswap_limit in docker compose 3?
  • docker boto3 AWS was not able to validate the provided access credentials
  • Using docker-compose, how do I share my image to docker-hub?
  • Docker with shared X11 socket: Why can it “start” Firefox outside of the container?
  • 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.