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

Article directory

  • 1. Use the docker-maven-plugin plug-in to push the project to the private server docker
    • 1.1. Build image v1.0
    • 1.2. Build image v2.0
    • 1.3. Push to mirror repository
  • 2. Pull the private server docker image to run
  • 3. References

This article describes how to push the project to the private docker warehouse through the docker-maven-plugin plug-in in the Spring Boot project, and then pull the project in the warehouse to run the project with 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 server docker

1.1. Build image v1.0

1. To use docker-maven-plugin, you need to add the plugin in pom.xml;

<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 push mirror warehouse address (because it is pushed to the local docker mirror warehouse) -->
                <pushRegistry>http://localhost:5000</pushRegistry>
                <images>
                    <image>
                        <!--Because it is pushed to a private mirror warehouse, the mirror name needs to add the warehouse address (equivalent to telling where to pull the mirror)-->
                        <name>localhost:5000/fire-tiny/${project.name}:${project.version}</name>
                        <!--Define image build behavior-->
                        <build>
                            <!--Define the base image-->
                            <from>java:8</from>
                            <args>
                                <!-- The name of the jar, generally configured as the av of gav -->
                                <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
                            </args>
                            <!--Define which files are copied to the container-->
                            <assembly>
                                <!--Define the directory copied to the container-->
                                <targetDir>/</targetDir>
                                <!--Only copy the generated jar package-->
                                <descriptorRef>artifact</descriptorRef>
                            </assembly>
                            <!--Define the container startup command-->
                            <entryPoint>["java", "-jar","/${project.build.finalName}.jar"]</entryPoint>
                            <!--Definition maintainer-->
                            <maintainer>firefish</maintainer>
                            <!--Open when using Dockerfile build-->
                            <!--<dockerFileDir>${project.basedir}</dockerFileDir>-->
                        </build>
                        <!--Define container startup behavior-->
                        <run>
                            <!--Set the container name, wildcards can be used (generally configured as a of gav)-->
                            <containerNamePattern>${project.artifactId}</containerNamePattern>
                            <!--Set port mapping-->
                            <ports>
                                <port>8080:8080</port>
                            </ports>
                            <!--Set the connection between containers (that is, the container needs to connect to mysql, and the external environment needs to provide mysql connection)-->
                            <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;

# run locally
[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 set the image to be packaged directly when using the package command, modify pom.xml, and add under the node > can be configured;

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>
        <!--If you want to build a mirror when the project is packaged-->
        <execution>
            <id>build-image</id>
            <phase>package</phase>
            <goals>
                <goal>build</goal>
            </goals>
        </execution>
    </executions>
</plugin>

1.2. Build image v2.0

Building image v2.0 is an upgrade to v1.0. The original disadvantage is that the steps of building a docker image are heavily coupled with the pom code of the project, which is not conducive to later modification and the construction process leads to bloated pom files. To address these shortcomings, v2.0 uses the DockerFile method to separate the building steps of the docker image from the pom file of the Spring Boot project. Specific steps are as follows:

1. Create a new DockerFile

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

# The base image that this image needs to depend on
FROM java:8
# Copy the files under target to the container
ARG JAR_FILE
ADD target/${JAR_FILE}/
# Declare that the service runs on port 8080
EXPOSE 8080
# Specify to run the jar package when the docker container starts
ENTRYPOINT ["java", "-jar","/fire-tiny-fabric-0.0.1-SNAPSHOT.jar"]
# specify the name of the maintainer
MAINTAINER mike

2. Modify the pom file

The process of building a docker image now only has the line ${project.basedir}, 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 push mirror warehouse address (because it is pushed to the local docker mirror warehouse) -->
                <pushRegistry>http://localhost:5000</pushRegistry>
                <images>
                    <image>
                        <!--Because it is pushed to a private mirror warehouse, the mirror name needs to add the warehouse address (this is equivalent to telling others where to pull the mirror)-->
                        <name>localhost:5000/fire-tiny/${project.name}:${project.version}</name>
                        <!--Define image build behavior-->
                        <build>
                            <!--Open when using Dockerfile build-->
                            <dockerFileDir>${project.basedir}</dockerFileDir>
                        </build>
                    </image>
                </images>
            </configuration>
        </plugin>
    </plugins>
</build>

3. Package, build, and view the image

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

# package build
mvn clean package docker:build
# View local mirrors
docker images

1.3. Push to mirror warehouse

1. Specify build and push to 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 to run

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

1. Pull the image to the local

Because we build the image locally and then push it to the private warehouse, we need to delete the original 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 of the container

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

But unfortunately, checking the docker log will display database-related errors without accident.

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 with 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 in /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 just 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
    
    # Mapping of domain name db and host 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"
    
# test interface
curl -X GET --header 'Accept: application/json' 'http://localhost:8080/brand/list?pageNum=1 &pageSize=3'

3. Reference

My article: “How to check which version a Docker image has.md”

My article: “Docker Setting 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 Install Nacos Service.md”

My article: “How to modify the file.md in Docker”

My article: “Connection or communication between Docker containers.md”

My article: “How MySQL installed by Docker persists database data.md”

My article: “Making Docker Private Warehouse.md”

My article: “Using the docker-maven-plugin plugin to build, publish, and push images to private warehouses.md”

My article: “Solve the failure to access port 9200 after Docker installed Elasticsearch.md”

Portal: Nanny Spring5 source code analysis

Welcome to exchange technology and work life with the author

Contact the author