Search code examples
spring-bootgitlabgitlab-cigraalvmgraalvm-native-image

Springboot3 build native image in gitlab fails with Docker API call to 'docker:2376/v1.24/...' failed with status code 400 "Bad Request"


I am trying to build a springboot3 program, just a hello world, with gitlab CI/CD.

Here is the pom of the springboot3 job, it is just a hello world, so just pasting the pom.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>1.1</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.3.0</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>23</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


    </dependencies>

    <build>
        <plugins>
  <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <image>
                        <env>
                            <BP_JVM_VERSION>23</BP_JVM_VERSION>
                            <BP_NATIVE_IMAGE_BUILD_ARGUMENTS>-H:-AddAllFileSystemProviders</BP_NATIVE_IMAGE_BUILD_ARGUMENTS>
                        </env>
                    </image>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

And here is the gitlab job:

build-zwith-public:
  image: vegardit/graalvm-maven:latest-java23
  stage: build
  script:
    - mvn -U -Pnative spring-boot:build-image -DskipTests
  services:
    - docker:dind

Reproducible 100%, I am getting this issue:

[INFO] --- spring-boot:3.3.0:repackage (repackage) @ demo ---
[INFO] Replacing main artifact /builds/a/ci-cd/gitlabquestion/target/demo-1.1.jar with repackaged archive, adding nested dependencies in BOOT-INF/.
[INFO] The original artifact has been renamed to /builds/a/ci-cd/gitlabquestion/target/demo-1.1.jar.original
[INFO] 
[INFO] <<< spring-boot:3.3.0:build-image (default-cli) < package @ demo <<<
[INFO] 
[INFO] 
[INFO] --- spring-boot:3.3.0:build-image (default-cli) @ demo ---
[INFO] Building image 'docker.io/library/demo:1.1'
[INFO] 
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder-jammy-tiny:latest' 100%
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.956 s
[INFO] Finished at: 2024-11-08T02:14:31Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:3.3.0:build-image (default-cli) on project demo: Execution default-cli of goal org.springframework.boot:spring-boot-maven-plugin:3.3.0:build-image failed: Docker API call to 'docker:2376/v1.24/images/create?fromImage=docker.io%2Fpaketobuildpacks%2Fbuilder-jammy-tiny%3Alatest' failed with status code 400 "Bad Request" -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException

I am expecting this job to build correctly, as mvn -U -Pnative spring-boot:build-image -DskipTests would successfully compile a native image a macOS, Unix, Windows enviromment.

I tried removing docker in docker, changing to other base image with graalvm, but stil same issue/

How do we resolve these 400 bad requests?


Solution

  • Define the docker service like below

    variables:
      DOCKER_HOST: tcp://docker:2375
    
    build:
      image: ghcr.io/graalvm/graalvm-community:latest
      stage: build
      services:
        - docker:dind
      script:
        - ./mvnw -U -Pnative spring-boot:build-image -DskipTests
    

    I prepared a public repo here, simple spring boot demo app and here is the mvn build image command job. Below you can see the successfull end of log of the job

    [INFO]     [creator]     Adding label 'io.buildpacks.build.metadata'
    [INFO]     [creator]     Adding label 'io.buildpacks.project.metadata'
    [INFO]     [creator]     Adding label 'org.opencontainers.image.title'
    [INFO]     [creator]     Adding label 'org.opencontainers.image.version'
    [INFO]     [creator]     Adding label 'org.springframework.boot.version'
    [INFO]     [creator]     Setting default process type 'web'
    [INFO]     [creator]     Saving docker.io/library/demo:0.0.1-SNAPSHOT...
    [INFO]     [creator]     *** Images (5e75232221c7):
    [INFO]     [creator]           docker.io/library/demo:0.0.1-SNAPSHOT
    [INFO]     [creator]     Adding cache layer 'paketo-buildpacks/bellsoft-liberica:native-image-svm'
    [INFO]     [creator]     Adding cache layer 'paketo-buildpacks/syft:syft'
    [INFO]     [creator]     Adding cache layer 'paketo-buildpacks/native-image:native-image'
    [INFO]     [creator]     Adding cache layer 'buildpacksio/lifecycle:cache.sbom'
    [INFO] 
    [INFO] Successfully built image 'docker.io/library/demo:0.0.1-SNAPSHOT'
    [INFO] 
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  07:41 min
    [INFO] Finished at: 2024-11-24T20:37:25Z
    [INFO] ------------------------------------------------------------------------
    Cleaning up project directory and file based variables 00:01
    Job succeeded