Search code examples
spring-bootdockerfile

How to add container labels with Spring Boot `build-image`?


In a regular Dockerfile I can add arbitrary metadata labels, for example:

LABEL title="My Spring Boot App"
LABEL version="1.0.0-SNAPSHOT"
LABEL authors="stewart"

These labels can be later inspected with the command

docker inspect --format='{{json .Config.Labels}}' my-spring-boot-image:latest

How can one achieve the same thing, if you are creating your Docker image using the spring-boot-maven-plugin ?

mvn spring-boot:build-image

Solution

  • Using the SO answer suggested in comments above, I successfully configured this:

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!--
                    docker inspect <TAG> - -format='{{json .Config.Labels}}' | jq 'with_entries(select(.key | startswith("org.opencontainers.image")))'
                    docker inspect <TAG> - -format='{{json .Config.Labels}}' | jq 'with_entries(select(.key | startswith("io.buildpacks") | not))'
                -->
                <configuration>
                    <image>
                        <name>${project.artifactId}:${project.version}.${buildNumber}</name>
                        <env>
                            <BP_IMAGE_LABELS>
                                built-by=${user.name}
                                organization="${organization}"
                            </BP_IMAGE_LABELS>
                            <BP_OCI_AUTHORS>${project.developers}</BP_OCI_AUTHORS>
                            <BP_OCI_CREATED>${maven.build.timestamp}</BP_OCI_CREATED>
                            <BP_OCI_DESCRIPTION>${project.description}</BP_OCI_DESCRIPTION>
                            <BP_OCI_DOCUMENTATION>${issueManagement}</BP_OCI_DOCUMENTATION>
                            <BP_OCI_LICENSES>(c) 2024 All Rights Reserved.</BP_OCI_LICENSES>
                            <BP_OCI_REF_NAME>${project.artifactId}-${project.version}.${buildNumber}</BP_OCI_REF_NAME>
                            <BP_OCI_REVISION>${buildNumber}</BP_OCI_REVISION>
                            <BP_OCI_SOURCE>${scm.url}</BP_OCI_SOURCE>
                            <BP_OCI_TITLE>${project.name}</BP_OCI_TITLE>
                            <BP_OCI_URL>${project.url}</BP_OCI_URL>
                            <BP_OCI_VENDOR>Stewart@Stackoverflow</BP_OCI_VENDOR>
                            <BP_OCI_VERSION>${project.version}</BP_OCI_VERSION>
                        </env>
                    </image>
                </configuration>
                <executions>
                    <execution>
                        <id>build-image</id>
                        <phase>install</phase>
                        <goals>
                            <goal>build-image-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>