

使用 dockerfile-maven-plugin 构建 docker 镜像
source link: https://blog.diqigan.cn/posts/coding/java/dockerfile-maven-plugin.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

使用 dockerfile-maven-plugin 构建 docker 镜像
本文介绍了 com.spotify:dockerfile-maven-plugin 的简单使用示例。
最终达成的目标是把 docker 镜像构建集成在 maven 打包过程中,可以使用 maven 命令构建 docker 镜像。
功能相似的插件有三个:
- com.spotify:dockerfile-maven-plugin
本文使用的插件,已经停止更新,但功能依旧稳定。 com.spotify:docker-maven-plugin
本文所用插件的同胞兄弟,官方不推荐使用,已停止更新。- io.fabric8io:docker-maven-plugin
支持在 pom.xml 中配置 Dockerfile 的各项内容,也支持自定义 Dockerfile,支持操作容器,功能强大,仍在更新。
以 spring-boot-web 项目为例:
创建一个 spring-boot-web 项目并确保项目正常;
依据项目需要在合适的位置定制一个 Dockerfile,这里把 Dockerfile 放在了
src/main/docker
目录下:FROM openjdk:8-jdk-alpine
# 设置时区
ENV TZ Asia/Shanghai
RUN apk --no-cache add tzdata && cp /usr/share/zoneinfo/${TZ} /etc/localtime \
&& echo ${TZ} > /etc/timezone \
&& apk del tzdata
VOLUME /tmp
# 定义变量
ARG JAR_FILE
ADD target/${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
# 声明服务以 tcp 协议运行在 8080 端口
EXPOSE 8080/tcp
在
project
标签下添加插件配置:<build>
<plugins>
<!--dockerfile-maven-plugin 配置-->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.13</version>
<executions>
<execution>
<id>build</id>
<!--绑定 maven 阶段-->
<!--install 表示运行 mvn install 时会自动执行 docker 镜像构建-->
<!--install 也可以改成 package,运行 mvn package 时会自动执行 docker 镜像构建-->
<phase>install</phase>
<goals>
<!--执行插件的目标-->
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<!--指定 docker 镜像仓库-->
<repository>cn/diqigan/${project.artifactId}</repository>
<!--指定 docker 镜像标签-->
<tag>${project.version}</tag>
<!--指定 Dockerfile 路径-->
<dockerfile>src/main/docker/Dockerfile</dockerfile>
<!--设置 Dockerfile 中变量-->
<buildArgs>
<!--对应 Dockerfile 中的 JAR_FILE 变量-->
<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
<!--dockerfile-maven-plugin 配置-->
</plugins>
</build>
镜像构建有两种方式:
绑定 maven 阶段自动构建:
以文中所示配置,执行
mvn install
指令会自动构建 docker 镜像。显式运行构建指令
mvn package dockerfile:buld
构建 docker 镜像。
可以在 maven 控制台日志中看到 docker 镜像已经构建成功。
- 个人不推荐在 pom.xml 中配置 Dockerfile 的各项内容,有种配置侵入的感觉,建议另写一个 Dockerfile 文件;
- 持续集成过程中完全可以使用 docker 相关指令构建镜像,maven 插件有点画蛇添足的意思;
- 本文相关代码可在 Seven-Steven/spring-examples 查看;
</div
Recommend
-
51
根据Dockerfile构建出一个镜像
-
9
``` docker pull maven ``` # Quick reference - **Maintained by**: [the Maven Project](https://github.com/carlossg/docker-maven) - **Where to get help**: [the Docker Community Forums](https://forums.docker.com/), [the Docker Community...
-
8
如何从 Docker 镜像提取 Dockerfile? 哈喽哈喽大家好,小编今天又有新发现! ...
-
10
无需 Dockerfile 的镜像构建:BuildPack vs Dockerfile过去的工作中,我们使用微服务、容器化以及服务编排构建了技术平台。为了提升开发团队的研发效率,我们同时还提供了 CICD 平台,用来...
-
8
爱码爱生活 docker-maven-plugin插件的使用直接把jar...
-
8
再见 Dockerfile,拥抱新型镜像构建技术 Buildpacks作者:米开朗基杨,方阗云原生正在吞并软件世界,容器改变了传统的应用开发模式,如今研发人员不仅要...
-
12
V2EX › 问与答 docker 打前端镜像时,执行 dockerfile 文件,前端 npm run build 失败时怎么进容器看 npm 日志
-
7
用dockerfile生成镜像并挂载数据卷 编写dockerfile文件 创建dockerfile01 文件
-
7
Docker进阶-Dockerfile建立一个自定义的镜像执行自定义进程 ...
-
10
docker入门加实战—Docker镜像和Dockerfile语法 镜像就是包含了应用程序、程序运行的系统函数库、运行配置等文件的文件包。构建镜像的过程其实就是把上述文件打包的过程。 我们要从0部署一个Java...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK