I have this Dockerfile
where I want to build a spring boot project with gradle then run it but the build takes up to 100 seconds to finish but in my local machine it builds fast. I’m sure it’s a caching issue but I don’t know how to speed it up and I’m kind of new to Docker
FROM gradle:7.5.1-jdk11-alpine AS build
COPY --chown=gradle:gradle . /home/gradle/src
WORKDIR /home/gradle/src
RUN gradle clean build --no-daemon
FROM openjdk:11.0.11-jre-slim-buster
EXPOSE 8080
RUN mkdir /app
COPY --from=build /home/gradle/src/build/libs/*.jar /app/demo.jar
ENTRYPOINT ["java", "-jar", "/app/demo.jar"]
3
Answers
On your local machine you will have a local cache of various things (node/npm, gradle, maven, etc.) that means those repeated tasks will be faster. However on a docker image build essentially you are starting with a blank/clean cache in the base image all the time and so it will need to pull everything anew.
You could look to find where the caches are stored and perhaps mount a volume there to allow a cache to persist between image builds if you really wanted to speed things up, but depending on how often you need to rebuild the image that may or may not add much value.
RUN gradle –no-daemon clean bootJar and
try to use openjdk:11.0.11-jre-slim it’s smaller than openjdk:11.0.11-jre-slim-buster
Hope it will help you
Building docker image for maven/gradle can be slow, somehow https://github.com/GoogleContainerTools/jib help the process Dockerize java apps much-much faster.
For Spring-boot
I know it’s different point of view from
Dockerfile
solution, but after using this tools, never focus on Dockerfile anymore but docker-compose instead (for deployment concern).