docker(三) 使用maven 插件构建docker镜像


layout: blog
istop: true
title: “使用maven 插件构建docker镜像”
date: 2018-09-11
category: docker
tags:

  • docker

介绍

作为一个java后端开发人员,docker如何与Maven 工程相结合是我们必须要面对的问题,它联系着我们的项目。也为了装x,so,看看吧。

环境介绍

  • 开发工具 idea
  • jar包管理 maven
  • 系统:Linux(本文采用centos7)
    java 和Maven 环境变量 需要提前配置好

简单使用

这里为了节省空间,采用一个现有的项目进行docker 改造。
项目就是一个springboot的入门项目,运行后,访问:http://host:8080/ ,界面如下图:
image

不使用Dockefile

pom.xml 改造

只给出核心代码

 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!-- Docker maven plugin -->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <!-- imageName 仓库名称/镜像名称:标签名称-->
                    <imageName>${docker.image.prefix}/${project.artifactId}:1.0.0</imageName>
                    <!-- 基础镜像名称 ,类似于 Dockerfile 中的 FROM  -->
                    <baseImage>java</baseImage>
                    <!-- 程序入口,类似于 Dockerfile中的 ENTTYPOINT-->
                    <entryPoint>["java","-Djava.security.egd=file:/dev/./urandom","-jar","/${project.build.finalName}.jar"]</entryPoint>

                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <!-- 指定需要复制的根目录,-->
                            <directory>${project.build.directory}</directory>
                            <!-- 用于指定需要复制的文件。 表示打包后的jar 文件 -->
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
            <!-- Docker maven plugin -->
        </plugins>
    </build>

运行

将项目复制到Linxu 服务器(使用scp)
进入项目根目录
mvn clean package docker:build

发现类似如下信息,证明已经构建成功

Step 1/3 : FROM java

Pulling from library/java
Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
Status: Downloaded newer image for java:latest
 ---> d23bdf5b1b1b
Step 2/3 : ADD /springboot-docker-1.0-SNAPSHOT.jar //

 ---> ef9ec89f58e1
Step 3/3 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/springboot-docker-1.0-SNAPSHOT.jar"]
Successfully built e61fa22956f9
Successfully tagged anhusky/springboot-docker:1.0.0

查看生成的镜像

[root@VM_9_185_centos springboot-docker]# docker images
REPOSITORY                              TAG                 IMAGE ID            CREATED             SIZE
anhusky/springboot-docker               1.0.0               e61fa22956f9        2 minutes ago       659MB

运行

[root@VM_9_185_centos springboot-docker]# docker run -d -p 8080:8080 anhusky/springboot-docker

访问服务地址,http://host:8080/ ,看是否能看到项目的主页面

使用DockerFile

pom.xml 改造

 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!-- Docker maven plugin -->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <!-- imageName 仓库名称/镜像名称:标签名称-->
                    <imageName>${docker.image.prefix}/${project.artifactId}:1.0.0</imageName>
                    <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>

                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <!-- 指定需要复制的根目录,-->
                            <directory>${project.build.directory}</directory>
                            <!-- 用于指定需要复制的文件。 表示打包后的jar 文件 -->
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
            <!-- Docker maven plugin -->
        </plugins>
    </build>

dockerfile 添加

src -> main -> docker -> Dockerfile

Dockerfile 文件如下:

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD springboot-docker-dockerfile-1.0.0.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

注意:copy 到服务器,打包、运行,与无dockerfile 的操作一致,请自行参考

源码

springboot-docker


springboot-docker-dockerfile

参考

3.7 使用Maven插件构建Docker镜像

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值