docker项目切换(nginx)、重启shell 脚本

docker项目切换、重启脚本

  • 背景
  • 具体操作
    • nginx配置
      • 配置文件1
      • 配置文件2
  • 编写nginx替换脚本(用来执行端口替换)
  • 编写启动脚本
  • dockerfile文件正常编写
  • 给脚本授权
  • 执行./start脚本

背景

项目部署docker中,更新项目时,需要将原原来的容器停止,再启动新的容器,这样会有一个空窗期,导致不可用
解决方案: 映射不同的端口并启动新的容器,将nginx转发到新容器,停止旧容器

具体操作

说明

  1. nginx配置文件不能放默认nginx.conf中
  2. 启动脚本start.sh中project_name不能有包含关系(在同一台服务器,如果有包含关系,将会停止掉包含的容器)
  3. 项目启动时输入的端口必须存在于nginx配置文件upstream-app.conf中

nginx配置

配置文件1

名称任意,示例app.conf

server {
	listen       80;
	# 修改为你自己的域名
    server_name xx.venny.cn;

	location / {
		proxy_set_header Host $host;
		root /app/html/;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		try_files $uri $uri/ /index.html;
		index /index.html;
	}


        location /api/ {
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header REMOTE-HOST $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                add_header Access-Control-Allow-Methods *;
                add_header Access-Control-Allow-Origin $http_origin;
                # 与自定义的保持一致
                proxy_pass http://stream-app/;
        }
}

配置文件2

名称任意,示例:upstream-app.conf

# 自定义upstream名
upstream stream-app {
# server 127.0.0.1:8000 weight=10;
# server 127.0.0.1:8001 weight=10;
# server 127.0.0.1:8002 weight=10;
# server 127.0.0.1:8003 weight=10;
# server 127.0.0.1:8004 weight=10;
# server 127.0.0.1:8005 weight=10;
# server 127.0.0.1:8006 weight=10;
# server 127.0.0.1:8007 weight=10;
# server 127.0.0.1:8008 weight=10;
# server 127.0.0.1:8009 weight=10;
}

编写nginx替换脚本(用来执行端口替换)

名称为replace.sh

#!/bin/bash
port=$1
conf_name=$2
cat $conf_name | while read l
do
 if [[ $l =~ "127.0.0.1:$port" ]];then
  #去除#号
  echo ${l##*#}
 elif  [[ $l =~ "server" ]];then
  #添加#号
  if [[ !($l =~ '#') ]];then
    echo '# '$l
  else
    echo $l
  fi
 else
  echo $l
 fi
done

编写启动脚本

start.sh


#/bin/bash

check_y(){
  if [ -n "$1" ] && [ "$1" = 'y' ];then
    echo 1
  else
    echo 0
  fi
}
# 指定项目nginx配置文件位置,根据实际位置填写
ng_path=/etc/nginx/
# 指定项目upstream配置文件名称
ng_conf_name=upstream-qin.conf
# 指定项目名称
project_name=app
# 版本默认取当前日期
version=$(date +%Y%m%d%H%M%S)
echo '当前版本号:'$version

read -rp "请输入启动端口号:" port

echo '当前启动端口:'$port

echo "开始构建容器"
docker build -t $project_name:$version .
echo "容器构建成功!"
sleep 5

echo "容器启动中...."
# 健康检查(接口/health必须存在,否则健康检查失败)、及启动项目,容器启动端口固定8080
container_full_id=$(docker run \
--health-cmd="curl --fail localhost:8080/health || exit 1" \
--health-interval=5s \
--health-retries=15 \
--health-timeout=10s \
-itd -p $port:8080 --name=$project_name$version -v /app/logs:/logs -v /etc/localtime:/etc/localtime $project_name:$version)

echo "容器启动完毕...."

container_id=$(echo $container_full_id|cut -b 1-12)

echo "容器id为$container_id"

echo '开始健康检查'

health=1

while [[ -z "$(docker ps -a -f id=${container_id} -f health=healthy | awk 'NR>1 {print $1}')" ]]
do
  echo '执行中....'
  if [[ "$(docker ps -a -f id=${container_id} -f health=unhealthy | awk 'NR>1 {print $1}')" == $container_id ]];then
    echo '健康检查失败'
    health=2
    break
  fi
  sleep 5;
done

if [[ $health != 1 ]];then
   exit
fi

echo '健康检查成功'

sleep 3

echo "开始修改nginx配置"

sleep 1
# 替换nginx upstream文件
./replace.sh $port ${ng_path}${ng_conf_name} > ${project_name}.ng.conf.temp

cat ${project_name}.ng.conf.temp > ${ng_path}${ng_conf_name}

rm -f ${project_name}.ng.conf.temp


echo 'nginx配置修改成功'

echo '当前配置:'

cat ${ng_path}${ng_conf_name}

sleep 2

read -rp '是否重启nginx  y/n:' reload_ngx

if [ "$(check_y $reload_ngx)" = "1" ];then
    nginx -s reload
    echo '重启nginx'
else
  exit
fi

sleep 1
echo "开始关闭旧容器"

sleep 10

if [ -n "$container_id" ];then
close_containers=$(docker ps |grep $project_name|grep -v $container_id|awk '{print "docker rm -f "$1}'|/bin/bash)

echo $close_containers

for cc in $close_containers
do
  echo "关闭容器$cc"
done

fi

read -rp '是否删除旧镜像y/n:' del_imgs

if [ "$(check_y $del_imgs)" = "1" ];then
  echo "开始删除原镜像"
  sleep 1
  docker images|grep -v $version|grep $project_name|awk '{print "docker rmi -f "$1":"$2}'|/bin/bash
fi

dockerfile文件正常编写

省略

给脚本授权

chmod +x start.sh

执行./start脚本

./start

根据提示输入端口号(端口号upstream-app中必须存在),依次输入y,y即可

你可能感兴趣的:(docker,docker,nginx,容器)