Hello I’m looking for a way to improve my docker ...
# ktor
f
Hello I’m looking for a way to improve my docker builds, they’re are taking around 50s-1min (it’s much faster from the IDE). Am I missing something ?
Copy code
FROM gradle:jdk19-jammy AS build
COPY --chown=gradle:gradle . /home/gradle/src
WORKDIR /home/gradle/src
RUN gradle buildFatJar --no-daemon

FROM openjdk:19
EXPOSE 8080
EXPOSE 8080:8080
RUN mkdir /app
COPY --from=build /home/gradle/src/build/libs/*.jar /app/app.jar

CMD ["java", "-jar", "/app/app.jar"]
a
Gradle does a lot of caching, and those caches will be missing when you do a fresh build on a new system. It’s like running
./gradlew clean --no-build-cache --no-configuration-cache buildFatJar
every time. It’s probably downloading the Gradle wrapper every time too. You could try introducing another layer that will initialise Gradle before you copy
/home/gradle/src
, so that layer can be re-used. But it’s kind of tricky to do that because most of the caching will be specific to any source or build config changes, and it’s difficult to split them up into an ‘init Gradle’ step and a ‘build source’ step. You could try copying the cache from your local machine into the container https://docs.gradle.org/current/userguide/dependency_resolution.html#sub:ephemeral-ci-cache. But that’s kind of tricky. Personally I run the Gradle build on my machine and then copy the result into the Docker image. That way, Gradle can be optimised for my machine, re-use the caches, and the Docker image is simpler.
f
Hum ok I see. Thanks for the tip
I confirm much faster to copy the gradle build! thanks again
🚀 1
e
that link above is about sharing the dependency cache. how helpful that is depends on your network speed
what saves much more time in real builds I see is the build cache. a remote build cache can really help with that https://docs.gradle.org/current/userguide/build_cache_use_cases.html#share_results_between_ci_builds
👍 1