Use the maven plugin dockerfile-maven-plugin in IDEA to make and push the Docker image to the private warehouse (Harbor)

background

A parent-child project built with maven. There are multiple sub-projects under the parent project. The function to be realized now is to package a certain project as a docker image and then push it to a Docker mirror warehouse (Harbor mirror warehouse).

dockerfile-maven-plugin

In the parent pom, import the plugin:

<pluginManagement>
     <plugins>
         <plugin>
             <groupId>com.spotify</groupId>
             <artifactId>dockerfile-maven-plugin</artifactId>
             <version>${docker.maven.plugin.version}</version>
             <executions>
                 <execution>
                     <id>default</id>
                     <goals>
                         <!--如果package时不想用docker打包,就注释掉这个goal-->
                         <goal>build</goal>
                         <goal>push</goal>
                     </goals>
                 </execution>
             </executions>
             <configuration>
                 <contextDirectory>${project.basedir}</contextDirectory>
                 <useMavenSettingsForAuth>true</useMavenSettingsForAuth>
                 <repository>${docker.repostory}/${docker.registry.name}/${project.artifactId}</repository>
                 <buildArgs>
                     <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
                 </buildArgs>
             </configuration>
         </plugin>
     </plugins>
 </pluginManagement>

Among them, the attribute configuration of the plug-in is as follows:

<properties>
    <!--Harbor仓库的地址,ip:port-->
    <docker.repostory>192.168.1.6:9001</docker.repostory>
    <!--上传的Docker镜像前缀,此前缀一定要和Harbor中的项目名称一致,表示打包后的Docker镜像会上传到Harbor的哪个项目中-->
    <docker.registry.name>fyk_project</docker.registry.name>
    <docker.maven.plugin.version>1.4.10</docker.maven.plugin.version>
</properties>

In this way, in each sub-project, you only need to perform some other personalized configurations, such as configuring the label of the image, as follows:

<finalName>fyk-config</finalName>
<plugins>
    <plugin>
        <groupId>com.spotify</groupId>
        <artifactId>dockerfile-maven-plugin</artifactId>
        <configuration>
            <tag>${docker.image.tag}</tag>
        </configuration>
    </plugin>
</plugins>

The configuration properties here are:

<properties>
    <!--Docker镜像的标签,也就是版本-->
    <docker.image.tag>v1.0.0</docker.image.tag>
</properties>

At this point, in IDEA, the maven configuration of dockerfile-maven-plugin is complete. However, mirroring and pushing mirroring cannot be made at this time, and the other two configurations need to be mirrored.

Make a mirror configuration

After completing the maven plug-in configuration of IDEA, now we start to make the image.
First of all, it is necessary to understand: the production of the image and the push operation are all done by Docker, so the Docker environment must be installed. Simply put, dockerfile-maven-plugin just simplifies the complexity of directly operating Docker. It should be done by Docker, and it must be done by Docker.
So, after understanding this point, you know that it is not necessary to install the Docker environment locally, as long as there is a Docker environment.
Since it is enough to have a Docker environment, here I use the Docker environment on the server (of course, you can also use the local Docker environment, using the local Docker environment does not require additional configuration, just follow the tutorial to install Docker locally) . To use the server's Docker environment, you need to configure an environment variable (by default, the plug-in is connected to the local Docker environment, that is, 127.0.0.1), the configuration of the environment variable is as follows, just change it to your own server Docker environment (may require Restart the computer ):
DOCKER_HOST tcp://192.168.1.6:2375
insert image description here
At this time, there is one more step in the operation of making a Docker image. Write a Dockerfile: create a Dockerfile in the directory of the subproject (same level as the pom file), The content is as follows:

FROM frolvlad/alpine-oraclejdk8
EXPOSE 10000
ADD target/fyk-config.jar /fyk-config.jar
ENTRYPOINT exec java -jar /fyk-config.jar

Ok, now execute the mvn clean package packaging command to create the image. After the command is executed, you can view the image in the Docker environment. The following is the image I just made:
insert image description here

Push the image to the warehouse

Since the warehouse I want to push is private and requires a username and password, I need to add the following configuration in the maven configuration file (setting file) :

<servers>
	<server>
		<id>192.168.1.6:9001</id>
		<username>fyk</username>
		<password>Fyk123456</password>
		<configuration>
			<email>[email protected]</email>
		</configuration>
	</server>
</servers>

The id here is the IP and port of the warehouse.
After configuration, execute the command:

mvn dockerfile:push

The meaning of this command is to push the image to the mirror warehouse. The pushed image is the image just generated in Docker (in the instance, this command pushes the image in Docker to the warehouse, and it does not matter how the image is created). After executing this command, check the Harbor warehouse:
insert image description hereAt this point, IDEA uses the maven plug-in dockerfile-maven-plugin to create and push the Docker image to the private warehouse (Harbor) and it is completed.

question

Can't connect to remote Docker service?
This problem may be caused by the firewall (this is simple, turn off the firewall or open a double-sided wall, if the wall cannot be opened, install Docker locally); if the wall is open, it may be that Docker has not enabled remote access: open docker’s
remote Access:
Enter /lib/systemd/system/docker.service

vim /lib/systemd/system/docker.service

Find the ExecStart line and modify it to the following

ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock

restart the daemon

systemctl daemon-reload

restart docker

systemctl restart docker

Verify
http://ip:2375/images/json with a browser
so far, the remote access of docker is opened.


During the operation process, a certain step of the operation fails, for example, the failure prompts that docker login is required, and so on.
In fact, this problem has nothing to do with the dockerfile-maven-plugin plugin. First of all, understand that the plug-in is just to simplify the operation. It should be done by Docker, or it must be done by Docker. Therefore, if there are too many such problems, you can manually upload an image to the warehouse in the docker environment to try. If the upload is successful, then according to the method described in this article, it should all be successful. If the manual upload fails, there is probably something wrong with your environment. For example, if docker login is prompted, then just log in. For specific questions, you can refer to other materials again.


Error: Get https://192.168.1.6:9001/v2/: http: server gave HTTP response to HTTPS client
In the Docker environment (on the server) file: /etc/docker/daemon.json, add:
"insecure -registries":["192.168.1.6:9001"]
as follows:
insert image description here


Guess you like

Origin blog.csdn.net/fyk844645164/article/details/107878063