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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值