user3909893
user3909893

Reputation: 431

Docker run container from dockerfile image error

I have the following error while trying to run:

$ docker run my-app-11
docker: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"java -jar /opt/my-app-1.0-SNAPSHOT.jar\": stat java -jar /opt/my-app-1.0-SNAPSHOT.jar: no such file or directory": unknown.

The Dockerfile I used to create an image is the following:

FROM anapsix/alpine-java

COPY target/my-app-1.0-SNAPSHOT.jar /opt/my-app-1.0-SNAPSHOT.jar

WORKDIR /opt

CMD ["java -jar /opt/my-app-1.0-SNAPSHOT.jar"]

When I enter into container and run the command it works well:

$ docker run -it my-app-11 /bin/bash
bash-4.4# java -jar /opt/my-app-1.0-SNAPSHOT.jar 
Hello World!

Upvotes: 0

Views: 615

Answers (1)

SiHa
SiHa

Reputation: 8412

From the documentation for CMD:

The CMD instruction has three forms:

  1. CMD ["executable","param1","param2"] (exec form, this is the preferred form)
  2. CMD ["param1","param2"] (as default parameters to ENTRYPOINT)
  3. CMD command param1 param2 (shell form)

And:

Note: Unlike the shell form, the exec form does not invoke a command shell. This means that normal shell processing does not happen.

Also:

If you want to run your <command> without a shell then you must express the command as a JSON array and give the full path to the executable. (emphasis mine)

So, you have a couple of issues:

  1. You are using the Exec form (because it's enclosed in brackets), but have not specified the full path to the Java executable.
  2. You have not provided the arguments as JSON array

When you enter the container, you are running a BASH shell, so the Java executable is in the path, and the command runs. This is not the case when the CMD runs as an exec, hence the failure.

I think that if you change line to:

CMD ["/path/to/java", "-jar", "/opt/my-app-1.0-SNAPSHOT.jar"]

or

CMD java -jar /opt/my-app-1.0-SNAPSHOT.jar

Either of these should work.

Upvotes: 1

Related Questions