1 introduction
This article explains how to do Docker development and package Springboot applications as an image, but it is manual. This article will show you how to package and deploy using Maven with one click.
2. Two Maven plugins are done
One-click deployment can be achieved using maven plugins from the same company, Spotify.
2.1 services/docker – maven plugin
2.1.1 Basic Usage
This plug-in can achieve image packaging and push to the warehouse, no Dockerfile and Dockerfile two ways can be recommended to use Dockerfile, more flexible. Add the following plugin configuration to maven’s POM.xml file:
< plugin > < groupId > com. The company < / groupId > < artifactId > docker maven - plugin < / artifactId > < version > 1.2.2 < / version > <configuration> <imageName>pkslow/springboot-mongo</imageName> <imageTags> <imageTag>${imageVersion}</imageTag> <imageTag>latest</imageTag> </imageTags> <! -- optionally overwrite tags every time image is built with docker:build --> <forceTags>true</forceTags> <dockerDirectory>${project.basedir}</dockerDirectory> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin>Copy the code
-
ImageName: this is the imageName;
-
ImageTags: tag. Supports multiple tags, that is, multiple tags for the same image file. I specified a parameter imageVersion that can be passed in command line to facilitate subsequent Jenkins integration.
-
ForceTags: Whether to overwrite the original tags.
-
DockerDirectory: the location of the Dockerfile file; And all files in this directory will be copied to ${project.build.directory}/docker. Because my Dockerfile is in the project root directory, the entire project files are copied over, including source code, etc. I have to make fun of this design. Is this forcing people to put Dockerfile in another place?
-
Resources: Used to add resource files outside of the dockerDirectory.
Run the following command to add it:
$ mvn clean package docker:build -DimageVersion=0.0.4Copy the code
The command docker images is used to view the image successfully and the operation is normal.
Push to Registry can be done with the following command:
mvn clean package docker:build -DpushImage
mvn clean package docker:build -DpushImageTagCopy the code
2.1.2 Binding with maven life cycle
Executions can be implemented with Maven life cycle by adding executions.
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>VERSION GOES HERE</version>
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
<execution>
<id>tag-image</id>
<phase>package</phase>
<goals>
<goal>tag</goal>
</goals>
<configuration>
<image>my-image:${project.version}</image>
<newName>registry.example.com/my-image:${project.version}</newName>
</configuration>
</execution>
<execution>
<id>push-image</id>
<phase>deploy</phase>
<goals>
<goal>push</goal>
</goals>
<configuration>
<imageName>registry.example.com/my-image:${project.version}</imageName>
</configuration>
</execution>
</executions>
</plugin>Copy the code
With these binding configurations in place, to package the image, simply use the MVN Clean Package.
2.1.3 Configuring warehouse login information
First, plug-ins can use validation information configured locally with ~/.dockercfg or ~/.docker/config.json, or they can be explicitly configured on Maven.
As configured in settings.xml:
<servers>
<server>
<id>docker-hub</id>
<username>foo</username>
<password>secret-password</password>
<configuration>
<email>[email protected]</email>
</configuration>
</server>
</servers>Copy the code
Passwords can be encrypted. See Maven’s Built in Encryption Function for details.
Use this in the project’s POM.xml:
<plugins> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>VERSION GOES HERE</version> <configuration> [...] <serverId>docker-hub</serverId> <registryUrl>https://index.docker.io/v1/</registryUrl> </configuration> </plugin> </plugins>Copy the code
2.2 services/dockerfile – maven
2.2.1 More concise plug-ins
Because the Docker-maven-plugin has some Bugs, Spotify developed a more convenient and concise plugin called Dockerfile-maven.
The configuration of the dockerfile-maven-plugin is simpler:
<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>Copy the code
Once configured, run the following maven command to package the image and push it to the repository:
mvn deployCopy the code
2.2.2 Warehouse verification
The account can be configured in pom.xml as follows:
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>${version}</version>
<configuration>
<username>repoUserName</username>
<password>repoPassword</password>
<repository>${docker.image.prefix}/${project.artifactId}</repository>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>Copy the code
It can also be configured in the Maven configuration file settings. XML. For more security, see github.com/spotify/doc…
3 summary
With the Maven plugin, you can quickly and easily package and deploy with one click, which is very convenient and beneficial for the entire Subsequent DevOps integration.
References:
Docker-maven-plugin:github.com/spotify/doc…
Dockerfile-maven:github.com/spotify/doc…
Visit pumpkin Talk www.pkslow.com for more exciting articles!
Welcome to pay attention to the wechat public number “Pumpkin slow Talk”, will continue to update for you…
Read more and share more; Write more. Organize more.
Welcome to pay attention and share.