您好,欢迎访问代理记账网站
  • 价格透明
  • 信息保密
  • 进度掌控
  • 售后无忧

DockerMaven插件

DockerMaven插件

前言

微服务部署我所知的目前有三种方式:

(1)手动部署:首先基于源码打包生成jar包(或war包),将jar包(或war包)上传至虚拟机并拷贝至JDK容器

(2)通过Maven插件自动部署

(3)在服务器起一个打包容器,将代码拉至容器内,然后在服务器编写dockerfile,k8s配置以及批处理,即可一键部署。

这里实践的为第二种。

环境

JDK8, Springboot2.4.1

步骤

(1)修改宿主机的docker配置,让其可以远程访问

vi /lib/systemd/system/docker.service

其中的ExecStart=后添加配置

-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock

(2)刷新配置,重启服务

systemctl daemon-reload
systemctl restart docker
docker start registry

(3)在工程pom.xml增加配置

<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>1.2.2</version>
    <configuration>
        <!-- 最后需要生成的镜像名称  -->
        <imageName>192.168.70.128:5000/${project.artifactId}:${project.version}</imageName>
        <!--<baseImage>openjdk:8-jdk-alpine</baseImage>-->
        <!--<entryPoint>["java","-jar","/${project.build.finalName}.jar]</entryPoint>-->
        <!-- Dockerfile文件存放目录 -->
        <dockerDirectory>${project.basedir}/development</dockerDirectory>
        <resources>
            <resource>
                <targetPath>/</targetPath>
                <directory>${project.build.directory}</directory>
                <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
        <!-- docker远程服务器地址 -->
        <dockerHost>http://192.168.70.128:2375</dockerHost>
    </configuration>
</plugin>

(4)以上配置需要准备Dokcerfile

在pom同级新建目录development,development新建Dockerfile

FROM openjdk:oraclelinux8
MAINTAINER fulin.tang
ADD redis-lock-0.0.1.jar /redis-lock-0.0.1.jar
ENTRYPOINT ["java","-jar","/redis-lock-0.0.1.jar"]

(5)运行命令

mvn clean package -Dmaven.test.skip=true docker:build -DpushImage

(6)运行容器

docker run -di --name redislock -p 8080:8080 192.168.70.128:5000/redis-lock:0.0.1

运行成功!

附完整pom

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
        <relativePath/>
    </parent>
    <groupId>com.tang</groupId>
    <artifactId>redis-lock</artifactId>
    <version>0.0.1</version>
    <name>redis-lock</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>io.lettuce</groupId>
                    <artifactId>lettuce-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson-spring-boot-starter</artifactId>
            <version>3.15.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.2.2</version>
                <configuration>
                    <!-- 最后需要生成的镜像名称  -->
                    <imageName>192.168.70.128:5000/${project.artifactId}:${project.version}</imageName>
                    <!--<baseImage>openjdk:8-jdk-alpine</baseImage>-->
                    <!--<entryPoint>["java","-jar","/${project.build.finalName}.jar]</entryPoint>-->
                    <!-- Dockerfile文件存放目录 -->
                    <dockerDirectory>${project.basedir}/development</dockerDirectory>
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                    <!-- docker远程服务器地址 -->
                    <dockerHost>http://192.168.70.128:2375</dockerHost>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>


分享:

低价透明

统一报价,无隐形消费

金牌服务

一对一专属顾问7*24小时金牌服务

信息保密

个人信息安全有保障

售后无忧

服务出问题客服经理全程跟进