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:

  • How to save a Docker image
  • Docker-compose error when try to start
  • Docker + MongoDB: How to view database with RoboMongo?
  • How can I safely reinstall Docker without removing volumes?
  • Docker image of Java project
  • Strategy for versioning Docker images linked to other contains via docker compose
  • 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: Error starting container: Unable to load the AUFS module
  • Hostname for website running inside Docker
  • Cannot access Google Container Registry from Google Compute Engine
  • How is the ID from the docker info generated and how stable is it?
  • Adding portMappings to Dockerrun.aws.json with Single container configuartion
  • SSH from Docker image using Drone
  • 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.