This article introduces the method of using the Maven plug-in to build Docker images. I will share it with you. The details are as follows:
tool
If you want to do a good job, you must first sharpen your tools. After research, the following Docker Maven plug-ins have entered my field of vision:
| Plugin Name | Official address |
|---|---|
| docker-maven-plugin | https://github.com/spotify/docker-maven-plugin |
| docker-maven-plugin | https://github.com/fabric8io/docker-maven-plugin |
| docker-maven-plugin | https://github.com/bibryam/docker-maven-plugin |
The author chose the first one from three dimensions: Stars, document ease of use and update frequency.
Build Docker Images with Plugins
Simple use
Let's take the previous project: microservice-discovery-eureka as an example:
Add the following paragraph in pom.xml
<build> <plugins> <!-- docker's maven plugin, official website: https://github.com/spotify/docker-maven-plugin --> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.12</version> <configuration> <!-- Note that imageName must comply with the regular [a-z0-9-_.], otherwise the build will not be successful --> <!-- See: https://github.com/spotify/docker-maven-plugin Invalid repository name ... only [a-z0-9-_.] are allowed--> <imageName>microservice-discovery-eureka</imageName> <baseImage>java</baseImage> <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resources> </configuration> </plugin> </plugins> </build> Execute the command:
mvn clean package docker:build
We will find that the console has something like the following:
[INFO] Building image microservice-discovery-eurekaStep 1 : FROM javaPulling from library/javaDigest: sha256:581a4afcbbedd8fdf194d597cb5106c1f91463024fb3a49a2d9f025165eb675fStatus: Downloaded newer image for java:latest ---> ea40c858f006Step 2 : ADD /microservice-discovery-eureka-0.0.1-SNAPSHOT.jar // ---> d1c174083bcaRemoving intermediate container 91913d847c20Step 3: ENTRYPOINT java -jar /microservice-discovery-eureka-0.0.1-SNAPSHOT.jar ---> Running in 0f2aeccdfd46 ---> d57b027ca65aRemoving intermediate container 0f2aeccdfd46Successfully built d57b027ca65a[INFO] Built microservice-discovery-eureka[INFO] --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Congratulations, the construction was successful.
We execute docker images and find that the image has been successfully built:
REPOSITORY TAG IMAGE ID CREATED SIZEmicroservice-discovery-eureka latest d57b027ca65a About a minute ago 681.5 MB
Start the mirror
docker run -p 8761:8761 microservice-discovery-eureka
We will find that the Docker image will start very quickly.
Access Test
Visit http://Docker host IP:8761 to see the Eureka interface normally.
Build with Dockerfile
The above method is the easiest way. Many times, we still need to build it with the help of Dockerfile. First, we create the file Dockerfile in the /microservice-discovery-eureka/src/main/docker directory.
FROM java:8VOLUME /tmpADD microservice-discovery-eureka-0.0.1-SNAPSHOT.jar app.jarRUN bash -c 'touch /app.jar'EXPOSE 9000ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
Modify pom.xml
<build> <plugins> <!-- docker's maven plugin, official website: https://github.com/spotify/docker-maven-plugin --> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.12</version> <configuration> <!-- Note that imageName must comply with the regular [a-z0-9-_.], otherwise the build will not be successful --> <!-- See: https://github.com/spotify/docker-maven-plugin Invalid repository name ... only [a-z0-9-_.] are allowed--> <imageName>microservice-discovery-eureka-dockerfile</imageName> <!-- Specify the path to which Dockerfile is located--> <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resources> </configuration> </plugin> </plugins> </build>The other steps are the same. In this way, you can use Dockerfile to build Docker images.
Push Docker image on DockerHub
First modify Maven's global configuration file settings.xml and add the following paragraph
<servers> <server> <id>docker-hub</id> <username>Your DockerHub username</username> <password>Your DockerHub password</password> <configuration> <email>Your DockerHub mailbox</email> </configuration> </server></servers>
Create a repo on DockerHub
The project pom.xml is modified to the following: Note that the path of imageName should be consistent with the path of repo
<build> <plugins> <!-- docker's maven plugin, official website: https://github.com/spotify/docker-maven-plugin --> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.12</version> <configuration> <!-- Note that imageName must comply with the regular [a-z0-9-_.], otherwise the build will not be successful --> <!-- See: https://github.com/spotify/docker-maven-plugin Invalid repository name ... only [a-z0-9-_.] are allowed --> <!-- If you want to push the docker image to DockerHub, the path here must be the same as the repo path --> <imageName>eacdy/test</imageName> <!-- Specify the path where the Dockerfile is located --> <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> <!-- The following two lines are used for docker push to DockerHub. --> <serverId>docker-hub</serverId> <registryUrl>https://index.docker.io/v1/</registryUrl> </configuration> </plugin> </plugins> </build> Execute the command:
mvn clean package docker:build -DpushImage
After the construction is successful, we will find that the Docker image has been pushed to DockerHub.
Push the image to a private repository
In many scenarios, we need to push the mirror to a private repository. In order to explain the comprehensiveness, the private repository uses a private repository that configures login authentication.
Just like pushing to DockerHub, we first need to modify Maven's global configuration file settings.xml and add the following paragraph
<servers> <server> <id>docker-registry</id> <username>Your DockerHub username</username> <password>Your DockerHub password</password> <configuration> <email>Your DockerHub mailbox</email> </configuration> </server></servers>
Change the project's pom.xml to the following,
<plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.12</version> <configuration> <!-- The path is: private repository address/the mirror path you want--> <imageName>reg.itmuch.com/test-pull-registry</imageName> <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> <!-- Consistent with maven configuration file settings.xml--> <serverId>docker-registry</serverId> </configuration></plugin> implement:
mvn clean package docker:build -DpushImage
Wait for a moment and you will be successful.
If you want to download the image from the private server, execute:
docker login reg.itmuch.com # Then enter the account number and password docker pull reg.itmuch.com/test-pull-registry
Bind the plugin to execute in a phase
In many scenarios, we have such needs, for example, when executing mvn clean package, we will automatically build a docker image for us. Is it OK? The answer is yes. We just need to bind the plugin's goal to a certain phase.
The so-called phase and goal can be understood as follows: the maven command format is: mvn phase:goal , for example, mvn package docker:build , then, package and docker are both phases, and build is goal .
Here is an example:
First configure the properties:
<properties><docker.image.prefix>reg.itmuch.com</docker.image.prefix></properties>
Plugin configuration:
<build> <plugins> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <executions> <execution> <id>build-image</id> <phase>package</phase> <goals> <goal>build</goal> </goals> </execution> </execution> </execution> </configuration> <imageName>${docker.image.prefix}/${project.artifactId}</imageName> <baseImage>java</baseImage> <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> </plugins> </build>As above, we just need to add:
<executions> <execution> <id>build-image</id> <phase>package</phase> <goals> <goal>build</goal> </goals> </execution> </executions>
Just do it. This example refers to the build target of docker, bound to the package phase. In other words, the user only needs to execute mvn package and automatically execute mvn docker:build.
Common exceptions
Can't connect to 2375 (usually on Win7)
The code copy is as follows:
Connect to localhost:2375 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:0:1] failed: Connection refused: connect -> [Help 1]
Solution steps:
Enter docker-machine env
$Env:DOCKER_TLS_VERIFY = "1"$Env:DOCKER_HOST = "tcp://192.168.99.100:2376"$Env:DOCKER_CERT_PATH = "C:/Users/Administrator/.docker/machine/machines/default
Add configuration for plugins
<!-- One of the ways to solve the problem of Connect to localhost:2375, be sure to be consistent with docker-machine env--><dockerHost>https://192.168.99.100:2376</dockerHost> <dockerCertPath>C:/Users/Administrator/.docker/machine/machines/default</dockerCertPath>
After modification, the plug-in configuration becomes:
<plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.12</version> <configuration> <imageName>eacdy/test</imageName> <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory> <!-- One of the ways to solve the problem of Connect to localhost:2375, be sure to be consistent with docker-machine env--> <dockerHost>https://192.168.99.100:2376</dockerHost> <dockerCertPath>C:/Users/Administrator/.docker/machine/machines/default</dockerCertPath> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> <!-- The following two lines are used for docker push to DockerHub. --> <serverId>docker-hub</serverId> <registryUrl>https://index.docker.io/v1/</registryUrl> </configuration></plugin> Reference: https://github.com/spotify/docker-maven-plugin/issues/116
TIPS
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.