Search code examples
spring-bootdockergradledocker-compose

Dockerfile for gradle, spring boot application using multi-stage


I have spring-boot micro service project. I dockerize it. bootBuildImage command run. When i run docker compose up command for spring boot project. I have spring-boot micro service project. I dockerize it. bootBuildImage command run. When i run docker compose up command for spring boot project.I have spring-boot micro service project. I dockerize it. bootBuildImage command run. When i run docker compose up command for spring boot project.

FROM gradle:8.7-jdk AS build

COPY /src/main ./src/main
COPY /build.gradle ./

COPY build.gradle ./

RUN gradle clean bootBuildImage

FROM amazoncorretto:21
ARG JAR_FILE=target/*.jar
COPY --from=build /build/libs/advert-container-1.0-SNAPSHOT.jar advert_app.jar
CMD apt-get update -y
ENTRYPOINT ["java", "-Xmx2048M", "-jar", "/advert_app.jar"]

When i run docker compose up command. My error log like below:

[+] Running 1/1
 ! advert_app Warning pull access denied for autozeug/advert_app, repositor...                     2.9s 
[+] Building 3.2s (12/13)                                                          docker:desktop-linux
 => [advert_app internal] load build definition from Dockerfile                                    0.0s
 => => transferring dockerfile: 420B                                                               0.0s
 => [advert_app internal] load metadata for docker.io/library/gradle:8.7-jdk                       0.9s
 => [advert_app internal] load metadata for docker.io/library/amazoncorretto:21                    0.0s
 => [advert_app auth] library/gradle:pull token for registry-1.docker.io                           0.0s
 => [advert_app internal] load .dockerignore                                                       0.0s
 => => transferring context: 2B                                                                    0.0s
 => [advert_app build 1/5] FROM docker.io/library/gradle:8.7-jdk@sha256:01ec604a8b1748c4678dfc214  0.0s
 => [advert_app stage-1 1/2] FROM docker.io/library/amazoncorretto:21                              0.0s
 => [advert_app internal] load build context                                                       0.0s
 => => transferring context: 2.11kB                                                                0.0s
 => CACHED [advert_app build 2/5] COPY /src/main ./src/main                                        0.0s
 => CACHED [advert_app build 3/5] COPY /build.gradle ./                                            0.0s
 => CACHED [advert_app build 4/5] COPY build.gradle ./                                             0.0s
 => ERROR [advert_app build 5/5] RUN gradle clean bootBuildImage                                   2.2s
------
 > [advert_app build 5/5] RUN gradle clean bootBuildImage:
0.366 
0.366 Welcome to Gradle 8.7!
0.366 
0.366 Here are the highlights of this release:
0.366  - Compiling and testing with Java 22
0.366  - Cacheable Groovy script compilation
0.366  - New methods in lazy collection properties
0.367 
0.367 For more details see https://docs.gradle.org/8.7/release-notes.html
0.367 
0.367 Starting a Gradle Daemon (subsequent builds will be faster)
1.766 
1.766 FAILURE: Build failed with an exception.
1.766 
1.767 * Where:
1.767 Build file '/home/gradle/build.gradle' line: 3
1.767 
1.767 * What went wrong:
1.767 Could not compile build file '/home/gradle/build.gradle'.
1.767 > startup failed:
1.767   build file '/home/gradle/build.gradle': 3: only alias(libs.plugins.someAlias) plugin identifiers where `libs` is a valid version catalog
1.767   
1.767   For more information on the plugins {} block, please refer to https://docs.gradle.org/8.7/userguide/plugins.html#sec:plugins_block in the Gradle documentation.
1.767   
1.767    @ line 3, column 5.
1.767          alias(libs.plugins.springframework)
1.767          ^
1.767   
1.767   build file '/home/gradle/build.gradle': 4: only alias(libs.plugins.someAlias) plugin identifiers where `libs` is a valid version catalog
1.767   
1.767   For more information on the plugins {} block, please refer to https://docs.gradle.org/8.7/userguide/plugins.html#sec:plugins_block in the Gradle documentation.
1.767   
1.767    @ line 4, column 5.
1.767          alias(libs.plugins.dependencymanagement)
1.767          ^
1.767   
1.767   2 errors
1.767 
1.767 
1.768 * Try:
1.768 > Run with --stacktrace option to get the stack trace.
1.768 > Run with --info or --debug option to get more log output.
1.768 > Run with --scan to get full insights.
1.768 > Get more help at https://help.gradle.org.
1.768 
1.768 BUILD FAILED in 1s
------
failed to solve: process "/bin/sh -c gradle clean bootBuildImage" did not complete successfully: exit code: 1

I read lots of dockerfile. But i want to learn what i missed.


Solution

  • The Gradle libs.* syntax references a Gradle version catalog. This is typically in gradle/libs.versions.toml. Remember that nothing is in your image that you don't explicitly COPY in; you need to make sure you COPY this file into the image before you run any Gradle commands.

    FROM gradle:8.7-jdk AS build
    
    COPY build.gradle ./
    COPY gradle/libs.versions.toml ./gradle/
    COPY src/main/ ./src/main/
    
    RUN gradle clean bootBuildImage
    

    If you have the Gradle wrapper checked into your source tree, then you can COPY ./gradlew ./ into the image as well, and build FROM any working JDK image; the image itself doesn't necessarily need to contain Gradle.