参考网址:https://github.com/spotify/dockerfile-maven 使用dockerfile-maven-plugin打包maven项目为docker image - 简书
1、创建并运行springboot程序
2、配置pom
<properties> <java.version>1.8</java.version> <!--项目名 --> <docker.image.prefix>web</docker.image.prefix> <!-- docker私服地址 --> <docker.registry>xxxx:xxx</docker.registry> </properties>
<plugin> <groupId>com.spotify</groupId> <artifactId>dockerfile-maven-plugin</artifactId> <version>1.4.10</version> <executions> <execution> <id>default</id> <goals> <!--如果package时不想用docker打包,就注释掉这个goal--> <goal>build</goal> <goal>push</goal> </goals> </execution> </executions> <configuration> <!-- 上下文路径配置,此处设置为项目根路径 用来读取Dockerfile--> <contextDirectory>${project.basedir}</contextDirectory> <useMavenSettingsForAuth>true</useMavenSettingsForAuth> <!--上传路径/镜像构建名: 私服地址/私服项目名/springboot项目名--> <repository>${docker.registry}/${docker.image.prefix}/${project.artifactId}</repository> <!-- 标记 --> <tag>${project.version}</tag> <!-- 作为Dockerfile文件传入 <buildArgs> <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE> </buildArgs>--> </configuration> </plugin>
3创建到根目录并配置Dockerfile文件 ${JAR_FILE}对应pom中<JAR_FILE>标签 只在插件中识别 直接执行有误
FROM openjdk:8-jdk-alpine VOLUME /tmp ADD target/docker-0.0.1.jar app.jar RUN sh -c 'touch /app.jar' ENTRYPOINT [ "java" ,"-jar","/app.jar" ]
配置私库harbor登录账号密码
找到maven配置文件
填入账号密码
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<servers>
<server>
<id>私库地址:端口</id>
<username>账号</username>
<password>密码</password>
</server>
</servers>
</settings>
4、运行
若在pom文件中设置了 goals 直接执行package
没有设置 分别执行 dockerfile中的 build和push
运行效果:
5、rancher部署服务
先添加私库地址- 安装部署harbor是会涉及到
vi /etc/docker/daemon.json
{
"insecure-registries": ["192.168.0.0/16", "10.8.0.0/16"],
"registry-mirrors": ["https://ew5e1tep.mirror.aliyuncs.com"]
}
systemctl daemon-reload
systemctl restart docker
创建服务
在k8s集群中 使用harbor 因为是http或者自签的https证书 所以拉去私库镜像失败
主机集群服务器中要有 kubectl 这个文件 /usr/local/bin
在k8s集群中添加资源-镜像库凭证
然后部署
访问 集群ip:30080
6、源码地址