docker 从入门到放弃—3.Jenkins 构建sprint-boot镜像,推docker仓库,发布脚本

1.第一步,登录到Jenkins后台,添加Jenkins到发布的环境服务器的免密登录,并且测试可以连通

测试连接的结果需要是success

 

2,将Dockerfile 配置好提交到代码的根目录

Dockerfile内容如下

# Start with a base image containing Java runtime  这里需要根据自己的环境选择镜像
FROM registry.cn-beijing.aliyuncs.com/*com/pro:base

#未镜像设定作者
MAINTAINER name@*.com

# 给镜像制定标签
LABEL appapi.*.com springboot appapi.jar

#set workdir
WORKDIR /work

# Add a volume pointing to /work
VOLUME /work

# Make port 8080 available to the world outside this container
EXPOSE 9090

#将打包好的jar 上传到镜像的对应工作目录#根据自己打包后生成的项目名称更改
ADD name.jar /work/app.jar

#将在当前镜像基础上执行指定命令,并提交为新的镜像
RUN sh -c ‘touch /work/app.jar’
RUN  ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

#设置java启动的参数
ENV JAVA_OPTS=”-server -Xms1024m -Xmx1024m”

#设置启动jar 包的命令
ENTRYPOINT [ “sh”, “-c”, “java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /work/app.jar” ]

 

3.新建项目并且正确的配置Git地址用户,打包等信息

Shell 内容如下

java -version

yourname=test
project_name=”app”
echo ‘================开始构建镜像================’

cd $yourname-$project_name
cp Dockerfile $WORKSPACE/$yourname-$project_name/target/
cd $WORKSPACE/$yourname-$project_name/target/
docker build -t spring-boot-$yourname-${project_name}api .
echo ‘================开始推送镜像================’
docker tag -f spring-boot-$yourname-${project_name}api:latest registry.cn-beijing.aliyuncs.com/$yourname/test:${project_name}api
docker push registry.cn-beijing.aliyuncs.com/$yourname/test:${project_name}api
echo ‘================结束推送镜像================’

ssh root@192.168.1.100 “/data/script/docker_pull.sh appapi 9010 9090”
echo ‘================服务启动完成================’

 

 

1.100服务器上的发布脚本内容如下

 

 

#!/bin/bash
#传入参数匹配
echo “开始匹配传入参数……”

if [ $# -lt 1 ]; then
echo “you must use like this : ./jenkins.sh <projectname> <serviceport> <dockerport>”
echo “sh jenkins.sh mapi 8080 8080”
exit
fi
if [ -n “$2” ]; then
echo ‘2’
serviceport=$2
fi

if [ -n “$3” ]; then
echo ‘3’
dockerport=$3
fi

docker stop $1
docker rm $1
docker login –username=username  –password=username   registry.cn-beijing.aliyuncs.com
docker pull registry.cn-beijing.aliyuncs.com/*com/test:$1
docker run –name=$1 -p $serviceport:$dockerport -v /data/www/$1:/work/logs -d registry.cn-beijing.aliyuncs.com/*com/test:$1

echo “finished!”

 

 

发表评论

电子邮件地址不会被公开。

Captcha Code