```FROM bellsoft/liberica-openjdk-alpine:17 AS bui...
# community-support
v
Copy code
FROM bellsoft/liberica-openjdk-alpine:17 AS build-environment
WORKDIR /product-app

# Cache gradlew
COPY gradlew build.gradle settings.gradle ./ <-----------------
COPY gradle gradle <-----------------
RUN ./gradlew tasks <-----------------

# Copy sources & build
COPY . .
RUN ./gradlew :app:bootJar
is this an idiomatic way to create `Dockerfile`s when building with
./gradlew
? is there maybe something neater? or possible a single copy command?
v
Why do you need to first copy settings script, build script, and wrapper files separately and execute the
tasks
task? Why can't you right away copy the project and run Gradle?
v
to cache gradle otherwise if you only change app sourxes you end up downloading gradle binary everytime
j
a bit off-topic since this is more about docker than gradle, but for something neater I would build a container image that only prepopulates a gradle home cache with all the required dependencies, and do the build at container run time (with
--volume
or an included script that downloads the sources from your VCS server and then starts the build). Building the project at container build time is wasteful unless you use these container images to actually run the project.
v
how do you prepopulate the gradle cache without triggering a gradle build? @Julien Plissonneau Duquène
v
otherwise if you only change app sourxes you end up downloading gradle binary everytime
Quite some time since I built a Docker image, but what you showed does not build an intermediate stage, does it? Wouldn't you need an intermediate
FROM
to enter a new build stage?
v
you would but thats unrelated, thats only to shed to build tools in the final runtime image
docker caches at every step, not just at FROM level
v
Ok, then I misremembered
j
how do you prepopulate the gradle cache without triggering a gradle build?
That's a good question. Maven has a convenient
dependency:go-offline
feature, but I could not find an equivalent feature in Gradle. However you can use the dependency verification feature (in bootstrap mode) to work around that, as it will download the dependencies to compute their checksums. It would probably be a good idea to then overwrite (or move) the generated file and rerun the verification with a trusted file, to make your supply chain a bit more secure. https://docs.gradle.org/current/userguide/dependency_caching.html#sec:cache-copy
also check out the other strategy in the manual (sharing a read-only dependency cache) as it will make your container images much, much lighter and remove the need to rebuild the image with each dependency update