Caching gradle binaries in a Docker build when using the gradle wrapper
It is usually recommended to use the gradle wrapper for building your gradle projects. Doing so ensures that by checking the wrapper into source control you can guarantee that all builds are done using the same version of gradle because the wrapper will always download the configured version.
The cost of having this benefit is felt when repeated builds, like building a Docker image, repeatedly downloads the gradle binaries every time. You can make use of Docker’s caching to cache the layer containing the gradle binaries separately from your application code because the wrapper’s files will change very infrequently compared to the application code.
The solution is to copy all the relevant gradle wrapper files to the Docker build and trigger the download of the binaries before copying and building the application.