GitLab Tip #1 - Push a Docker image to GitLab using Maven

Assuming that you have logged into the Docker registry you are going to use for pushing your image, the Maven recipe below should get you started.


<?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.mycompany</groupId>
    <artifactId>myproduct</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>MyProduct</name>
    <build>
        <finalName>myproduct</finalName>
        <plugins>
            <plugin>
                <groupId>io.fabric8</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>0.15.9</version>
                <configuration>
                    <images>
                        <image>
                            <name>registry.gitlab.com/myusername/myproduct:${project.version}</name>
                            <build>
                                <assembly>
                                    <inline>
                                        <dependencySets>
                                            <dependencySet>
                                                <useProjectArtifact>false</useProjectArtifact>
                                                <outputFileNameMapping>${artifact.artifactId}.${artifact.extension}</outputFileNameMapping>
                                            </dependencySet>
                                        </dependencySets>
                                    </inline>
                                </assembly>
                                <cleanup>none</cleanup>
                                <compression>bzip2</compression>
                                <dockerFileDir>.</dockerFileDir>
                                <nocache>true</nocache>
                                <optimise>true</optimise>
                            </build>
                        </image>
                    </images>
                </configuration>
                <executions>
                    <execution>
                        <id>pre-integration-test</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <addMavenDescriptor>false</addMavenDescriptor>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.mycompany.myproduct.Main</mainClass>
                        </manifest>
                    </archive>
                    <forceCreation>true</forceCreation>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>com.mycompany</groupId>
            <artifactId>myproduct</artifactId>
            <version>${project.version}</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    <properties>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>
        

And complete the following:

  1. Put your Dockerfile in the src/main/docker directory.
  2. Update the dependency to use.
  3. Invoke Maven using mvn clean install docker:push.

And voila you can now push your Docker image to GitLab using Maven

Posted October 3, 2016

Up