赞
踩
前面我们介绍了K8S+Docker+Maven插件打包部署SpringCloud微服务项目,在实际应用过程中,很多项目没有用到K8S和微服务,但是用到了Docker和SpringBoot,所以,我们这边介绍,如果使用Jenkins+jib-maven-plugin插件打包部署SpringBoot项目的Docker镜像。
网上有多种Docker打包插件使用说明,讲解最多的是Spotify开源的,Spotify官方已不再推荐使用docker-maven-plugin插件进行打包,而是推荐其最新的docker打包插件dockerfile-maven-plugin,但是dockerfile-maven-plugin也已经很久没有更新了,在使用方面也有局限性,比如:只支持在本机Docker的镜像build、tag、push。
经过几个插件的对比,发现Google开源的Jib插件功能更强大,它可以不写Dockerfile,不需要在本地安装Docker环境就能实现Docker打包,而且一直在更新,所以这里选择这个插件作为我们的Docker打包插件。
Jib构建Java的Docker和OCI映像,不需要在本机安装Docker daemon,也不需要深入掌握Docker最佳实践。它可以作为Maven和Gradle的插件以及Java库使用。
下面介绍Jib( jib-maven-plugin插件 )如何将SpringBoot应用程序分层打包Docker镜像,充分利用Docker的镜像分层复用机制,解决网络限制和占用大量磁盘空间的问题。
Jib( jib-maven-plugin插件 )构建的三个参数:
我们这里主要使用buildTar命令,将Docker镜像文件打包在本地,然后通过Jenkins插件发布到服务器运行,这样可以不需要搭建使用私有Docker镜像仓库Harbor。
clean package -Ptest -Dimage=gitegg:1.0.1 jib:buildTar -f pom.xml
docker load --input jib-image.tar
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<to>
<image>私有仓库地址/项目名称/${project.artifactId}:${project.version}</image>
<auth>
<username>私有仓库用户名</username>
<password>私有仓库密码</password>
</auth>
</to>
<!-- 容器内的目录 -->
<volumes>
<value>/tmp</value>
<value>/log</value>
</volumes>
<workingDirectory>/gitegg</workingDirectory>
<!--K8S时,使用OCI,单独只用docker load会报错,所以这里不使用这个format-->
<format>OCI</format>
下面是SpringBoot项目中pom.xml的jib-maven-plugin插件配置信息:
······
<properties>
······
<!-- jib-maven-plugin插件版本,代码打包docker -->
<jib.maven.plugin.version>3.3.2</jib.maven.plugin.version>
······
</properties>
······
<!-- Docker 打包插件 -->
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>${jib.maven.plugin.version}</version>
<!-- 绑定到Maven的install生命周期 ,此处如果不使用https,会有问题,需要设置sendCredentialsOverHttp=true-->
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<!--允许非https-->
<allowInsecureRegistries>true</allowInsecureRegistries>
<!-- 相当于Docerkfile中的FROM -->
<from>
<image>openjdk:11-jre</image>
</from>
<to>
<image>${docker.harbor.addr}/${docker.harbor.project}/${project.artifactId}:${project.version}</image>
<auth>
<username>${docker.harbor.username}</username>
<password>${docker.harbor.password}</password>
</auth>
</to>
<container>
<!--jvm内存参数-->
<jvmFlags>
<jvmFlag>-Xms512m</jvmFlag>
<jvmFlag>-Xmx8g</jvmFlag>
</jvmFlags>
<!-- 容器内的目录-->
<volumes>
<value>/tmp</value>
<value>/log</value>
</volumes>
<workingDirectory>/gitegg</workingDirectory>
<environment>
<TZ>Asia/Shanghai</TZ>
</environment>
<!--使用该参数保证镜像的创建时间与系统时间一致-->
<creationTime>USE_CURRENT_TIMESTAMP</creationTime>
<!--K8S时,使用OCI,非K8S时,load会报错-->
<!-- <format>OCI</format>-->
</container>
</configuration>
</plugin>
前面我们讲了如何安装Docker,以及相关配置,所以这里不再赘述,直接按照自己的服务器实际情况进行安装配置Docker即可,这里讲解如何为运行SpringBoot的Fat Jar做环境准备,以及说明镜像包传输到服务器之后执行的部署脚本。
mkdir -p /opt/tmp /opt/bak /opt/script /data/container/docker_server/tmp /data/container/docker_server/logs
chmod -R 777 /opt/tmp /opt/bak /opt/script /data/container/docker_server/tmp /data/container/docker_server/logs
echo "param validate"
if [ $# -lt 1 ]; then
echo "you must use like this : ./publish_docker_server.sh <container_name> <image_name> <version> [image port] [server port]"
exit
fi
if [ "$1" != "" ]; then
container_name="$1"
fi
echo "container_name=" $container_name
if [ "$2" != "" ]; then
image_name="$2"
fi
if [ "$3" != "" ]; then
version="$3"
fi
echo "version=" $version
if [ "$4" != "" ]; then
image_port="$4"
fi
echo "image_port=" $image_port
if [ "$5" != "" ]; then
server_port="$5"
fi
echo "server_port=" $server_port
echo "执行docker ps"
docker ps
if [[ "$(docker inspect $container_name 2> /dev/null | grep $container_name)" != "" ]];
then
echo $container_name "容器存在,停止并删除"
echo "docker stop" $container_name
docker stop $container_name
echo "docker rm" $container_name
docker rm $container_name
else
echo $container_name "容器不存在"
fi
# 删除镜像
echo "执行docker images"
docker images
if [[ "$(docker images -q $image_name 2> /dev/null)" != "" ]];
then
echo $image_name '镜像存在,删除镜像'
docker rmi $(docker images -q $image_name 2> /dev/null) --force
else
echo $image_name '镜像不存在'
fi
#bak image
echo "bak image" $image_name
BAK_DIR=/opt/bak/docker/$image_name/`date +%Y%m%d`
mkdir -p "$BAK_DIR"
cp "/opt/tmp/jib-image.tar" "$BAK_DIR"/"$image_name"_`date +%H%M%S`.tar
echo "docker load" $image_name
docker load --input /opt/tmp/jib-image.tar
echo "docker run" $image_name
docker run -d -p $image_port:$server_port --name=$container_name --restart=always -v /data/container/docker_server/tmp:/tmp -v /data/container/docker_server/logs:/var/log $image_name
echo "remove tmp " $image_name
rm -rf /opt/tmp/jib-image.tar
echo "Docker Server is starting,please try to access $container_name conslone url"
container_name=gitegg-server
image_name=gitegg-server
version=latest
image_port=8181
server_port=8080
echo "param validate"
if [ $# -lt 1 ]; then
echo "you must use like this : ./publish_docker_server.sh <container_name> <image_name> <version> [image port] [server port]"
exit
fi
if [ "$1" != "" ]; then
container_name="$1"
fi
echo "container_name=" $container_name
if [ "$2" != "" ]; then
image_name="$2"
fi
if [ "$3" != "" ]; then
version="$3"
fi
echo "version=" $version
if [ "$4" != "" ]; then
image_port="$4"
fi
echo "image_port=" $image_port
if [ "$5" != "" ]; then
server_port="$5"
fi
echo "server_port=" $server_port
echo "执行docker ps"
docker ps
if [[ "$(docker inspect $container_name 2> /dev/null | grep $container_name)" != "" ]];
then
echo $container_name "容器存在,停止并删除"
echo "docker stop" $container_name
docker stop $container_name
echo "docker rm" $container_name
docker rm $container_name
else
echo $container_name "容器不存在"
fi
# 删除镜像
echo "执行docker images"
docker images
if [[ "$(docker images -q $image_name 2> /dev/null)" != "" ]];
then
echo $image_name '镜像存在,删除镜像'
docker rmi $(docker images -q $image_name 2> /dev/null) --force
else
echo $image_name '镜像不存在'
fi
#bak image
echo "bak image" $image_name
BAK_DIR=/opt/bak/docker/$image_name/`date +%Y%m%d`
mkdir -p "$BAK_DIR"
cp "/opt/tmp/jib-image.tar" "$BAK_DIR"/"$image_name"_`date +%H%M%S`.tar
echo "docker load" $image_name
docker load --input /opt/tmp/jib-image.tar
echo "docker run" $image_name
docker run -d -p $image_port:$server_port --name=$container_name --restart=always -v /data/container/docker_server/tmp:/tmp -v /data/container/docker_server/logs:/var/log $image_name
echo "remove tmp " $image_name
rm -rf /opt/tmp/jib-image.tar
echo "Docker Server is starting,please try to access $container_name conslone url"
clean package -Dmaven.test.skip=true -Ptest -Dimage=gitegg jib:buildTar -f pom.xml
/opt/script/publish_docker_server.sh gitegg gitegg latest 8181 8080
立即构建
查看构建日志:点击立即构建之后,下方会出现进度条,点击进度条就可以进入构建日志界面。
docker images
docker ps
docker stop 容器id
docker rm 容器id
docker rmi 镜像id
docker logs -f 容器id
docker exec -it 容器id /bin/bash
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。