Maven发布Docker镜像
使用过Docker的同学都清楚,Docker容器是基于镜像Image来运行的。所以我们前提条件是要会熟悉如何制作镜像。制作镜像最简单的方式就是通过Docker自带的docker build、tag、push
等相关的命令操作(具体命令看下面
)。还有就是本文所描述法人通过maven的Docker插件
来实现基于源码来实现一键镜像Image发布到远程仓库(Docker Hub、阿里云仓库等)。
使用docker命令发布镜像:这条命令需要在Dockerfile文件所在路劲下执行。
docker build -t testhub.cloud.net/test/redis:3.6 ./ && docker push testhub.cloud.net/test/redis:3.6
一、项目结构
二、Dockerfile文件
所在目录结构看上图
- Dockerfile文件
FROM openjdk:11
#容器中工作目录
WORKDIR /docker-work
#将本地jar包copy到容器根目录下
ADD /target/test.jar /test.jar
#将本地启动脚本copy到容器中
ADD src/main/docker/run.sh /bin/run.sh
#授权脚本,否则无法执行
RUN chmod +x /bin/run.sh
CMD ["/bin/run.sh"]
- run.sh文件
#! /bin/bash
echo "Start test Server..."
java -jar /test.jar
三、POM文件
注意一下是pom.xml局部配置,基于自己的pom加入一下内容
<!--注意:
下面会使用这个version标签值作为发布镜像Image的版本。
类似:redis:3.6后面的3.6
-->
<version>v1.0.0</version>
<properties>
<docker.username>你的docker仓库的用户名</docker.username>
<docker.password>你的docker仓库的密码</docker.password>
<!--类似:registry.cn-hangzhou.aliyuncs.com-->
<docker.registry.url>你的仓库地址</docker.registry.url>
<!--类似:redis-->
<docker.namespace>仓库命名空间</docker.namespace>
</properties>
<build>
<finalName>test</finalName>
<plugins>
<!--SpringBoot自带打包插件-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--Dokcer发布镜像插件-->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<configuration>
<username>${docker.username}</username>
<password>${docker.password}</password>
<!--类似:registry.cn-hangzhou.aliyuncs.com/cache/redis:3.6-->
<repository>${docker.registry.url}/${docker.namespace}/${project.artifactId}</repository>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
<skip>false</skip>
</configuration>
</plugin>
</plugins>
</build>
四、发布镜像
注意:以下命令需要在pom.xml所在路径下执行
git pull && mvn clean install dockerfile:build dockerfile:push
五、发布成功
发布成功之后就可以在仓库上看到自己的镜像了。