[Docker] Use the docker-maven-plugin plug-in to build, publish and push the image to a private warehouse


This article describes how to push the project to a private docker warehouse through the docker-maven-plugin plug-in in the Spring Boot project, and then pull the project in the warehouse and run the project using docker run. The author builds it himself and the quality is guaranteed.

1. Use the docker-maven-plugin plug-in to push the project to the private docker server

1.1. Build image v1.0

1. To use it docker-maven-plugin, you need to pom.xmladd the plug-in;

<build>
     <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>io.fabric8</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <version>0.33.0</version>
            <configuration>
                <!-- Docker 推送镜像仓库地址(由于是推送到本地的docker镜像仓库) -->
                <pushRegistry>http://localhost:5000</pushRegistry>
                <images>
                    <image>
                        <!--由于推送到私有镜像仓库,镜像名需要添加仓库地址(相当于告诉去哪里拉取镜像)-->
                        <name>localhost:5000/fire-tiny/${project.name}:${project.version}</name>
                        <!--定义镜像构建行为-->
                        <build>
                            <!--定义基础镜像-->
                            <from>java:8</from>
                            <args>
                                <!-- jar的名称,一般配置为gav的av -->
                                <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
                            </args>
                            <!--定义哪些文件拷贝到容器中-->
                            <assembly>
                                <!--定义拷贝到容器的目录-->
                                <targetDir>/</targetDir>
                                <!--只拷贝生成的jar包-->
                                <descriptorRef>artifact</descriptorRef>
                            </assembly>
                            <!--定义容器启动命令-->
                            <entryPoint>["java", "-jar","/${project.build.finalName}.jar"]</entryPoint>
                            <!--定义维护者-->
                            <maintainer>firefish</maintainer>
                            <!--使用Dockerfile构建时打开-->
                            <!--<dockerFileDir>${project.basedir}</dockerFileDir>-->
                        </build>
                        <!--定义容器启动行为-->
                        <run>
                            <!--设置容器名,可采用通配符(一般配置为gav的a)-->
                            <containerNamePattern>${project.artifactId}</containerNamePattern>
                            <!--设置端口映射-->
                            <ports>
                                <port>8080:8080</port>
                            </ports>
                            <!--设置容器间连接(即容器需要连接mysql,需要外部环境提供mysql连接)-->
                            <links>
                                <link>mysql:db</link>
                            </links>
                        </run>
                    </image>
                </images>
            </configuration>
        </plugin>
    </plugins>
</build>

Note: Pay attention to db:3306

spring:
  datasource:
    url: jdbc:mysql://db:3306/fire?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
    username: root
    password: root

2. Before we build the image, we need to package the project first, and then build it, otherwise there will be an error, just use the following command directly

mvn package docker:build

3. After the packaging is completed, we can see this image on our local;

# 本地运行
[root@linux-local work]# docker images
REPOSITORY                                             TAG              IMAGE ID       CREATED             SIZE
localhost:5000/fire-tiny/fire-tiny-fabric              0.0.1-SNAPSHOT   9b7cf9c38c5d   About an hour ago   680MB

4. Of course, we can also packagedirectly package the image when using the command, modify it pom.xml, and <plugin>add <executions>configuration under the node;

It is an additional addition; if you don't build it, just build the docker image when you need it

<plugin>
    <groupId>io.fabric8</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>0.33.0</version>
    <executions>
        <!--如果想在项目打包时构建镜像添加-->
        <execution>
            <id>build-image</id>
            <phase>package</phase>
            <goals>
                <goal>build</goal>
            </goals>
        </execution>
    </executions>
</plugin>

1.2. Build image v2.0

Building the image v2.0 is an upgrade to v1.0. The original shortcomings are that the steps to build the docker image are seriously coupled with the project's pom code, which is not conducive to later modifications and the build process causes the pom file to be bloated. To address these shortcomings, v2.0 uses the DockerFile method to separate the construction steps of the docker image from the pom file of the Spring Boot project. Specific steps are as follows:

1. Create a new DockerFile file

Create a new DockerFile file under the project, and customize the content. The reference content is as follows:

# 该镜像需要依赖的基础镜像
FROM java:8
# 拷贝target下的文件到容器中
ARG JAR_FILE
ADD target/${JAR_FILE} /
# 声明服务运行在8080端口
EXPOSE 8080
# 指定docker容器启动时运行jar包
ENTRYPOINT ["java", "-jar","/fire-tiny-fabric-0.0.1-SNAPSHOT.jar"]
# 指定维护者的名字
MAINTAINER mike

2. Modify the pom file

The process of building a docker image now only has <dockerFileDir>${project.basedir}</dockerFileDir>this line, which is very concise.

<build>
     <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>io.fabric8</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <version>0.33.0</version>
            <configuration>
                <!-- Docker 推送镜像仓库地址(由于是推送到本地的docker镜像仓库) -->
                <pushRegistry>http://localhost:5000</pushRegistry>
                <images>
                    <image>
                        <!--由于推送到私有镜像仓库,镜像名需要添加仓库地址(这个相当于告诉别人拉取镜像的时候去哪里拉取)-->
                        <name>localhost:5000/fire-tiny/${project.name}:${project.version}</name>
                        <!--定义镜像构建行为-->
                        <build>
                            <!--使用Dockerfile构建时打开-->
                            <dockerFileDir>${project.basedir}</dockerFileDir>
                        </build>
                    </image>
                </images>
            </configuration>
        </plugin>
    </plugins>
</build>

3. Package, build, and view images

Take it away in a set of 3 steps, which is much simpler than the original and looks comfortable.

# 打包构建
mvn clean package docker:build
# 查看本地镜像
docker images

1.3. Push to the mirror warehouse

1. Specify build and push to the private warehouse

2. Log in to the private warehouse address: http://localhost:8280/, and view the image just pushed

2. Pull the private server docker image and run it

After pushing the image to the private warehouse, you need to pull the image locally and use the image.

1. Pull the image to local

Because we built the image locally and then pushed it to the private warehouse, we need to delete the originally built image first, and then go to the private warehouse to pull the image.

docker rmi "localhost:5000/fire-tiny/fire-tiny-fabric:0.0.1-SNAPSHOT"
docker pull "localhost:5000/fire-tiny/fire-tiny-fabric:0.0.1-SNAPSHOT"

2. Run the container

docker run --rm -d --name fire-tiny-fabric -p 8080:8080 "localhost:5000/fire-tiny/fire-tiny-fabric:0.0.1-SNAPSHOT"

3. Access one of the interfaces in the container

curl -X GET --header 'Accept: application/json' 'http://localhost:8080/brand/list?pageNum=1&pageSize=3'

But unfortunately, checking the docker log will show database-related errors as expected.

This is because when we built the image of the fire-tiny-fabric project in the previous step, we specified that we need to rely on the mysql database, but we did not specify the database in docker run, so there will be errors in database connection.

4. Rerun the container

  • If there is a database built using docker, specify the mysql database through –link:

    docker run --rm -d --name fire-tiny-fabric -p 8080:8080 \
    --link mysql:db \
    "localhost:5000/fire-tiny/fire-tiny-fabric:0.0.1-SNAPSHOT"
    

    Note: mysql in mysql:db is the name of the container (–name), and the following db is the variable specified when building fire-tiny-fabric. The principle of –link is to add an alias name to /etc/hosts.

  • If it is a database built locally, specify the IP address and port

    We use db as the domain name to connect to the database in the project, so we only need to add a domain name mapping from db to the host IP address to the container.

    spring:
      datasource:
        url: jdbc:mysql://db:3306/fire?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
        username: root
        password: root
    
    # 域名db与主机ip的映射
    docker run --rm -d --name fire-tiny-fabric -p 8080:8080 \
    --add-host=db:192.168.1.6 \
    "localhost:5000/fire-tiny/fire-tiny-fabric:0.0.1-SNAPSHOT"
    
# 测试接口
curl -X GET --header 'Accept: application/json' 'http://localhost:8080/brand/list?pageNum=1&pageSize=3'

3. References

My article: "How to check which versions of a Docker image.md"

My article: "Docker sets domestic image source.md"

My article: "Docker Quick Start Practical Tutorial.md"

My article: "Docker installs MySQL, Redis, RabbitMQ, Elasticsearch, Nacos and other common services.md"

My article: "Docker installs Nacos service.md"

My article: "How to modify file .md in Docker"

My article: "Connection or communication methods between Docker containers.md"

My article: "How to persist database data with MySQL installed by Docker.md"

My article: "Making Docker Private Repository.md"

My article: "Using the docker-maven-plugin plug-in to build and publish push images to private warehouses.md"

My article: "Resolving Docker's failure to access port 9200 after installing Elasticsearch.md"


Portal: Nanny-style Spring5 source code analysis

Welcome to exchange technology and work life with the author

Contact the author

Guess you like

Origin blog.csdn.net/yuchangyuan5237/article/details/131971906