Docker Community Forums

Share and learn in the Docker community.

OpenJDK cant deploy the simplest java app


(Fortblox) #1

HI, I’m new to dockers and I wanted to play around with a docker.
So far I have failed miserably…

I have a very simple app.jar file and this is my simple Dockerfile:

FROM openjdk:8-jre
COPY . var/www/app
WORKDIR var/www/app
CMD ["java", "-jar", "app.jar"]

running

docker build -t java-app .

Image built successfully.
When executing

docker run -i -t java-app

I get the following error

Error response from daemon: OCI runtime create failed: container_linux.go:295: starting container process caused "exec: \"java\": executable file not found in $PATH": unknown.

I just followed simple instructions I found on the web. If I understand the error correctly, java isn’t recognised as a command despite Extending the OpenJDK image. Any help?


(Cs02rm0) #2

I think your Dockerfile is fine, seems to work for me.

Might be worth trashing your docker data (preferences->reset->remove all data / restart).


(Fortblox) #3

Silly me, I didn’t realize the openjdk image is just an online installation of java.
I thought it’s an offline java image. My apt wasn’t configured, so nothing worked.
Thanks for the reply.
Close.


#4

I have the same problem. I tried to change the versions of jdk in the docker file to align with the version in my environment variables. But I still couldnt solve the problem.

Can you clarify “My apt wasn’t configured, so nothing worked.”?


(Gary Forghetti) #5

Post your Dockerfile here and the commands you are using to build and run your container.

🐳  gforghetti:[~/Downloads] $ cat Dockerfile
FROM openjdk:8-jre
CMD ["java", "-version"]
🐳  gforghetti:[~/Downloads] $ docker image build -t test .
Sending build context to Docker daemon  13.82kB
Step 1/2 : FROM openjdk:8-jre
 ---> ed287c436e66
Step 2/2 : CMD ["java", "-version"]
 ---> Using cache
 ---> ff8a149634df
Successfully built ff8a149634df
Successfully tagged test:latest
🐳  gforghetti:[~/Downloads] $ docker container run -it test
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-8u181-b13-2~deb9u1-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)
🐳  gforghetti:[~/Downloads] $

#6

Here is the Docker file:

FROM alpine/git as clone
WORKDIR /app
RUN git clone https://github.com/spring-projects/spring-petclinic.git
FROM maven:3.5-jdk-8-alpine as build
WORKDIR /app
COPY --from=clone /app/spring-petclinic /app
RUN mvn install
FROM openjdk:8-jre-alpine
WORKDIR /app
COPY --from=build /app/target/spring-petclinic-2.1.0.BUILD-SNAPSHOT.jar /app
CMD [“java -jar spring-petclinic-2.1.0.BUILD-SNAPSHOT.jar”]

To build it

docker build --tag=newpetclinic .

To run it

docker run -p 4001:8080 newpetclinic`

And I got the following error:
`

docker: Error response from daemon: OCI runtime create failed: container_linux.go:344: starting container process caused “exec: “java -jar spring-petclinic-2.1.0.BUILD-SNAPSHOT.jar”: executable file not found in $PATH”: unknown.`


(Gary Forghetti) #7

Bring up a container from that image and get a command prompt and debug it.

docker run -p 4001:8080 newpetclinic sh

Look for the java executeable. etc.


#8

Actually I can not run this docker run comment i get the error I mentioned in my previous command. Should I chance my java_home path or something?


(Gary Forghetti) #9

Sorry, the image you used was alpine so it does not have bash installed.
But you could of brought up a sh prompt like this:

docker run -p 4001:8080 newpetclinic sh

And then looked around.

So I built an image with your Dockerfile and ran it and debugged it for you.
Your CMD statement is wrong in your Dockerfile.

Example

🐳  gforghetti:[~/Downloads/test] $ docker container run -it test
/bin/sh: [“java: not found

Your CMD in your Dockerfile:

CMD [“java -jar spring-petclinic-2.1.0.BUILD-SNAPSHOT.jar”]

Should be this:

CMD ["java", "-jar", "spring-petclinic-2.1.0.BUILD-SNAPSHOT.jar"]`

I fixed the CMD and re-built and it works.

🐳  gforghetti:[~/Downloads/test] $ cat Dockerfile
FROM alpine/git as clone
WORKDIR /app
RUN git clone https://github.com/spring-projects/spring-petclinic.git
FROM maven:3.5-jdk-8-alpine as build
WORKDIR /app
COPY --from=clone /app/spring-petclinic /app
RUN mvn install
FROM openjdk:8-jre-alpine
WORKDIR /app
COPY --from=build /app/target/spring-petclinic-2.1.0.BUILD-SNAPSHOT.jar /app
CMD ["java", "-jar", "spring-petclinic-2.1.0.BUILD-SNAPSHOT.jar"]
🐳  gforghetti:[~/Downloads/test] $ docker image build -t test .
Sending build context to Docker daemon  2.048kB
Step 1/11 : FROM alpine/git as clone
 ---> 1e76d5809b62
Step 2/11 : WORKDIR /app
 ---> Using cache
 ---> 31a17ad44e97
Step 3/11 : RUN git clone https://github.com/spring-projects/spring-petclinic.git
 ---> Using cache
 ---> 73a7dd24aa99
Step 4/11 : FROM maven:3.5-jdk-8-alpine as build
 ---> fb4bb0d89941
Step 5/11 : WORKDIR /app
 ---> Using cache
 ---> bd92b58e5f54
Step 6/11 : COPY --from=clone /app/spring-petclinic /app
 ---> Using cache
 ---> 4a5c09890416
Step 7/11 : RUN mvn install
 ---> Using cache
 ---> 1d8a8cfc88c5
Step 8/11 : FROM openjdk:8-jre-alpine
 ---> 1b46cc2ba839
Step 9/11 : WORKDIR /app
 ---> Using cache
 ---> 228f42c954c3
Step 10/11 : COPY --from=build /app/target/spring-petclinic-2.1.0.BUILD-SNAPSHOT.jar /app
 ---> Using cache
 ---> 8cc1fe8b2136
Step 11/11 : CMD ["java", "-jar", "spring-petclinic-2.1.0.BUILD-SNAPSHOT.jar"]
 ---> Running in da807f9680e3
Removing intermediate container da807f9680e3
 ---> 41209a3c5587
Successfully built 41209a3c5587
Successfully tagged test:latest
🐳  gforghetti:[~/Downloads/test] $ docker container run -it test


              |\      _,,,--,,_
             /,`.-'`'   ._  \-;;,_
  _______ __|,4-  ) )_   .;.(__`'-'__     ___ __    _ ___ _______
 |       | '---''(_/._)-'(_\_)   |   |   |   |  |  | |   |       |
 |    _  |    ___|_     _|       |   |   |   |   |_| |   |       | __ _ _
 |   |_| |   |___  |   | |       |   |   |   |       |   |       | \ \ \ \
 |    ___|    ___| |   | |      _|   |___|   |  _    |   |      _|  \ \ \ \
 |   |   |   |___  |   | |     |_|       |   | | |   |   |     |_    ) ) ) )
 |___|   |_______| |___| |_______|_______|___|_|  |__|___|_______|  / / / /
 ==================================================================/_/_/_/

:: Built with Spring Boot :: 2.1.2.RELEASE

#10

Yes! You are right :see_no_evil::sweat_smile::joy:

Thank you very much!


(Gary Forghetti) #11

There are 2 formats for the CMD statement.

CMD ["java", "-jar", "spring-petclinic-2.1.0.BUILD-SNAPSHOT.jar"]

or this would have worked:

CMD java -jar spring-petclinic-2.1.0.BUILD-SNAPSHOT.jar

CMD ["executable","param1","param2"] ( exec form, this is the preferred form)

CMD command param1 param2 ( shell form)