crockpotveggies crockpotveggies - 1 year ago 112
Java Question

Docker Gradle project generates single JAR, but keeps rebuilding 4 layers

Using Docker 1.12. For some reason, when I build my Docker container which is a Gradle 2.14 project, it builds the fat JAR and stores it locally. The problem is that although I have a single fat JAR being stored in the Docker image, it creates 4 new layers each around 300MB.

Not sure why it requires so much space for a single file. How can I optimize this Dockerfile to prevent it in the future?

Should I be building the JAR outside the container and then copying it?

FROM anapsix/alpine-java:8_jdk


RUN mkdir /build

COPY . /build

WORKDIR /build

RUN mkdir /app
RUN cp ./lib/machine-learning-models.jar /app/machine-learning-models.jar

RUN bash gradlew shadowJar
RUN cp ./build/libs/server-0.1-SNAPSHOT-all.jar /app/server.jar

RUN rm -rf /build


ENTRYPOINT java -cp /app/machine-learning-models.jar:/app/server.jar

Thanks for your help!

Answer Source

It appears the best solution is to just build the fat JAR outside the container and COPY it.

My final solution uses a bare bones Docker Alpine container bundled with the Java JRE. At build time I run gradle shadowJar and then copy the JAR into the container.

FROM anapsix/alpine-java:8_jre

As per @mixja's response, it's essentially a "release" image.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download