以前曾寫過docker及jenkins基礎使用 https://www.cnblogs.com/xiaochangwei/category/816943.htmlhtml
如今搭建環境的功能爲:nginx
1.jenkins自動拉取代碼打包web
2.根據時間戳生成docker鏡像docker
3. 啓動生成的鏡像shell
3.1.能夠指定啓動的名字和訪問路徑tomcat
3.2.能夠指定端口bash
3.3.若是端口或名字已經被佔用,則直接停掉以前啓動的app
4.訪問的時候不要輸入端口,直接爲 測試環境ip/應用名字webapp
根據要求,採用了下列步驟方法:tcp
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/應用名稱 訪問應用了 無需關心具體映射的端口
[[1;34mINFO[m] [1m------------------------------------------------------------------------[m [[1;34mINFO[m] [1;32mBUILD SUCCESS[m [[1;34mINFO[m] [1m------------------------------------------------------------------------[m [[1;34mINFO[m] Total time: 20.770 s [[1;34mINFO[m] Finished at: 2019-05-05T14:23:44+08:00 [[1;34mINFO[m] Final Memory: 39M/440M [[1;34mINFO[m] [1m------------------------------------------------------------------------[m [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 [91mWarning: ALREADY_ENABLED: 32822:tcp[00m success [91mWarning: ALREADY_ENABLED: 32821:tcp[00m success [91mWarning: ALREADY_ENABLED: 32814:tcp[00m success [91mWarning: ALREADY_ENABLED: 80:tcp[00m success [91mWarning: ALREADY_ENABLED: 88:tcp[00m success [91mWarning: ALREADY_ENABLED: 9999:tcp[00m success [91mWarning: ALREADY_ENABLED: 8080:tcp[00m success [91mWarning: ALREADY_ENABLED: 9099:tcp[00m 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