dockermaven-pluginfabric8docker-maven-plugin

Fabric8 docker-maven-plugin unable set labels to an image


Using the following plugin

<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.33</version

and using the following configuration (Just posting the relevant bits here)

    <configuration>
      <verbose>build</verbose>
      <images>
        <image>
          <name>${container.imageNameWithTag}</name>
          <build>
            <labels>
              <dummy.label>dummyLabelValue</dummy.label>
            </labels>
            <contextDir>${project.basedir}/src/main/docker</contextDir>
           <assembly>some required assembly </assembly>
          </build>
         </image>
        </images>
    </configuration>

    <executions>
      <execution>
        <id>docker-build</id>
        <goals>
          <goal>build</goal>
        </goals>
        <phase>package</phase>
      </execution>
    </executions>

But the final image has only these labels

        "Labels": {
            "org.label-schema.build-date": "20181204",
            "org.label-schema.license": "GPLv2",
            "org.label-schema.name": "CentOS Base Image",
            "org.label-schema.schema-version": "1.0",
            "org.label-schema.vendor": "CentOS"
        }

which I reckon are from centos base image, but no dummy.label

Am I missing any configuration, or anything is wrongly configured?

The documentation to the plugin is at Maven Docker Plugin


Solution

  • After looking into Build Configuration, of maven-docker-plugin, there is a buildOptions property that also could be used.

    The buildOptions also states

    These options map to the ones listed as query parameters in the Docker Remote API

    The query parameters in Docker Remote API has labels as a parameter.

    labels: Arbitrary key/value labels to set on the image, as a JSON map of string pairs.

    So we have to specify a JSON string into build options as shown below

    <configuration>
              <verbose>build</verbose>
              <images>
                <image>
                  <name>${container.nameWithTag}</name>
                  <build>
                    <contextDir>${project.basedir}/src/main/docker</contextDir>
                    <buildOptions>
    
                      <labels>{
                        "org.label-schema.name":"${container.name}",
                        "org.label-schema.description":"My Image",
                        "org.label-schema.vcs-url":"${project.scm.url}",
                        "org.label-schema.vendor":"Test Vendor",
                        "org.label-schema.version":"${container.tag}"
                        }</labels>
    
                    </buildOptions>
                  </build>
                </image>
              </images>
            </configuration>