[Java] Vert.x uses Maven to build a Docker image and upload a private library

In order to learn the knowledge of Docker and automated operation and maintenance, a complete set of operation and maintenance tools was also deployed in the home machine. I have to say that this Docker image is almost a disk killer, and a complete openj9 jdk image is almost 500MB. Fortunately, we don't do Nightly Build, otherwise the space will be gone all at once.

As more and more attempts were made later, the whole environment became more and more bloated (adding Nexus, adding Gitlab, adding Jenkins, adding Portainer...), at this time I had to consider a more lightweight solution . Until I found out that it could be packaged into a Docker image through Maven's Pom configuration, I decided to make drastic changes as follows:

  1. It doesn't make much sense to cut off Gitlab and develop Gitlab on your own machine and deploy it on your own machine. If the disk is broken, it should be gone or it will be gone;
  2. Cut off Nexus, the code version library is gone, what else does the code package management library do;
  3. Cutting down Jenkins can be packaged with Maven direct commands, so Jenkins is not needed for continuous integration;
  4. Cut off the Portainer, this can be cut or not, it is purely because the resources are not enough, so it is cut;

After a set of operations, I found that I can still fight for a few years. Sure enough, "poor" means change and change.

The key to realizing Maven to build a Docker image is the Pom file. The Pom file on my side is written like this:

<properties>
	...
	
	<!-- docker参数 -->
    <!-- 这里是maven docker组件的版本号 -->
	<docker-maven.version>0.36.0</docker-maven.version>
    <!-- 这里是镜像仓库的仓库名称 -->
	<docker.image.registry>vtx</docker.image.registry>
    <!-- 这里是目标仓库的ip和端口地址 -->
	<target-ip.repo>192.168.100.152:5000</target-ip.repo>
	<!-- docker私有仓库机器的tcp2375端口指向 -->
	<docker-host.name>tcp://192.168.100.152:2375</docker-host.name>
    <!-- 这里是私有仓库用户名 -->
	<docker.username>yzh</docker.username>
    <!-- 这里是私有仓库密码 -->
	<docker.password>Pwd</docker.password>
    ...
</properties>
...

<build> 
  <plugins>
    ... 
    <plugin> 
      <groupId>io.fabric8</groupId>  
      <artifactId>docker-maven-plugin</artifactId>  
      <version>${docker-maven.version}</version>  
      <configuration> 
        <!-- 远程docker守护进程url -->  
        <dockerHost>${docker-host.name}</dockerHost>  
        <!-- 远程push权限 -->  
        <authConfig> 
          <push> 
            <username>${docker.username}</username>  
            <password>${docker.password}</password> 
          </push> 
        </authConfig>  
        <images> 
          <image> 
            <!-- 镜像名称(这里注意要包含远程目标仓库的访问前缀) --> 
            <!-- 例子中的是192.168.100.152:5000,不然它会找docker.io上面的 --> 
            <name>${target-ip.repo}/${docker.image.registry}/${project.artifactId}:${project.version}</name>  
            <!-- 构建内容 -->  
            <build> 
              <!-- 基础镜像(采用了jdk11 openj9版本) -->  
              <from>adoptopenjdk:11-openj9</from>  
              <!-- 作者名称 -->  
              <maintainer>[email protected]</maintainer>  
              <!-- 镜像开放端口 -->  
              <ports> 
                <port>8197</port> 
              </ports>  
              <!-- 镜像内挂载目录 -->  
              <volumes> 
                <volume>/tmp</volume> 
              </volumes>  
              <!-- 镜像内工作目录 -->  
              <workdir>/usr/local/share</workdir>  
              <!-- dockerfile中jar包启动参数 -->  
              <entryPoint> 
                <exec> 
                  <arg>java</arg>  
                  <arg>-jar</arg>  
                  <arg>${project.artifactId}-${project.version}.jar</arg>  
                  <arg>-XX:MaxGCPauseMillis=200</arg>  
                  <arg>-Xloggc:/tmp/${project.artifactId}/gc.log</arg>  
                  <arg>-XX:+PrintGCDetails</arg>  
                  <arg>-XX:+PrintGCDateStamps</arg>  
                  <arg>-XX:+UnlockExperimentalVMOptions</arg>  
                  <arg>-XX:+UseG1GC</arg>  
                  <arg>-Djava.security.egd=file:/dev/./urandom</arg>  
                  <arg>-XX:+UseCGroupMemoryLimitForHeap</arg>  
                  <arg>-XX:MaxRAMFraction=1</arg>  
                  <arg>-XX:+HeapDumpOnOutOfMemoryError</arg>  
                  <arg>-cluster</arg> 
                </exec> 
              </entryPoint>  
              <!-- 原始包位置与镜像的前期准备 -->  
              <assembly> 
                <!-- 上传模式 -->  
                <mode>dir</mode>  
                <!-- 上传目录 -->  
                <targetDir>/usr/local/share</targetDir>  
                <!-- jar包描述 -->  
                <descriptorRef>artifact</descriptorRef> 
              </assembly> 
            </build> 
          </image> 
        </images> 
      </configuration>  
      <!-- docker操作执行 -->  
      <executions> 
        <!-- 镜像打包(本地产生镜像) -->  
        <execution> 
          <id>push-image</id>  
          <!-- 对应maven指令package -->  
          <phase>package</phase>  
          <!-- 执行docker命令build\push -->  
          <goals> 
            <!-- 先build,后push -->  
            <goal>build</goal>  
            <goal>push</goal> 
          </goals>  
          <configuration>  
            <imageName>${target-ip.repo}/${docker.image.registry}/${project.artifactId}:${project.version}</imageName> 
          </configuration> 
        </execution> 
      </executions> 
    </plugin> 
  </plugins> 
</build>

(Here I am doing a direct Docker build for the Vert.x application. If it is a Springboot project, some adjustments are required.)

At the beginning, you may also report the following question, as shown in the figure below:

Failed to execute goal io.fabric8:docker-maven-plugin:0.33.0:build (build-image) on project phw2-bi-vtx: Cannot create docker access object: Connect to 127.0.0.1:2375 [/127.0.0.1] failed: Connection refused (Connection refused) -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal io.fabric8:docker-maven-plugin:0.33.0:build (build-image) on project phw2-bi-vtx: Cannot create docker access object

This problem may be caused by the fact that port 2375 (Docker's external development port) is not opened. Check whether port 2375 is open through the following statement:

# 查看2375端口是否开启
nc -vz -w 2 localhost 2375

nc: connectx to localhost port 2375 (tcp) failed: Connection refused
nc: connectx to localhost port 2375 (tcp) failed: Connection refused

At this time, the port needs to be opened by starting a special image, as shown in the following figure:

docker run -it -d \
--name=socat \
-p 2375:2375 \
-v /var/run/docker.sock:/var/run/docker.sock \
--restart=always \
bobrik/socat:latest \
TCP4-LISTEN:2375,fork,reuseaddr \
UNIX-CONNECT:/var/run/docker.sock

Then you can use Maven to build and upload the private library normally.

Guess you like

Origin blog.csdn.net/kida_yuan/article/details/129683441