On the method of using Maven plug-in to build docker image
This article introduces the method of using Maven plug-in to build docker image and shares it with you, as follows:
tool
Sharp tools make good work. After investigation, the following docker Maven plug-ins have come into my view:
The author selects the first one from the three dimensions of stars, document ease of use and update frequency.
Using plug-ins to build docker images
Simple use
Let's take the previous project: microservice discovery Eureka as an example:
In POM Add the following paragraph to the XML
Execute command:
We will find that the console has the following contents:
Congratulations, the build succeeded.
When we execute docker images, we will find that the image has been successfully built:
Start mirroring
We will find that the docker image will start soon.
Access test
visit http://Docker Host IP: 8761. Eureka interface can be seen normally.
Build using dockerfile
The method described above is the simplest. In many cases, we still need to build with the help of dockerfile. First, we create the file dockerfile in the / microservice discovery Eureka / SRC / main / docker directory
Modify POM xml
The other steps are the same. In this way, you can use dockerfile to build docker images.
Push the docker image onto the dockerhub
First, modify Maven's global configuration file settings XML, add the following paragraph
Create repo on dockerhub
Project POM The XML is modified as follows: note that the path of imagename should be consistent with the path of repo
Execute command:
After the completion, we will find that the docker image has been pushed to the dockerhub.
Push the image to the private warehouse
In many scenarios, we need to push the image to the private warehouse. In order to explain the comprehensiveness, the private warehouse adopts the private warehouse configured with login authentication.
Like pushing the image to dockerhub, we first need to modify Maven's global configuration file settings XML, add the following paragraph
Put the POM of the project The XML is changed as follows,
Execution:
Wait a moment and push will succeed.
If you want to download the image from the private server, execute:
Bind the plug-in to a phase for execution
In many scenarios, we have such requirements. For example, when executing MVN clean package, we will automatically build a docker image for us. Is that ok? The answer is yes. We just need to bind the goal of the plug-in to a phase.
The so-called phase and goal can be understood as follows: the format of the Maven command is MVN phase: goal, for example, MVN package docker: build. Then, both package and docker are phase and build is goal.
Here is an example:
Configure Properties First:
Plug in configuration:
As above, we only need to add:
Just. This example refers to the build target of docker, which is bound to the phase of package. In other words, users only need to execute MVN package, and then MVN docker: build is automatically executed.
Common anomalies
Unable to connect 2375 (usually on win7)
reference resources: https://github.com/spotify/docker-maven-plugin/issues/116