As a docker project, dockerfile files are built, tagged, and published. If you can include these operations on the Dockerfile file in Maven lifecycle management, it will greatly simplify the build and release process of the Docker project.
Dockerfile maven is a maven plugin from Spotify that also includes a MAVEN extension that assists with Dockerfile file building, labeling, and publishing in a Docker project (with Maven management). Spotify also provided an additional Maven plugin docker-maven-plugin, but the plugin was no longer new. Dockerfile maven will replace the outdated Docker-maven-plugin plugin. This article focuses on the Dockerfile Maven.
Dockerfile maven is currently the latest version of 1.4.0, license Apache License 2.0.
1. Dockerfile maven actually contains the following two parts
A maven plugin that contains 3 goals a maven extension that supports docker-info types of artifacts
2. Features of Dockerfile maven
By default, the process of building dockerfile is included in the MVN package stage; By default, the process of labeling Dockerfile is included in the MVN package stage; By default, the process of releasing Dockerfile is included in the MVN deploy phase; can also be directly implemented: MVN Dockerfile:build
MVN Dockerfile:tag
MVN Dockerfile:push
You can also support the Dockerfile file that references another Docker project in one Docker project.
3. Dockerfile maven Pre-use
Java 7+ Maven
4. Basic usage of Dockerfile maven
The Pom.xml file in the Docker project is configured as follows:
<plugin>
<groupId>com.spotify</groupId>
<artifactid>dockerfile-maven-plugin </artifactId>
<version>${dockerfile-maven-version}</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
< configuration>
<repository>spotify/foobar</repository>
<tag>${project.version} </tag>
<buildArgs>
<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
Description: Where ${dockerfile-maven-version} is the version of the Dockerfile maven plugin, currently 1.4.0;
${project.version} is the version of the Docker project;
${project.build.finalname}.jar builds the generated components for the Docker project, in the form of jar packages;
When building a Docker project, execute mvn deploy directly to build and publish the Dockerfile file to the MAVEN local library Spotify/foobar.
5. Usage of Dockerfile maven extensions
If you need to access the Dockerfile file information for another project during the code development process for a Docker project, you need to dockerfile the help of the MAVEN extension.
For example, the current Docker project needs to access the Dockerfile file information for another Docker project Hisfoobar, which is configured in the Pom.xml file for the current Docker project:
1) First configure the Dockerfile maven extension
<build>
<extensions>
<extension>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-extension</artifactId>
<version>${version}</version>
</extension>
</extensions>
</build>
Description: Where ${version} is the same as ${dockerfile-maven-version}, currently 1.4.0;
The new type Docker-info is supported in this extension.
2) Then declare a dependency on the Hisfoobar project
<dependency>
<groupId>com.spotify</groupId>
<artifactid>hisfoobar</ artifactid>
<version>${hisfoobar-version}</version>
<type>docker-info</type >
</dependency>
Description: Where ${hisfoobar-version} is the version of the Hisfoobar project;
Note that the type of the Foobar entry here is Docker-info.
3) Access the Dockerfile file image name of the Hisfoobar project in the code
String imageName = getresource ("Meta-inf/docker/com.spotify/hisfoobar/image-name");
6. Dockerfile MAVEN's certification support for private Docker registries (such as Google Container Registry)
Starting with 1.3.0, when you build, publish, and get dockerfile files to the Docker registry, the plug-in automatically detects Docker profiles such as ~/.dockercfg or ~/.docker/config.json.
Starting with 1.3.6, there is no need to configure Docker profiles for this purpose, directly using Maven's Settings.xml or project Pom.xml.
1) Configure the Remote component library authentication information in the Settings.xml file
First, configure the Pom.xml file for the Docker project
<plugin>
<groupId>com.spotify</groupId>
<artifactid>dockerfile-maven-plugin </artifactId>
<version>${dockerfile-maven-version}</version>
<configuration>
<repository>docker-repo.example.com:8080/organization/image</repository>
<tag>${ project.version}</tag>
<buildArgs>
<jar_file>target/${project.build.finalname}.jar </JAR_FILE>
</buildArgs>
<usemavensettingsforauth>true</usemavensettingsforauth >
</configuration>
</plugin>
Description: The remote component library to be published is located in Docker-repo.example.com:8080/organization/image;
Usemavensettingsforauth=true indicates that the authentication information to access the Remote component library is configured in Settings.xml.
Second, configure the settings.xml file
<servers>
<server>
<id>docker-repo.example.com:8080</id>
<username> myuser</username>
<password>mypassword</password>
</server>
</servers >
2) Configure the Remote component library authentication information in the Pom.xml file of the Docker project
<plugin>
<groupId>com.spotify</groupId>
<artifactid>dockerfile-maven-plugin </artifactId>
<version>${dockerfile-maven-version}</version>
<configuration>
<username>repoUserName</username>
<password>repoPassword</password>
<repository>docker-repo.example.com:8080/organization/image</repository>
<tag>${ project.version}</tag>
<buildArgs>
<jar_file>target/${project.build.finalname}.jar </JAR_FILE>
</buildArgs>
</configuration>
</plugin>
By contrast, the authentication information that accesses the remote component library is configured directly in the Pom.xml.
7. The Dockerfile Maven plugin is configured in the Docker project, but during the MAVEN build process, you want to temporarily skip Dockerfile-related builds, releases
If you want to temporarily skip all 3 goals related to all Dockerfile, perform the following MAVEN command:
MVN Clean Package-ddockerfile.skip
If you just want to skip a certain goal, execute the following MAVEN command:
MVN Clean Package-ddockerfile.build.skip
MVN Clean Package-ddockerfile.tag.skip
MVN Clean Package-ddockerfile.push.skip
Reference Links:
Https://github.com/spotify/dockerfile-maven
Https://github.com/spotify/docker-maven-plugin