Docker 使用总结

1.应用根目录创建ansible文件夹,添加两个文件builder.sh 、Dockerfile
Dockerfile

FROM 127.0.0.1:5000/self_jetty:2.0

COPY  webmerser /home/pbmp/jetty-web-merser/webapps/webmerser

#FROM 10.10.67.139:5000/self_jetty:2.0 引用的基础镜像
#COPY webmerser /home/pbmp/jetty-web-merser/webapps/webmerser 把应用copy到镜像里

builder.sh

#!/bin/bash

VERSION=$1

if [ ! ${VERSION} ]; then 
	echo "Please input release version number"
	exit 1
fi

image_name="10.10.67.139:5000/prov-remote/webmerser:${VERSION}"

echo "build image ${image_name}"

docker build --pull --no-cache=true -t ${image_name} .

if [ $? != 0 ]; then
	echo "build image failed"
	exit 1
fi

echo "Push Image to register"
docker push ${image_name}

if [ $? == 0 ]; then
	echo "Push successfully"
	echo "image: ${image_name}"
fi

2.整理应用中所有配置文件,把开发、测试、生产不一致的文件梳理3份
3.将整理的配置文件放到开发、测试、生产宿主机的对应应用目录下

cd /home/pbmp/webmerser
[wangrc@pbmp-docker1 webmerser]$ ll
total 0
drwxr-xr-x 3 pbmp mpsp 22 Oct 27 15:07 configs
drwxr-xr-x 3 pbmp mpsp 22 Oct 27 15:28 logs
drwxr-xr-x 2 pbmp mpsp 80 Oct 27 15:28 run
drwxr-xr-x 2 pbmp mpsp 77 Oct 27 15:07 serial

#configs 路径下为所有配置文件

[wangrc@pbmp-docker1 webmerser]$ tree  configs/
configs/
└── webmerser
    └── WEB-INF
        └── classes
            ├── config
            │   ├── platConfig.properties
            │   └── seq.properties
            ├── config.properties
            ├── jdbc-off.properties
            └── jdbc-on.properties

4 directories, 5 files

#logs下为所有日志
#serial下为映射文件
#run 下为启动脚本

#!/bin/bash

CONST_HOST="/home/pbmp/webmerser/configs"
CONST_JETTY_BASE="/home/pbmp/jetty-web-merser/webapps"
CONST_CONFIG="webmerser/WEB-INF/classes/config.properties"
CONST_PLATCONFIG="webmerser/WEB-INF/classes/config/platConfig.properties"
CONST_SEQ="webmerser/WEB-INF/classes/config/seq.properties"
CONST_JDBC_OFF="webmerser/WEB-INF/classes/jdbc-off.properties"
CONST_JDBC_ON="webmerser/WEB-INF/classes/jdbc-on.properties"

IMAGE_NAME=$1
if [ !  ${IMAGE_NAME} ]; then
        echo >&2 "请输入镜像名"
        exit 1
fi

echo >&2 "拉取镜像: ${IMAGE_NAME}"

docker pull ${IMAGE_NAME}
if [ $? != 0 ]; then
        echo >&2 "拉取镜像失败!!!"
        exit 1
fi


APPNAME=`echo ${IMAGE_NAME} | awk -F ':' '{print $2}' | awk -F '/' '{print $3}'`
if [ !  ${APPNAME} ]; then
        echo >&2 "无法获取部署应用名"
        exit 1
fi

#
app_status=`docker ps -a|grep -v "CONTAINER ID"|awk '{print $NF}'|grep ${APPNAME}`
if [ ${app_status} ]; then
   echo >&2 "停止应用: ${APPNAME}"
   docker stop ${APPNAME}
   echo >&2 "删除旧版应用 ${APPNAME}"
   docker rm ${APPNAME}
fi


#create new container
echo >&2 "创建新应用:${APPNAME}"
docker run -d --net=host \
--name ${APPNAME} \
-v ${CONST_HOST}/${CONST_CONFIG}:${CONST_JETTY_BASE}/${CONST_CONFIG} \
-v ${CONST_HOST}/${CONST_PLATCONFIG}:${CONST_JETTY_BASE}/${CONST_PLATCONFIG} \
-v ${CONST_HOST}/${CONST_SEQ}:${CONST_JETTY_BASE}/${CONST_SEQ} \
-v ${CONST_HOST}/${CONST_JDBC_OFF}:${CONST_JETTY_BASE}/${CONST_JDBC_OFF} \
-v ${CONST_HOST}/${CONST_JDBC_ON}:${CONST_JETTY_BASE}/${CONST_JDBC_ON} \
-v /home/pbmp/webmerser/logs/webmerser:/home/pbmp/jetty-web-merser/logs \
-v /etc/localtime:/etc/localtime \
-v /home/pbmp/webmerser/serial:/home/pbmp/jetty-web-merser/webapps/webmerser/WEB-INF/classes/serial \
-v /usr/share/zoneinfo/Asia/Shanghai:/usr/share/zoneinfo/Asia/Shanghai \
${IMAGE_NAME}

sleep 5
docker top ${APPNAME}

echo "任务执行成功"

#run.sh 主要作用 拉取镜像、创建新应用 ,并制定映射关系

4. 开发jenkins脚本配置–编译应用、创建镜像、启动应用

cd ${WORKSPACE}/webmerser_v2/ansible
if [ -d "webmerser" ]; then
 echo "Delete dir webmerser"
 rm -rf "webmerser"
fi
cp -r ${WORKSPACE}/webmerser_v2/target/webmerser . 
chmod +x builder.sh
./builder.sh ${version}


# deploy image
image_name="10.10.67.139:5000/prov-remote/webmerser:${version}"
cd /usr/mpsp/webmerser
ansible -i hosts webmerser_dev -m shell -a "~/webmerser/run/webmerser.sh ${image_name}"

5. 测试jenkins脚本配置–启动应用

cd /usr/mpsp/webmerser
ansible -i hosts webmerser_test -m shell -a "~/run/webmerser.sh ${image}"

6. 生产jenkins脚本配置–从开发仓库push镜像到生产docker仓库

if [ -e ${image} ]; then
   echo “请输入镜像名”
   exit 1
fi

PROD_IP="172.16.59.66:80"
image_name_short=${image#"10.10.67.139:5000"}
echo ${image_name_short}
prod_image=${PROD_IP}${image_name_short}
#execute ansible
cd /usr/mpsp/webmerser
ansible -i hosts webmerser_test -m shell -a "docker tag ${image} ${prod_image}"
ansible -i hosts webmerser_test -m shell -a "docker login -u admin -p 12345 172.16.59.66:80"
ansible -i hosts webmerser_test -m shell -a "docker push ${prod_image}"

if [ $? == 0 ]; then
   echo "Push Successfully"
   echo "Release product image name: ${prod_image}"
fi


发表评论

电子邮件地址不会被公开。 必填项已用*标注