Docker uses the Maven plug-in to build the image, dockermaven
You can use the Maven Docker plug-in to build a Docker image.
Quick Start
Add the Docker plug-in to pom. xml
<Plugin> <groupId> com. spotify </groupId> <artifactId> docker-maven-plugin </artifactId> <version> 0.4.13 </version> <configuration> <imageName> linyustmgxue/docker-demo: 0.0.1 </imageName> // specify the image name. linyustmgxue is the repository name (corresponding to the DockerHub user name), and docker-demo is the image name (corresponding to the DockerHub repository name ), 0.0.1 is the Tag Name (equivalent to the version number) <baseImage> java </baseImage> // specify the basic image, equivalent to the FROM command <entryPoint> ["java", "-jar ", "app. jar "] </entryPoint> // equivalent to the ENTRYPOINT command <resources> <resource> <targetPath>/</targetPath> <directory >$ {project. build. directory} </directory> // specify the root directory to be copied, $ {project. build. directory} indicates 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
Run docker images to view the image just created
Read the Dockerfile
To read the Dockerfile, you do not need to specify baseImage and entrypoint.
<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 to be read <imageName> linyustmgxue/docker-demo: 0.0.1 </imageName> // specify the image name, linyuw.gxue is the repository name (corresponding to the DockerHub user name), docker-demo is the image name (corresponding to the DockerHub repository name), and 0.0.1 is the Tag Name (equivalent to the version number) <resources> <resource> <targetPath>/</targetPath> <directory >$ {project. build. directory} </directory> // specify the root directory to be copied, $ {project. build. directory} indicates 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 plug-in to a phase for execution
This is required in many scenarios. For example, when executing the mvn clean package, the plug-in automatically creates a Docker image. To achieve this, you only need to bind the goal of the plug-in to a certain phase.
The maven command format is mvn phase: goal. phase is bound to the Target Construction lifecycle stage, and the goal configuration execution target.
You only need to add the following Configuration:
<Plugin> <groupId> com. spotify </groupId> <artifactId> docker-maven-plugin </artifactId> <version> 0.4.13 </version> // execute the build target in the maven lifecycle package <executions> <execution> <id> build-image </id> <phase> package </phase> <goals> <goal> build </goal> </goals> </execution> </executions> // $ gorgeous split line $ $ <configuration> <imageName> linyuw.gxue/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 Image
You can also use the Maven plug-in to push images to Docker Hub.
Modify the Maven global configuration file settings. xml to configure the Docker Hub user information.
<Servers> <server> <id> docker-hub </id> # the username of the website must be in lowercase. <username> linyubaigxue </username> <password> 765371578Ly </password> <configuration> <email> 765371578@qq.com </email> </configuration> </server> </servers>
Modify the pom File
<Plugin> <groupId> com. spotify </groupId> <artifactId> docker-maven-plugin </artifactId> <version> 0.4.13 </version> <configuration> <imageName> linyustmgxue/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 the server. id in the configuration file setting. xml, used to push the image --> <serverId> docker-hub </serverId> </configuration> </plugin>
Run the following command to add the pushImage ID to push the image
mvn clean package docker:build -DpushImage
In the preceding example, you can specify the image name and tag by using imageName. You can also use the imageTags element to specify the image name and tag more flexibly. In this way, you can specify two tags 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 creating a command.
Copy codeThe Code is as follows:
Mvn clean package: build-DpushImageTags-DdockerImageTags = latest-DdockerImageTags = another-tag
To create images 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 for your learning and support for helping customers.