Home > Enterprise >  Dockerfile to run a Java 11 class
Dockerfile to run a Java 11 class

Time:01-02

I'm attempting to write the simplest docker file that will run a main class named InsertVolume in package scripts. Here is my Dockerfile :

FROM adoptopenjdk/openjdk11:latest

ENV MAVEN_VERSION 3.3.9

RUN mkdir -p /usr/share/maven \
  && curl -fsSL http://apache.osuosl.org/maven/maven-3/$MAVEN_VERSION/binaries/apache-maven-$MAVEN_VERSION-bin.tar.gz \
    | tar -xzC /usr/share/maven --strip-components=1 \
  && ln -s /usr/share/maven/bin/mvn /usr/bin/mvn

CMD "mvn compile exec:java -Dexec.mainClass=\"scripts.InsertVolume\""

Here is the result of running command

 docker build -f Dockerfile -t test/insert .

:

[ ] Building 0.8s (6/6) FINISHED                                                                                                                                                                                                                                          
 => [internal] load build definition from Dockerfile                                                                                                                                                                                                                 0.0s
 => => transferring dockerfile: 37B                                                                                                                                                                                                                                  0.0s
 => [internal] load .dockerignore                                                                                                                                                                                                                                    0.0s
 => => transferring context: 2B                                                                                                                                                                                                                                      0.0s
 => [internal] load metadata for docker.io/adoptopenjdk/openjdk11:latest                                                                                                                                                                                             0.7s
 => [1/2] FROM docker.io/adoptopenjdk/openjdk11:latest@sha256:ec7b07fa180861542ae8af31fbdad43f2f133170b43d29204a4b89e8f56a4c6f                                                                                                                                       0.0s
 => CACHED [2/2] RUN mkdir -p /usr/share/maven   && curl -fsSL http://apache.osuosl.org/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz     | tar -xzC /usr/share/maven --strip-components=1   && ln -s /usr/share/maven/bin/mvn /usr/bin/mvn             0.0s
 => exporting to image                                                                                                                                                                                                                                               0.0s
 => => exporting layers                                                                                                                                                                                                                                              0.0s
 => => writing image sha256:22b094dc17fc3e41b7db6cea72819e5fef1edff6dedf4c291345546fb4f53019                                                                                                                                                                         0.0s
 => => naming to docker.io/test/insert                                                                                                                                                                                                                       0.0s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them

When I then attempt to run the image using :

docker run test/insert

I receive error :

docker: Error response from daemon: OCI runtime create failed: container_linux.go:367: starting container process caused: exec: "mvn compile exec:java -Dexec.mainClass=\"scripts.InsertVolume\"": executable file not found in $PATH: unknown.

The mvn command mvn compile exec:java -Dexec.mainClass="scripts.InsertVolume" runs successfully when I execute against the project (not using docker).

How to modify the Dockerfile so that it compiles and executed the class scripts.InsertVolume ?

Using mvn compile within the Dockerfile should enable the main class to be run ?

Update :

After some debugging I got this to work with following Dockerfile :

FROM adoptopenjdk/openjdk11:latest

ENV MAVEN_VERSION 3.3.9

RUN mkdir -p /usr/share/maven \
  && curl -fsSL http://apache.osuosl.org/maven/maven-3/$MAVEN_VERSION/binaries/apache-maven-$MAVEN_VERSION-bin.tar.gz \
    | tar -xzC /usr/share/maven --strip-components=1 \
  && ln -s /usr/share/maven/bin/mvn /usr/bin/mvn

COPY pom.xml /build/
COPY src /build/src/

WORKDIR /build/

RUN mvn clean package

ENTRYPOINT ["java", "-jar", "target/test-1.0-SNAPSHOT.jar"]

# docker build -f Dockerfile -t test/insert .
# docker run -it --rm test/insert

The crux of the update is copying the project src and building using commands :

COPY pom.xml /build/
COPY src /build/src/

WORKDIR /build/

RUN mvn clean package

CodePudding user response:

Check first if tar -xzC /usr/share/maven --strip-components=1 did what you thought it should.

In other words, in your Dockerfile, add for testing a RUN ls -alrth /usr/bin/mvn et RUN find /usr/share/maven/ to double-check the symlink /usr/bin/mvn actually reference an existing /usr/share/maven/bin/mvn file.

You can also do the same with a docker run -it --rm test/insert bash.

  • Related