In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to package an image with docker-maven-plugin and upload it to a private warehouse". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Catalogue
1. Docker-maven-plugin introduction
2. Environment and software preparation
3. Demo example
3.1Configuring DOCKER_HOST
3.2 example build image
3.3 execute orders
3.4 bind Docker commands to each stage of Maven
3.5 use private Docker warehouse address
3.6 Security authentication configuration
3.7 other parameters
4 、 FAQ
1. Docker-maven-plugin introduction
In the process of continuous integration, project projects are generally compiled and packaged with Maven, and then generate an image, which can greatly provide online efficiency, rapid dynamic expansion and rapid rollback, which is very convenient. The docker-maven-plugin plug-in is designed to help us automatically generate images and push them to the repository through simple configuration in the Maven project.
2. Environment and software preparation
This demo environment, I am operating on the native Mac OX, the following is the installed software and version:
Docker:version 17.03.1-ce
Maven:version 3.3.9
Java: version 1.8.0_91
Docker-maven-plugin:1.0.0
Note: here we want to test the Java Maven project to use the docker-maven plug-in to create and upload images, so we need to install Docker, Maven and Java first. The installation process is ignored here.
3. Demo sample 3.1 configure DOCKER_HOST
By default, the local Docker address of the docker-maven-plugin plug-in connection is: localhost:2375, so we need to set the environment variable first.
DOCKER_HOST=tcp://:2375
Note: if the DOCKER_HOST environment variable is not set, it can be executed by specifying DOCKER_HOST on the command line, such as I specify DOCKER_HOST:DOCKER_HOST=unix:///var/run/docker.sock mvn clean install docker:build natively.
3.2 example build image
There are two ways to build an image, the first is to specify the build information to the POM, and the second is to build using an existing Dockerfile.
The first way is to support the configuration of FROM, ENTRYPOINT, CMD, MAINTAINER and ADD information in POM, without the need for Dockerfile configuration. But if you use commands in VOLUME or other Dockerfile, you need to use the second way to create a Dockerfile and configure dockerDirectory in POM to specify the path.
Here we take a Java Maven project, mavendemo, as an example.
3.2.1 specify build information to build in POM
Com.spotify docker-maven-plugin 1.0.0 mavendemo java docker_maven docker_maven@email.com / ROOT ["java", "- version"] ["java", "- jar" "${project.build.finalName} .jar"] / ROOT ${project.build.directory} ${project.build.finalName} .jar
3.2.2 build using Dockerfile
Pom.xml configuration
Com.spotify docker-maven-plugin 1.0.0 mavendemo ${basedir} / docker / ROOT ${project.build.directory} ${project.build.finalName} .jar ${basedir} / docker/Dockerfile configure FROM javaMAINTAINER docker_maven docker_maven@email.comWORKDIR / ROOTCMD ["java" "- version"] ENTRYPOINT ["java", "- jar", "${project.build.finalName} .jar"]
The effect of the above two methods of executing docker:build is the same, and the output process is roughly as follows:
[INFO]-docker-maven-plugin:1.0.0:build (default-cli) @ mavenDemo-
[INFO] Building image mavendemo
Step 1/5: FROM java
-- > d23bdf5b1b1b
Step 2/5: MAINTAINER docker_maven docker_maven@email.com
-- > Using cache
-- > 2faf180d4a50
Step 3/5: WORKDIR / ROOT
-- > Using cache
-- > 862210f7956a
Step 4ax 5: ENTRYPOINT java-jar mavenDemo.jar
-- > Running in 96bbe83de6ec
-- > c29009c88993
Removing intermediate container 96bbe83de6ec
Step 5ax 5: CMD java-version
-- > Running in f69b8d2a75b1
-- > bc8d54014325
Removing intermediate container f69b8d2a75b1
Successfully built bc8d54014325
After the execution is complete, use docker images to view the generated image:
REPOSITORY TAG IMAGE ID CREATED SIZE
Mavendemo latest 333b429536b2 38 minutes ago 643 MB
3.3 execute orders
Mvn clean package docker:build only performs build operations
Push image after mvn clean package docker:build-DpushImage executes build
Mvn clean package docker:build-DpushImageTag executes build and push specifies the mirror of tag
Note: at least one imageTag must be specified here, which can be configured in POM or on the command line. The command line is specified as follows: the configuration specified in the mvn clean package docker:build-DpushImageTags-DdockerImageTags=imageTag_1-DdockerImageTags=imageTag_2,POM file is as follows:
...... ImageTag_1 imageTag_2... 3.4 bind Docker commands to all stages of Maven
We can bind Docker commands to each stage of Maven. We can divide Docker into build, tag and push, and then bind package and deploy phases of Maven respectively. At this time, we only need to execute mvn deploy to complete the entire build, tag and push operations. When we execute mvn build, we only complete build and tag operations. In addition, when we want to skip certain steps or perform only one step, we don't need to modify the POM file, we just need to specify that we skip a step in docker. For example, when our project has already configured the automation template, but this time we only need to mirror the local self-test and do not want to execute the push phase, then we can skip the push operation by specifying the parameter-DskipDockerPush at this time.
Com.spotify docker-maven-plugin 1.0.0 mavendemo java docker_maven docker_maven@email.com / ROOT ["java", "- version"] ["java", "- jar" "${project.build.finalName} .jar"] / ROOT ${project.build.directory} ${project.build.finalName} .jar Build-image package build tag-image package tag Mavendemo:latest docker.io/wanyang3/mavendemo:$ {project.version} push-image deploy Push docker.io/wanyang3/mavendemo:$ {project.version}
In the above example, when we execute mvn package, we perform build and tag operations, and when we execute mvn deploy, we perform build, tag, and push operations. If we want to skip a process in docker, we just need to:
-DskipDockerBuild skips build mirroring
-DskipDockerTag skips tag mirroring
-DskipDockerPush skips push mirroring
-DskipDocker skips the whole phase
For example, if we want to skip the tag process when we want to execute package, we need mvn package-DskipDockerTag.
3.5 use private Docker warehouse address
In the actual work environment, we need to image push to our private Docker repository, and we can easily implement it using the d ocker-maven-plugin plug-in, which can be implemented in several ways:
1. Modify imageName operation of POM file
... Registry.example.com/wanyang3/mavendemo:v1.0.0.
2. Modify the newName operation in POM file
... Mavendemo... Tag-image package tag mavendemo registry.example.com/wanyang3/mavendemo:v1.0.0... 3.6Security authentication configuration
When we push images to the Docker repository, whether public or private, security authentication is often required, and the operation can only be carried out after the login is completed. Of course, we can log in from the command line docker login-u user_name-p password docker_registry_host, but it's not very convenient for automated processes. Using the docker-maven-plugin plug-in, we can easily achieve security authentication.
First of all, the relevant server configuration is added to the Maven configuration file setting.xml, which mainly configures the Docker registry user authentication information.
My-docker-registry wanyang3 12345678 wanyang3@mail.com
Then you just need to use server id in pom.xml.
Com.spotify docker-maven-plugin 1.0.0 registry.example.com/wanyang3/mavendemo:v1.0.0... My-docker-registry 3.7 other parameters
The docker-maven-plugin plug-in also provides a lot of useful configurations, so let's list a few parameters.
Parameter description: tag is forcibly overridden when the default value is truebuild. When using falsetruebuild with imageTags, specify-no-cache does not use cache falsetruebuild, specify-pull=true to re-pull the basic image every time after the completion of falsetruebuild, push image falsetruebuild completes, push specifies the image of tag, failed to use false5push image with imageTags, failed to retry 510push image, and retry time 10struebuild, specify-rm=true, that is, when the intermediate container falsetruebuild is deleted after build is completed. Use the first seven bits of the nearest git commit id as tag, such as image:b50b604, provided that newNamefalse4 and FAQ are not configured
1. When executing build images, an error is reported:
[INFO] Building image mavendemo
Org.apache.http.impl.execchain.RetryExec execute
I exception (java.io.IOException) caught when processing request to {}-> unix://localhost:80: No such file or directory
[ERROR] Failed to execute goal com.spotify:docker-maven-plugin:1.0.0:build (default-cli) on project mavenDemo: Exception caught: java.util.concurrent.ExecutionException: com.spotify.docker.client.shaded.javax.ws.rs.ProcessingException: java.io.IOException: No such file or directory-> [Help 1]
This is due to the fact that the Docker service is not started, just start Docker.
2. Error report 2 when executing build images:
ERROR] Failed to execute goal com.spotify:docker-maven-plugin:1.0.0:build (default-cli) on project mavenDemo: Exception caught: Request error: POST unix://localhost:80/build?t=mavenDemo: 500, body: {"message": "Error parsing reference:\" mavenDemo\ "is not a valid repository/tag: repository name must be lowercase"}: HTTP 500 Internal Server Error-> [Help 1]
This is because the name of the image is incorrect, and the name of the Docker image needs to match [a-z0-9].
This is the end of "how to package an image with docker-maven-plugin and upload it to a private warehouse". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.