Spotify officials no longer recommend the use of docker Maven plugin and do not maintain it. Instead, spotify recommends another Maven plugin dockerfile Maven plugin developed by the company.
Today, let's introduce how to use the plug-in.
Build Docker image
- Configure POM xml
First, in POM XML and configure the dockerfile Maven plugin.
If it is a mirror warehouse Harbor, configure the private server address of the mirror warehouse
<properties> <!--docker Private server address--> <docker.repostory>harbor.hngtrust.com</docker.repostory> </properties>
If it is the Docker Hub of the image warehouse, configure the private server address of the image warehouse
<properties> <!--docker Private server address--> <docker.repostory>docker.io</docker.repostory> </properties>
Add the configuration of dockerfile Maven plugin. Take eureka as an example:
<plugin> <groupId>com.spotify</groupId> <artifactId>dockerfile-maven-plugin</artifactId> <version>1.4.13</version> <executions> <execution> <id>harbor</id> <goals> <goal>build</goal> <goal>push</goal> </goals> </execution> </executions> <configuration> <repository>${docker.repostory}/library/${project.artifactId}</repository> <tag>${project.version}</tag> <useMavenSettingsForAuth>true</useMavenSettingsForAuth> <username>admin</username> <password>Harbor12345</password> <buildArgs> <JAR_FILE>${project.build.finalName}.jar</JAR_FILE> </buildArgs> </configuration> </plugin>
You can see that the configuration of this plug-in is simpler than that of docker Maven plugin.
repository specifies the repo name of the docker image.
Tag specifies the tag of the docker image.
buildArgs can specify one or more variables, pass them to Dockerfile, and reference them in Dockerfile through ARG instruction.
In addition, you can specify both build and push targets in execution. When running mvn package, the build target will be executed automatically to build Docker image. When the mvn deploy command is run, the push target will be automatically executed to push the Docker image to the Docker warehouse.
- Write Dockerfile
Unlike the Docker made plugin, the plug-in does not need to prepare dockerfiles. Instead, the plug-in automatically generates dockerfiles by configuring pom, and uses the generated dockerfiles to build Docker images.
The plug-in requires that the Dockerfile file must be provided and placed in the project root directory, that is, with POM XML sibling directory. Then, like the docker made plugin plug-in, you do not need to specify the dockerDirectory parameter of the Dockerfile file storage path.
Moreover, an important function of using this plug-in is that we can reference the artifact s built by maven, such as jar package, in the relative path starting with target in Dockerfile.
As shown in the Dockerfile file below:
FROM java:8 RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone ENV ACTIVE="" ADD target/dhcc-eureka-server-0.1.1-SNAPSHOT.jar dhcc-eureka-server.jar ENTRYPOINT ["java", "-jar","-Dspring.profiles.active=${ACTIVE}", "/dhcc-eureka-server.jar"]
We can see that after the ADD instruction, the relative path starting with target is used to refer to the jar package built by maven.
In addition, we can see from the above example that we have used another new function provided by the plug-in. As mentioned earlier, in POM The variable specified by buildArgs in XML can be referenced in Dockerfile through ARG instruction. ENV ACTIVE = "" can then be referenced in other parts of Dockerfile in the form of ${ACTIVE}. POM The variable value passed in XML can be started in different environments.
- Build Docker image
To build a Docker image using this plug-in, you need an installed Docker running environment.
And you need to define Docker on the machine running the plug-in_ Host environment variable, configure the URL to access Docker, as follows:
export DOCKER_HOST=tcp://localhost:2375
The above example is the Docker defined in the Linux environment_ Host environment variable. Because Docker is installed locally, localhost is used. If your Docker running environment is not on this machine, please use the IP of the machine where Docker is located
2375 is the port of the remote access API opened for Docker. If you open other ports, please use the specific port. If your Docker does not open the remote access API, please open it yourself.
If you want to run the Maven plug-in on Windows, you also need to configure Docker on Windows_ Host environment variable, the value is: tcp://localhost:2375 , you can run the Maven command to build the Docker image:
mvn package
or
mvn dockerfile:build
After the command is executed successfully, run the docker command to check whether the image exists:
docker images
Push Docker image
After the Docker image is built, we can also use the plug-in to push the image to the Docker warehouse, such as the Docker Hub.
The following describes how to push to the Docker Hub.
- Create a repository
First, we need to log in to the Docker Hub and create a repository, as shown in the following figure. We created the repository of config server:
- Configure POM xml
The repository of docker hub must be in the following format:
/<repository_name>
<configuration> <repository>jwangkun/dhcc-eureka-server</repository> <tag>${project.version}</tag> <buildArgs> <JAR_FILE>${project.build.finalName}.jar</JAR_FILE> </buildArgs> </configuration>
username is the user name of the Docker Hub. For example, my user name is jwangkun, repository_name is the name of the repository created on the Docker Hub in the previous step.
Harbor is in POM The repository name of the Docker image configured for the plug-in in XML is as follows:
<configuration> <repository>${docker.repostory}/library/dhcc-eureka-server</repository> <tag>${project.version}</tag> <buildArgs> <JAR_FILE>${project.build.finalName}.jar</JAR_FILE> </buildArgs> </configuration>
Note: library is the name of the project created in the harbor warehouse, and ${docker. Repository} is the above POM Docker.xml for properties configuration Value of memory
- Configuration authentication
Next, you need to configure the authentication information for the plug-in, that is, the user name and password to log in to the Docker Hub. Because the plug-in needs to log in to the Docker Hub before push ing the image.
There are three ways to configure authentication information.
The first: in POM Configuring authentication information in XML
As shown below, you can directly in POM XML to configure usnername and password for the plug-in:
<configuration> <repository>jwangkun/dhcc-eureka-server</repository> <tag>${project.version}</tag> <username>jwangkun</username> <password>xxxx</password> <buildArgs> <JAR_FILE>${project.build.finalName}.jar</JAR_FILE> </buildArgs> </configuration>
Second: in settings Configure server in XML
As shown below, in maven's settings Add a server configuration under servers in XML:
For Docker Hub, server ID must be http://docker.io .
<servers> <server> <id>docker.io</id> <username>jwangkun</username> <password>xxxx</password> </server> </servers>
For Harbor, server The ID must be changed to Harbor service address and port.
<servers> <server> <id>192.168.1.1:9001</id> <username>admin</username> <password>*****</password> <configuration> <email>******@qq.com</email> </configuration> </server> </servers>
Then in POM useMavenSettingsForAuth specified in XML is true:
<configuration> <repository>jwangkun/dhcc-eureka-server</repository> <tag>${project.version}</tag> <useMavenSettingsForAuth>true</useMavenSettingsForAuth> <buildArgs> <JAR_FILE>${project.build.finalName}.jar</JAR_FILE> </buildArgs> </configuration>
The third is to provide authentication information directly on the mvn command line
If not in POM The authentication information is not configured in settings.xml If the authentication information is configured in XML, we can also simply provide the authentication information in the form of parameters on the running mvn command line, as shown below:
mvn dockerfile:push -Ddockerfile.username=jwangkun -Ddockerfile.password=xxxx
The first way, due to the need in POM The user password is configured in XML, while POM XML is generally submitted to the version control system along with the source code, so there is a risk of exposing the user password.
The third way is to provide an additional user name and password every time you run the maven command, which is troublesome.
Therefore, the second method is generally recommended.
- push image
Next, execute the maven command to push the image.
For the first two authentication methods, we can directly run the mvn goal command to push the image:
mvn deploy
perhaps
mvn dockerfile:push
For the third authentication method, use the command mentioned above to push the image.
mvn dockerfile:push -Ddockerfile.username=jwangkun -Ddockerfile.password=xxxx
It should be noted that if you do not configure the maven repository for deploying artifact s, please do not use the mvn deploy command, because it will execute the deploy target of Maven deploy plugin. Because the remote maven repository to be deployed is not configured, an error similar to the following will be reported:
Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter
In this case, use the following command to push the image:
mvn dockerfile:push
- After the maven command to check the image is executed successfully, you can log in to the Docker Hub to check whether the image exists.
Push image to Harbor warehouse
Since the warehouse I want to push is private and requires user name and password, the following configuration should be added to maven's configuration file (setting file):
<configuration> <repository>${docker.repostory}/library/${project.artifactId}</repository> <tag>${project.version}</tag> <useMavenSettingsForAuth>true</useMavenSettingsForAuth> <username>admin</username> <password>Harbor12345</password> <buildArgs> <JAR_FILE>${project.build.finalName}.jar</JAR_FILE> </buildArgs> </configuration>
Note that if it is a Harbor warehouse, use maven settings for auth must be added. It seems to be in maven settings If the servers in xml do not take effect or for other reasons, the address will be reported as wrong or the user name and password cannot be logged in. Add useMavenSettingsForAuth
<useMavenSettingsForAuth>true</useMavenSettingsForAuth>
After configuration, execute the command:
mvn dockerfile:push
Next: Kubernetes Ingress→