Skip to content

Can not use jar inside docker #166

@lanliu99

Description

@lanliu99

A helloworld java program can run with openjdk in unpackaged dir, but when package classes into jar, this doesn't work, here is detail:
#1. This Docker works with unpackeged dir used in Dockerfile:
Dockerfile
FROM openjdk
RUN mkdir /home/mylib
COPY ./classes /home/mylib
WORKDIR /home/mylib
RUN chmod -R 755 /home/mylib && ls -l /home/mylib
CMD ["java", "com.mycompany.app.App"]

docker build:
Sending build context to Docker daemon 20.48 kB
Step 1 : FROM openjdk
---> 7c57090325cc
Step 2 : RUN mkdir /home/mylib
---> Running in 23ccbff7b0dc
---> fa1e54d9cd1a
Removing intermediate container 23ccbff7b0dc
Step 3 : COPY classes /home/mylib
---> 730b80254d46
Removing intermediate container 5a145af08e07
Step 4 : WORKDIR /home/mylib
---> Running in a55c9e7387b5
---> 024f61681aa2
Removing intermediate container a55c9e7387b5
Step 5 : RUN chmod -R 755 /home/mylib && ls -l /home/mylib
---> Running in e84552bc63a0
total 0
drwxr-xr-x. 3 root root 23 Feb 1 16:19 com
---> 9f9ef5a68863
Removing intermediate container e84552bc63a0
Step 6 : CMD java com.mycompany.app.App
---> Running in 8119d7462f15
---> 5a791b689045
Removing intermediate container 8119d7462f15
Successfully built 5a791b689045

docker run:
hello !

#2. When package classes into jar, use jar in image, it doesn't work
Docker file:
FROM openjdk
RUN mkdir /home/mylib
COPY ./my-app-1.0-SNAPSHOT.jar /home/mylib
WORKDIR /home/mylib
RUN chmod -R 755 /home/mylib && ls -l /home/mylib
CMD ["java", “-cp /home/mylib/my-app-1.0-SNAPSHOT.jar”, "com.mycompany.app.App"]

Output:
Sending build context to Docker daemon 20.48 kB
Step 1 : FROM openjdk
---> 7c57090325cc
Step 2 : RUN mkdir /home/mylib
---> Running in 6d439987e509
---> f8f3e1bc604f
Removing intermediate container 6d439987e509
Step 3 : COPY ./my-app-1.0-SNAPSHOT.jar /home/mylib
---> 262f23fb541b
Removing intermediate container 484e0ac5e7a8
Step 4 : WORKDIR /home/mylib
---> Running in 913ed091bb95
---> a310b5f7a92b
Removing intermediate container 913ed091bb95
Step 5 : RUN chmod -R 755 /home/mylib && ls -l /home/mylib
---> Running in 0116e0e69b31
total 4
-rwxr-xr-x. 1 root root 2623 Feb 1 15:21 my-app-1.0-SNAPSHOT.jar
---> f3a1c08c4b93
Removing intermediate container 0116e0e69b31
Step 6 : CMD java -cp /home/mylib com.mycompany.app.App
---> Running in f12d02de8c9b
---> 72875b7ca1c6
Removing intermediate container f12d02de8c9b
Successfully built 72875b7ca1c6

docker run:
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

Why I can't use jar in this image?

Best regards

Lan

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions