Docker images can be built through Maven's Docker plug-in
Quick Start
Add Docker plugin in pom.xml
<plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.13</version> <configuration> <imageName>linyuantongxue/docker-demo:0.0.1</imageName> // Specify the image name, linyuantongxue is the repository name (corresponding to DockerHub user name), docker-demo is the image name (corresponding to DockerHub repository name), 0.0.1 is the label name (equivalent to version number) <baseImage>java</baseImage> // Specify the base image, equivalent to FROM Directive <entryPoint>["java","-jar","app.jar"]</entryPoint> // equivalent to the ENTRYPOINT directive <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> // Specify the root directory to be copied, ${project.build.directory} means the target directory <include>${project.build.finalName}.jar</include> // Specify the file to be copied, ${project.build.finalName}.jar refers to the packaged jar file</resource> </resources> </configuration></plugin> Run the following command to build a Docker image
mvn clean package docker:build
Execute docker images to view the image you just built
Read Dockerfile file
You do not have to specify baseImage and entrypoint when reading Dockerfile files
<plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.13</version> <configuration> <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory> // Specify the Dockerfile file to be read <imageName>linyuantongxue/docker-demo:0.0.1</imageName> // Specify the image name, linyuantongxue is the repository name (corresponding to DockerHub username), docker-demo is the image name (corresponding to DockerHub repository), 0.0.1 is the tag name (equivalent to version number) <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> // Specify the root directory to be copied, ${project.build.directory} means the target directory <include>${project.build.finalName}.jar</include> // Specify the file to be copied, ${project.build.finalName}.jar refers to the packaged jar file</resource> </resources> </configuration></plugin>Bind the plugin to a phase to execute
There are such needs in many scenarios. For example, when the plug-in executes the mvn clean package, the plug-in automatically builds the Docker image. To achieve this, you only need to bind the plug-in's goal to a certain phase.
The maven command format is: mvn phase:goal. phase binds the target's construction lifecycle phase and the execution target of goal configuration
Just add the following configuration:
<plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.13</version> // Execute build target in maven lifecycle package<executions> <execution> <id>build-image</id> <phase>package</phase> <goals> <goal>build</goal> </goals> </execution> </execution> </executions> // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ <imageName>linyuantongxue/docker-demo:0.0.1</imageName> <baseImage>java</baseImage> <entryPoint>["java","-jar","app.jar"]</entryPoint> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration></plugin>Push mirror
Use the Maven plugin to push images to Docker Hub
Modify the Maven global configuration information file settings.xml and configure Docker Hub user information
<servers> <server> <id>docker-hub</id> # DockerHub The username of this website must be all lowercase to be correct <username>linyuantongxue</username> <password>765371578Ly</password> <configuration> <email>[email protected]</email> </configuration> </server></servers>
Modify pom file
<plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.13</version> <configuration> <imageName>linyuantongxue/docker-demo:0.0.1</imageName> <baseImage>java</baseImage> <entryPoint>["java","-jar","app.jar"]</entryPoint> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> <!--Consistent with server.id in configuration file setting.xml, used to push images --> <serverId>docker-hub</serverId> </configuration></plugin>Execute the following command to add the pushImage identifier to indicate the push image
mvn clean package docker:build -DpushImage
In the above example, the imageName can be used to specify the image name and label, and the imageTags element can be used to specify the image name and label more flexibly, so that two labels can be specified for the same image.
<configuration> <imageName>linyuantongxue/docker-demo</imageName> <imageTags> <imageTag>0.0.1</imageTag> <imageTag>latest</imageTag> </imageTags></configuration>
You can also use the dockerImageTags parameter to specify the tag name when building the command
The code copy is as follows:
mvn clean package:build -DpushImageTags -DdockerImageTags=latest -DdockerImageTags=another-tag
If you need to repeatedly build an image with the same tag name, set forceTags to true
<configuration> // ...... <forceTags>true</forceTags></configuration>
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.