This article introduces the techniques for using Maven Docker images and shares them with you, as follows:
Maven is one of the most popular Java project management tools at present, providing powerful package dependency management and application construction functions.
Docker provides official Maven images that can be used to manage and build Java applications. Compared to directly installing the Maven tool, using Docker images has better portability and can be easily switched versions, which is ideal for use during continuous integration.
For the usage of Maven official mirror, please refer to the usage documentation.
Using Alibaba Cloud to accelerate
The download speed of Maven's official warehouse on the domestic network is really heartbreaking. Using Alibaba Cloud's Maven image can greatly improve the download speed of software packages.
We can add Alibaba Cloud image configuration based on the official Maven image. Its code is available at https://github.com/AliyunContainerService/maven-image
Its configuration file settings.xml is as follows
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd"> <localRepository>/usr/share/maven/ref/repository</localRepository> <mirrors> <mirror> <!--This sends everything else to /public --> <id>aliyun-nexus</id> <mirrorOf>*</mirrorOf> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> </mirror> <mirror> <!--This is used to direct the public snapshots repo in the profile below over to a different nexus group --> <id>aliyun-nexus-public-snapshots</id> <mirrorOf>public-snapshots</mirrorOf> <url>http://maven.aliyun.com/nexus/content/repositories/snapshots/</url> </mirror> </mirrors></settings>
Alibaba Cloud Container Service provides pre-built Docker images for direct use of registry.cn-hangzhou.aliyuncs.com/acs/maven.
We can directly execute the following command in the current directory like using the mvn command to build the application
Copy the code as follows: docker run -it --rm --name maven -v "$(pwd)":/usr/src/app -w /usr/src/app registry.cn-hangzhou.aliyuncs.com/acs/maven mvn clean install
If you want to be able to cache the downloaded maven repository, we can use Docker's file volume to implement it
First, execute the following command to create a file volume called "maven-repo"
docker volume create --name maven-repo
In the subsequent call, mount it to the repository download directory in the maven image
Copy the code as follows: docker run -it --rm --name maven -v "$(pwd)":/usr/src/app -v maven-repo:/usr/share/maven/ref -w /usr/src/app registry.cn-hangzhou.aliyuncs.com/acs/maven mvn clean install
In this way, the maven repository will not be downloaded every time.
Optimize Dockerfile to improve build speed
We can build applications in Dockerfile and use the hierarchical caching mechanism during Docker construction to improve the build speed
Here is a sample Dockerfile.build file
FROM registry.cn-hangzhou.aliyuncs.com/acs/maven:3-jdk-8ENV MY_HOME=/usr/src/appRUN mkdir -p $MY_HOMEWORKDIR $MY_HOMEADD pom.xml $MY_HOME# get all the downloads out of the wayRUN ["/usr/local/bin/mvn-entrypoint.sh","mvn","verify","clean","--fail-never"]# add sourceADD . $MY_HOME# run maven verifyRUN ["/usr/local/bin/mvn-entrypoint.sh","mvn","verify"]
One of the important tricks is to first add pom.xml to the working directory, use the maven command to download the jar package required by the application, and then add the application source file for compilation. In this way, as long as the pom.xml is not updated, the dependency jar package will not be re-downloaded, which can greatly speed up the image construction speed.
We can compile the application through the following command
docker build -t builder-img -f Dockerfile.build .
Separate application compilation and Docker image construction
For static compiled languages, we usually need to separate the application compilation process from the mirror construction process. There are two main considerations:
We can copy the application compilation results from the Docker image, as follows
docker build -t builder-img -f Dockerfile.build .docker create --name builder builder-imgdocker cp builder:/usr/src/app/target ./target
At this time, the result of maven construction is copied to the "target" subdirectory of the current directory.
After that, we can use another Dockerfile to build application images. I won't go into details if I have limited space.
Summarize
This article uses Maven as an example to introduce some common techniques for Docker in application construction.
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.