How to manage Spring boot project docker image based on dockerfile maven plugin

Configure Open JDK

This project is built on JDK11, so to run this project, you need JDK11 compatible JDK. Considering that Oracle Commercial JDK charges enterprises, we recommend that you use Open JDK 11 built by Oracle, which can be downloaded here.

As shown in the figure below, on the Archived OpenJDK General-Availability Releases page, we find the group named 11 GA (build 11 + xx). Here, we can find the relevant versions of the binary and corresponding to the Windows, Linux and Mac operating systems. Source code download address.

For most people, it is enough to complete the development of the project based on Open JDK 11 built by Oracle, but if you want to regularly obtain the build version of Open JDK 11 with the first patch update, I prefer the Open JDK maintained by the Redhat team.The Red Hat build of OpenJDK is a free and open source implementation of the Java Platform, Standard Edition (Java SE).OracleJDK and OpenJDK are functionally very similar but have major differences when it comes to support. Red Hat’s build of OpenJDK is a great alternative.Click here to enter the AAA download page.

在这里插入图片描述

Configure maven

Edit the file ~/.m2/settings.xml, find the tag named servers, and add the following content to it:

        <server>
            <id>official-docker</id>
            <username>qwfys</username>
            <password>********</password>
            <configuration>
                <email>qwfys200@qq.com</email>
            </configuration>
        </server>

~ Is a symbolic representation of the user directory, if we have a user with a login name of lwk, under normal circumstances, in the Linux / Uninx system, the root user directory is / root, and the non-root user directory is / home / lwk . In Windows 7 / 8.x / 10, the user directory is C: \ Users \ lwk.

安装docker到Windows 8+上

  • 开机主机的虚拟化配置

安装docker前,需要在开机的时候进入主机BIOS配置中,将CPU虚拟化支持功能启用。具体步骤如下:

步骤1:电脑开机的时候,长按F2进入BIOS配置界面。

步骤2:在CPU相关配置项中查找名称为"Intel (VMX) Virtualization Technology
"的配置项,确保其为"Enabled"状态。

  • 下载与安装

Docker针对Windows、Linux、Mac系统都有相应的版本可以供大家使用,这里着重介绍Docker的Windows版本,根据Docker官主建议,Docker目前仅支持Windows 7+以上系统,点击这里可以进入Docker官方Windows下载页面进行下载。但是考虑到海外链接,下载的时候,速度太慢,这里我已经下载了一个版本放在我的微云上了,现分享给大家,大家可以点击这里前往微云下载。

  • 阿里云加速器

docker在运行的过程中需要经常前往dokcer hub上获取基础镜像,但是因为docker hub部署在海外,国内小伙伴在使用的过程中需要等待比较久的时候,这个时候,我们可以借助国内的docker加速器来解决docker获取基础镜像慢的问题。docker加速器,本质上是docker hub的一个镜像。关于阿里云docker加速器配置,我此前整理过相应的文档,大家可以点击这里,前往阅读。

project结构介绍

  • Dockerfile

Dockerfile is common to all projects and is located in the root directory of the project. The contents are as follows:

FROM openjdk:11-jdk
VOLUME /tmp
ARG JAR_FILE
ADD ${JAR_FILE} app.jar
RUN bash -c 'touch /app.jar'
EXPOSE 18000
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom","-jar", "/app.jar"]

Considering that we currently build and push the docker image for the project to the image warehouse, all through the plugin named dockerfile. We focus on listing the pom.xml files of each project to help you configure the details.

  • user-service
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.qwfys.sample</groupId>
        <artifactId>user-service</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <groupId>com.qwfys.sample</groupId>
    <artifactId>user-service-impl</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <!--<docker.image.organization>registry.cn-hangzhou.aliyuncs.com/qwfys</docker.image.organization>-->
        <docker.image.organization>lanzhou</docker.image.organization>
        <docker.image.repository>user-service</docker.image.repository>
        <!--<docker.tag.version>0.0.1.release</docker.tag.version>-->
        <docker.tag.version>latest</docker.tag.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.qwfys.sample</groupId>
            <artifactId>user-service-spec</artifactId>
        </dependency>
        <dependency>
            <groupId>com.qwfys.sample</groupId>
            <artifactId>user-business-spec</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <finalName>${project.name}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.4.13</version>
                <configuration>
                    <repository>${docker.image.organization}/${docker.image.repository}</repository>
                    <tag>${docker.tag.version}</tag>
                    <buildArgs>
                        <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
                    </buildArgs>
                    <useMavenSettingsForAuth>true</useMavenSettingsForAuth>
                </configuration>
            </plugin>
        </plugins>

    </build>
    <repositories>
        <!--<repository>-->
        <!--    <id>aliyun-docker</id>-->
        <!--    <url>registry.cn-hangzhou.aliyuncs.com</url>-->
        <!--</repository>        -->
        <repository>
            <id>official-docker</id>
            <url>https://index.docker.io/v1/</url>
        </repository>
    </repositories>
</project>
  • user-api
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.qwfys.sample</groupId>
        <artifactId>dragon-api</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <groupId>com.qwfys.sample</groupId>
    <artifactId>usr-api</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <description>user-oriented</description>

    <properties>
        <!--<docker.image.organization>registry.cn-hangzhou.aliyuncs.com/qwfys</docker.image.organization>-->
        <docker.image.organization>lanzhou</docker.image.organization>
        <docker.image.repository>user-api</docker.image.repository>
        <!--<docker.tag.version>0.0.1.release</docker.tag.version>-->
        <docker.tag.version>latest</docker.tag.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.qwfys.sample</groupId>
            <artifactId>user-service-spec</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>com.spring4all</groupId>
            <artifactId>swagger-spring-boot-starter</artifactId>
            <version>1.9.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <finalName>${project.name}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.4.13</version>
                <configuration>
                    <repository>${docker.image.organization}/${docker.image.repository}</repository>
                    <tag>${docker.tag.version}</tag>
                    <buildArgs>
                        <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
                    </buildArgs>
                    <useMavenSettingsForAuth>true</useMavenSettingsForAuth>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <!--<repository>-->
        <!--    <id>aliyun-docker</id>-->
        <!--    <url>registry.cn-hangzhou.aliyuncs.com</url>-->
        <!--</repository>        -->
        <repository>
            <id>official-docker</id>
            <url>https://index.docker.io/v1/</url>
        </repository>
    </repositories>

</project>

login to dokcer hub

lwk@qwfys:~$ docker login --username=qwfys
Password: 
WARNING! Your password will be stored unencrypted in /home/lwk/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
lwk@qwfys:~$ 

create organization on docker hub

Create an organization named lanzhou on docker hub.

build & push with dockerfile

Use the maven plugin named dockerfile to build a docker image based on the current project and push it to the corresponding repositoriy on the docker hub.

  • for project named user-service

进入user-service-impl所在目录~/Public/project/com/qwfys/dragon/dragon-service/user-service/user-service-impl,然后执行如下命令:

mvn clean install dockerfile:build dockerfile:push
  • for project name usr-api

进入usr-api所在目录~/Public/project/com/qwfys/dragon/dragon-api/usr-api,执行如下命令

mvn clean install dockerfile:build dockerfile:push

Run

  • step 1 search docker image
[root@qwfys90 ~]# docker search lanzhou
NAME                    DESCRIPTION         STARS               OFFICIAL            AUTOMATED
lanzhou/user-service                        0                                       
lanzhou/user-api                            0                                       
[root@qwfys90 ~]# 
  • step2 run
[root@qwfys90 ~]# docker run -d --name user-service lanzhou/user-service
Unable to find image 'lanzhou/user-service:latest' locally
latest: Pulling from lanzhou/user-service
7e2b2a5af8f6: Already exists 
09b6f03ffac4: Already exists 
dc3f0c679f0f: Already exists 
fd4b47407fc3: Already exists 
bb40faab53a2: Already exists 
4bb55ce33bc6: Already exists 
b2c255cea82d: Already exists 
97241e915afc: Pull complete 
32be84694706: Pull complete 
Digest: sha256:7960c98adf8cfb87b1eb873438d1e03c25ce5abac2570946f5e74bfd1e9d011f
Status: Downloaded newer image for lanzhou/user-service:latest
add3525792b3f7572d732a26aca5f9a24f87a24679fd2ac82942cc36d5c45592
[root@qwfys90 ~]# docker run -d --name user-backend -p 18000:18000 lanzhou/user-api
Unable to find image 'lanzhou/user-api:latest' locally
latest: Pulling from lanzhou/user-api
7e2b2a5af8f6: Already exists 
09b6f03ffac4: Already exists 
dc3f0c679f0f: Already exists 
fd4b47407fc3: Already exists 
bb40faab53a2: Already exists 
4bb55ce33bc6: Already exists 
b2c255cea82d: Already exists 
05e259fd5e9c: Pull complete 
4c60065635a9: Pull complete 
Digest: sha256:d1c2b9c65f0037371f4e199ad7c56c64b424484267959bdf32af5b2d88276bf6
Status: Downloaded newer image for lanzhou/user-api:latest
bea17018d7e608b0fdc5c53f1b8be922b85d31a2cf79f2b0d28a66fcdee248e9
[root@qwfys90 ~]# docker ps -a
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                      NAMES
bea17018d7e6        lanzhou/user-api        "java -Djava.securit…"   37 seconds ago      Up 35 seconds       0.0.0.0:18000->18000/tcp   user-backend
add3525792b3        lanzhou/user-service    "java -Djava.securit…"   5 minutes ago       Up 5 minutes        18000/tcp                  user-service
[root@qwfys90 ~]# 
[root@qwfys90 ~]# ss -ntlp
State      Recv-Q Send-Q                                                                          Local Address:Port                                                                                         Peer Address:Port              
LISTEN     0      100                                                                                 127.0.0.1:25                                                                                                      *:*                   users:(("master",pid=1547,fd=13))
LISTEN     0      128                                                                                         *:22                                                                                                      *:*                   users:(("sshd",pid=1147,fd=3))
LISTEN     0      100                                                                                     [::1]:25                                                                                                   [::]:*                   users:(("master",pid=1547,fd=14))
LISTEN     0      128                                                                                      [::]:18000                                                                                                [::]:*                   users:(("docker-proxy",pid=7004,fd=4))
LISTEN     0      128                                                                                      [::]:22                                                                                                   [::]:*                   users:(("sshd",pid=1147,fd=4))
[root@qwfys90 ~]# 

打开浏览器http://192.168.3.90:18000/swagger-ui.html

至此运行成功。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qwfys200

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值