简单控制docker-compose启停

逸兴
逸兴
逸兴
57
文章
25
评论
2020-03-2211:53:34
评论
1764字阅读5分52秒
摘要

1)nginx 的语法测试,配置重载;
2)lnmp项目的启停控制;

在安装lnmp的docker compose环境中,为了方便网站的启停,用脚本来管理。

主要使用了docker-compose的 -f 指定compose文件的方式来实现的。

本文主是上篇文章 《docker-compose 部署nginx1.7 + php7.3.5 + MySQL5.7》 的补充

1.0 脚本

脚本放在 /data/mcabana/lastackd

compose 文件路径,和 nginx容器的id,需要手动填写。

脚本功能:

1)nginx 的语法测试,配置重载;

2)lnmp项目的启停控制;

#!/bin/bash
#
# fileName: lastackd.sh
# author: www.mcabana.com
# mail: 934356678@qq.com
# created: Fri 13 Mar 2020 05:57:41 PM CST
# 
### BEGIN INIT INFO
# Description:
#
#
### END INIT INFO

. /etc/init.d/functions 

# 项目的docker compose文件位置
dm_conf="/data/mcabana/docker-compose.yml"

# nginx 容器的id
nginx_id="64e752fbacb3"


function usage() {

	if [ $# -lt 1 ];then
		echo "USAGE: lastackd {start|stop|restart|-t|-h|-s}"
		exit
	fi

}

function f_help() {
cat <<EOF
www.mcabana.com
Usage: lastackd [OPTION]

Options:
	-h		  : this help
	-t		  : test nginx configuration and exit
	-s		  : reload nginx configuration and exit
	start		  : start docker-compose web-lastack container
	stop		  : stop docker-compose web-lastack container
	restart		  : restart docker-compose web-lastack container

	
EOF

}

function check_conf() {
	docker container exec -it $nginx_id nginx -t &>/dev/null
	test $? -eq 0 && action "nginx test" /bin/true
}

function reload_nginx() {
	docker container exec -it $nginx_id nginx -s reload
	test $? -eq 0 && action "nginx reolad" /bin/true

}


function main() {
	usage "$*"
	case $1 in
		-h)
			f_help
			;;
		-t)
			check_conf
			;;
		-s)
			reload_nginx
			;;
		start|stop)
			docker-compose -f $dm_conf $1
			;;
		re)
			docker-compose -f $dm_conf restart
			;;
		*)
			usage
			;;
	esac


}

main "$*"

2.0 添加软连接,设置环境变量

脚本写完后,为了方便随地调用,就添加下软连接,以后使用就不需要用绝对路径了。

ln -s /data/mcabana/lastackd /usr/bin/lastack

然后就可以使用了,如获取帮助

[root mcabana 12:00:47] /data
-- $ lastack -h
www.mcabana.com
Usage: lastackd [OPTION]

Options:
	-h		  : this help
	-t		  : test nginx configuration and exit
	-s		  : reload nginx configuration and exit
	start		  : start docker-compose web-lastack container
	stop		  : stop docker-compose web-lastack container
	restart		  : restart docker-compose web-lastack container

	
[root mcabana 12:00:48] /data

# 重启项目

简单控制docker-compose启停

其他使用可参照-h,帮助信息。




https://www.hugbg.com/archives/1721.html
逸兴
  • 本文由 发表于 2020-03-2211:53:34
  • 除非特殊声明,本站文章均为原创,转载请务必保留本文链接
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: