jenkins自动打包生成docker镜像后自动发布并nginx代理访问

 

之前曾写过docker及jenkins基础使用  https://www.cnblogs.com/xiaochangwei/category/816943.html

 

现在搭建环境的功能为:

1.jenkins自动拉取代码打包

2.根据时间戳生成docker镜像

3. 启动生成的镜像

  3.1.可以指定启动的名字和访问路径

  3.2.可以指定端口

  3.3.如果端口或名字已经被占用,则直接停掉之前启动的

4.访问的时候不要输入端口,直接为 测试环境ip/应用名字

 

根据要求,采用了下列步骤方法:

1.构建后动态生成Dockerfile

2.构建docker镜像

3.停掉端口或名称冲突的docker服务

4.启动新生成的镜像

5.更新nginx配置文件并重启nginx

 

为了简单方便且动态指定各项参数,直接采用了构建后shell操作

-------------------------------------------------------------------------------------

 

#参数设定
#1.映射端口,不指定则使用随机值
PORT=

#2.容器名字,不指定则使用打包tag号,如:jenkins-myapp-18
NAME=


#-----------------------下列内容请勿修改---------------------------------
#-----------------------下列内容请勿修改---------------------------------
#-----------------------下列内容请勿修改---------------------------------
if [ -z "$NAME" ]; then 
    NAME=$JOB_NAME
fi

TAG=`date "+%Y%m%d%H%M%S"`

rm -f Dockerfile
cat > Dockerfile << EOF
FROM tomcat:8-jre8
MAINTAINER xiaochangwei <changw.xiao@qq.com>
LABEL app="testApp" version="0.0.1" by="xiaochangwei"
RUN rm -rf /usr/local/tomcat/webapps/ROOT
COPY ./target/*.war /usr/local/tomcat/webapps/$NAME.war
ENTRYPOINT ["/usr/local/tomcat/bin/catalina.sh","run"]
EOF

IMAGE=$JOB_NAME/$NAME:$TAG
docker build -t $IMAGE --rm=true .



#检查容器名称是否存在,存在则停止并删除
exsitContainer=`docker ps -a --filter name=$NAME |awk '{print $1}'|tail -n +2`
echo $exsitContainer
if [ -n "$exsitContainer" ]; then 
    docker stop $exsitContainer
    docker rm $exsitContainer 
fi

#如果指定了端口,则检查端口是否被占用,如被占用,直接停止对应容器
if [ -n "$PORT" ]; then 
    containers=`docker ps -a --filter status=running|grep ':'$PORT |awk '{print $1}'` 
    echo 'containers:'$containers
    if [ ${#containers[@]} -gt 0 ];then
      echo 'stop container:'$containers
      #docker stop $containers
        for container in $containers
          do
           docker stop $container
          done
    fi
    #根据指定端口启动容器
    docker run -d --privileged=true --env JAVA_OPTS=-Duser.timezone=GMT+08 -p $PORT:8080 --name $NAME $IMAGE
else
   #使用随机端口启动容器
   docker run -d -P --env JAVA_OPTS=-Duser.timezone=GMT+08 --name $NAME $IMAGE
fi

#调用脚本更新nginx代理配置
sh /docker/nginx/nginx_conf_update.sh


docker ps -a |grep $IMAGE

其中  /docker/nginx/nginx_conf_update.sh 逻辑为:通过docker ps 列出启动的镜像应用,然后动态生成nginx配置文件

[root@localhost nginx]# cat nginx_conf_update.sh 
#!/bin/bash

path=/docker/nginx/conf.d/default.conf

echo "" > $path

echo "server {" >> $path

echo "listen       80;" >> $path
echo "client_max_body_size   200m;"  >> $path
echo "server_name   localhost;" >> $path

echo "fastcgi_buffers 18 128k;" >> $path
echo "send_timeout 300;" >> $path
#echo "fastcgi_connect_timeout 300s;" >> $path
#echo "fastcgi_send_timeout 300s;" >> $path
#echo "fastcgi_read_timeout 300s;" >> $path

#echo "proxy_connect_timeout 500s;" >> $path
#echo "proxy_read_timeout 500s;" >> $path
#echo "proxy_send_timeout 500s;" >> $path

echo "" >> $path

docker_ip(){
port=$(docker port $1 | grep 80 | cut -d':' -f2)
    if [  ${#port} -gt 0 ]; then
    #ip=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' $1)
    #ok=$(cat ../data/nginx/conf.d/default.conf |grep $1)

    #if [ ${#ok} -gt 0 ];then
    #    echo "存在"
    #    sed -i '/\'$1'/,+3d' ../data/nginx/conf.d/default.conf
    #fi

        firewall-cmd --zone=public --add-port=$port/tcp --permanent

    sed -i '$i \
        \
    location '$1' {  \
        proxy_pass   http://172.17.0.1:'$port$1';  \
        proxy_set_header Host $host;  \
        proxy_connect_timeout 500s; \
        proxy_read_timeout 500s; \
        proxy_send_timeout 500s; \
    } ' $path
   fi
}
for i in $(docker inspect -f '{{.Name}}' $(docker ps -aq))
do
        docker_ip $i
done



echo "     location / {" >> $path
echo "         proxy_pass   http://172.17.0.1:13380/;" >> $path
echo "         proxy_set_header Host \$host; "  >> $path
echo "         client_max_body_size   200m;"  >> $path
echo "     } "  >> $path

echo "     location /jenkins {" >> $path
echo "         proxy_pass   http://172.17.0.1:8081/;" >> $path
echo "         proxy_set_header Host \$host; "  >> $path
echo "         client_max_body_size   200m;"  >> $path
echo "     } "  >> $path

echo "}" >> $path

firewall-cmd --reload
docker exec -i nginx nginx -s reload
[root@localhost nginx]# 

 

在构建前需要先启动一个nginx

docker run -d -p 80:80 --name=nginx  --restart=always -v /etc/localtime:/etc/localtime -v /docker/nginx/conf.d:/etc/nginx/conf.d nginx

 

这样构建后就能直接通过 ip/应用名称 访问应用了  无需关心具体映射的端口

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 20.770 s
[INFO] Finished at: 2019-05-05T14:23:44+08:00
[INFO] Final Memory: 39M/440M
[INFO] ------------------------------------------------------------------------
[bdjc_ls_x] $ /bin/sh -xe /tmp/jenkins4101726310382471801.sh
+ PORT=
+ NAME=
+ '[' -z '' ']'
+ NAME=bdjc_ls_x
++ date +%Y%m%d%H%M%S
+ TAG=20190505142345
+ rm -f Dockerfile
+ cat
+ IMAGE=bdjc_ls_x/bdjc_ls_x:20190505142345
+ docker build -t bdjc_ls_x/bdjc_ls_x:20190505142345 --rm=true .
Sending build context to Docker daemon 176.9 MB

Step 1/6 : FROM tomcat:8-jre8
 ---> dd6ff929584a
Step 2/6 : MAINTAINER xiaochangwei <changw.xiao@qq.com>
 ---> Using cache
 ---> 3bbe1bdd60b1
Step 3/6 : LABEL app "testApp" version "0.0.1" by "xiaochangwei"
 ---> Using cache
 ---> fa190e40687a
Step 4/6 : RUN rm -rf /usr/local/tomcat/webapps/ROOT
 ---> Using cache
 ---> 75294c2b57d3
Step 5/6 : COPY ./target/*.war /usr/local/tomcat/webapps/bdjc_ls_x.war
 ---> ea3a50c66ed9
Removing intermediate container 5936210f7b50
Step 6/6 : ENTRYPOINT /usr/local/tomcat/bin/catalina.sh run
 ---> Running in 61ace663e5a4
 ---> 48f8683a1145
Removing intermediate container 61ace663e5a4
Successfully built 48f8683a1145
++ docker ps -a --filter name=bdjc_ls_x
++ awk '{print $1}'
++ tail -n +2
+ exsitContainer=
+ echo

+ '[' -n '' ']'
+ '[' -n '' ']'
+ docker run -d -P --env JAVA_OPTS=-Duser.timezone=GMT+08 --name bdjc_ls_x bdjc_ls_x/bdjc_ls_x:20190505142345
7e85e36db00dde78495608e81d8bfe35e93e08f13b1b4216c3961a460e121017
+ sh /docker/nginx/nginx_conf_update.sh
success
Warning: ALREADY_ENABLED: 32822:tcp
success
Warning: ALREADY_ENABLED: 32821:tcp
success
Warning: ALREADY_ENABLED: 32814:tcp
success
Warning: ALREADY_ENABLED: 80:tcp
success
Warning: ALREADY_ENABLED: 88:tcp
success
Warning: ALREADY_ENABLED: 9999:tcp
success
Warning: ALREADY_ENABLED: 8080:tcp
success
Warning: ALREADY_ENABLED: 9099:tcp
success
success
2019/05/05 14:24:04 [notice] 263#263: signal process started
+ grep bdjc_ls_x/bdjc_ls_x:20190505142345
+ docker ps -a
7e85e36db00d        bdjc_ls_x/bdjc_ls_x:20190505142345                       "/usr/local/tomcat..."   12 seconds ago      Up 11 seconds              0.0.0.0:32823->8080/tcp   bdjc_ls_x
Finished: SUCCESS
构建日志

 

posted @ 2019-05-08 09:33  肖哥哥  阅读(4311)  评论(0编辑  收藏  举报
生命不息  奋斗不止  每天进步一点点