docker web程序本地化_docker本地化部署
#!/bin/bashexport USER_HOME=`pwd`FILE_HOME=${USER_HOME}REMOTE_HOST=${1}die() {if [ $# != 2 ] ; thenecho " The first is return code,the second error message!"echo " e.g.: die 1 'error message'"exit 1;f
#!/bin/bash
export USER_HOME=`pwd`
FILE_HOME=${USER_HOME}
REMOTE_HOST=${1}
die() {
if [ $# != 2 ] ; then
echo " The first is return code,the second error message!"
echo " e.g.: die 1 'error message'"
exit 1;
fi
code=$1
msg=$2
echo "${msg}" && exit "${code}"
}
process(){
#1、检查是否含有base.tar.gz镜像包
cd "${FILE_HOME}" || die 2 "cd ${FILE_HOME} failed"
if [ ! -f "./base.tar.gz" ]; then
die 1 "init failed, not found base.tar.gz"
fi
#2、上传至服务器
scp ./base.tar.gz $REMOTE_HOST:/home/admin/ || die 3 "upload failed, check ssh is already linked"
#3、登录服务器并导入镜像
ssh $REMOTE_HOST "sudo cd /home/admin;sudo docker load < base.tar.gz;sudo rm -rf ./base.tar.gz;exit" || die 4 "load failed..."
}
check() {
if [ -z "$REMOTE_HOST" ]; then
die 5 "check failed,remote host param not input"
fi
}
main() {
check
process
}
main
将initImages.sh放到导出镜像目录下执行
命令
//remoteHost为测试环境机子ip
sh initImages.sh ${remoteHost}
例如
sh initImages.sh admin@10.57.17.247
初始化成功返回示例
Loaded image: xxxxxxx
3、本地化部署项目改造
文件添加
项目根目录添加env文件
env
ENV SHUTTER=10.x.xx.xxx:xxx
ENV CLUSTER=octopus-xxx
ENV GRAYLOG_HOST=10.x.x.xx
ENV GRAYLOG_PORT=80
ENV APP_PORT=8088
ENV ENV=test-xx
ENV APPNAME=octopus-xxx
ENV APP_RUN_MODE=fg
ENV LIMIT_CPU=2
ENV LIMIT_MEM=2
ENV CONSOLE_LEVEL=DEBUG
注意:以上配置根据自身项目需要作出修,不可直接复制!!!!
项目根目录添加本地化部署build脚本
#!/bin/bash
export APPNAME=`sed '/APPNAME/!d;s/.*=//' env | tr -d '\r'`
export USER_HOME=`pwd`
APP_HOME=${USER_HOME}
die() {
if [ $# != 2 ] ; then
echo " The first is return code,the second error message!"
echo " e.g.: die 1 'error message'"
exit 1;
fi
code=$1
msg=$2
echo "${msg}" && exit "${code}"
}
#初始化环境变量
init(){
cp Dockerfile DockerfileBak
sed -i "" "/^ENV.*/d" Dockerfile
sed -i "" "2 r env" Dockerfile
}
# 编译打包
build() {
cd "${APP_HOME}" || die 2 "cd ${APP_HOME} failed"
# 输出commit id
echo -n "$(git rev-parse HEAD|awk '{print substr($0,1,11)}')" > "${APP_HOME}/commit_id"
mvn clean package -Dmaven.test.skip=true || die 301 "Failed compiling project"
rm -rf output
mkdir output
tar czf "output/${APPNAME}-dist.tar.gz" commit_id deploy/tomcat/* deploy/target/* start.shprestop.shstop.shvalidate.sh Dockerfile > /dev/null 2>&1 || die 301 "tar failed"
}
check() {
cd "${APP_HOME}" || die 2 "cd ${APP_HOME} failed"
if [ ! -f "./output/${APPNAME}-dist.tar.gz" ]; then
die 1 "package not generated and build fail..."
fi
die 0 "package generated and build success..."
}
clean(){
rm -rf Dockerfile
rm -rf commit_id
mv DockerfileBak Dockerfile
}
main() {
init
build
clean
check
}
main
注:此文件直接复制,不用做任何更改
项目修改
说明
由于本地化部署打包文件保存至新文件夹output下,为了不上传至gitlab,因此项目需要做小小的改动
最外层pom.xml中的clean插件中fileset添加
output
**/*
.ignore文件添加output文件夹忽略
output
4、用docker启动项目
将output下tar.gz包上传服务器admin目录下
命令
scp -r local_folder remote_ip:remote_folder
举例
scp -r ./octopus-xxx-dist.tar.gz admin@10.x.x.xx:/home/admin/
新建项目文件夹并将压缩包
cd /home/admin
mkdir ${APP_NAME}
tar zxf ${APP_NAME}-dist.tar.gz -C ./${APP_NAME/
在解压后的目录下启动docker
命令
//打包镜像
sudo docker build -t name:tag .
//启动镜像
docker run -d -p 8088:8088 name:tag
注意:build语句最后面有个点
举例
sudo docker build -t xxx:20190303 .
sudo docker run -d -p 8088:8088 xxx:20190303
验证
方法一
命令
sudo docker ps -a
结果
status One of created, restarting, running, removing, paused, exited, or dead
Up x seconds : 成功
Exited x seconds: 停止
方法二
命令
sudo docker exec -it ${containerId} /bin/bash
结果
//进入容器内部查看
ps -ef | grep java
方法三
命令
curl -I -m 10 -o /dev/null -s -w "%{http_code}" "http://127.0.0.1:${APP_PORT}/ok.htm"
回滚、持续发布....
待续...
更多推荐
所有评论(0)