jenkins服务提供拉取代码并本地编译然后推送到Docker Build服务器。
docker run -it --rm -p 80:8080 -v /data/docker/jenkins:/var/jenkins_home -v /data/docker/jenkins/maven:/home/anonymous/.m2 docker.smart.com/base/jenkinscat /data/docker/jenkins/upload_file
#jenkins 编译完成之后推送脚本
#!/bin/bash
dcpu=${cpu:="0.4"}
dmem=${mem:="512"}
dins=${ins:="1"}
model="test"
#sysName="www"
#BUILD_NUMBER="1"
#sysDir="/data/docker/jenkins/workspace/www/diligrp-website-web/target/diligrp-website-web"
sysName=${JOB_NAME}
HOST=root@192.168.10.130
sysTmp=$(find ${WORKSPACE} -name ${filename:=*.war})
sysDir=${sysTmp/'.war'/}
echo "war path: ${sysDir}"
upload_file()
{
ssh -nq $HOST "[ -d /tmp/${sysName} ] && rm -rf /tmp/${sysName} >>/dev/null ; mkdir -p /tmp/${sysName}"
ssh -nq $HOST "mkdir -p /tmp/${sysName} "
scp -r ${sysDir}/* $HOST:/tmp/${sysName}
ssh -nq $HOST "sh /usr/bin/Docker_Build ${sysName} ${BUILD_NUMBER} ${dcpu} ${dmem} ${dins} ${model}"
}
upload_file
11)配置Docker Build服务
Docker Build其实就是将Jenkins服务器推过来的源码包利用Dockfile生成 docker 镜像。
cat /usr/bin/Docker_Build#!/bin/bash
cpu=$3
mem=$4
ins=$5
sysName=$1
BUILD_NUMBER=$2
model=$6
#sysTmp=$(find / -name ${filename:=*.war})
#sysDir=${sysTmp/'.war'/}
sysDir=/tmp/$1
echo "war path: ${sysDir}"
if [ ! -f "${sysDir}/Dockerfile" ]; then
#дDockerfile
echo "FROM docker.smart.com/base/tomcat
RUN rm -rf /usr/local/tomcat/webapps/ROOT/*
ADD ./ /usr/local/tomcat/webapps/ROOT
" > ${sysDir}/Dockerfile
fi
dockerPath=docker.smart.com/front/$sysName:${BUILD_NUMBER}
cd ${sysDir}
docker build -t ${dockerPath} .
docker push ${dockerPath}
curl -X DELETE -H "Content-Type: application/json" ${model}/${sysName}
sleep 20
if [ ! -f "${sysDir}/app_marathon.json" ]; then
cat << EOF >${sysDir}/app_marathon.json
{"id": "/${model}/${sysName}","container": {"docker":{"image":"${dockerPath}","network": "BRIDGE","parameters":[{"key":"dns", "value": "192.168.10.86"}],"portMappings": [{"containerPort": 8080}]}},"cpus": ${cpu},"mem": ${mem},"instances": ${ins}}
EOF
fi
curl -X POST -H "Content-Type: application/json" -d@${sysDir}/app_marathon.json
sleep 5
cd /
rm -rf ${sysDir}
更多Docker相关教程见以下内容:
Ubuntu 14.04安装Docker
在Ubuntu Trusty 14.04 (LTS) (64-bit)安装Docker
在 Ubuntu 15.04 上如何安装Docker及基本用法
平台部署遇到的一些坑:
系统防火墙问题,docker的防火墙和系统的防火墙是独立管理的,可以关闭系统防火墙。