This commit is contained in:
wandoubaba 2023-10-28 10:21:35 +08:00
parent 6a4b97665a
commit 95e81c0f1f
5 changed files with 177 additions and 69 deletions

View File

@ -20,8 +20,12 @@
| 文件 | 作用 | 用法或说明 | | 文件 | 作用 | 用法或说明 |
|---|---|---| |---|---|---|
|composer|调起容器中的`composer`命令的shell脚本|`./composer $args...`| |composer|调起容器中的`composer`命令的shell脚本|用法与主机中的compose完全一致`./composer $args...`|
|start|调起容器中的`php start.php`命令的shell脚本|`./start`或`./start -d`| |run|调起容器中的`php start.php`命令的shell脚本支持一系统参数并支持-d模式|`./run help`可以查看用法|
|docker-compose.yml|运行容器的编排文件|一般不需要理会这个文件composer和start脚本都需要依赖这个文件| |docker-compose.yml|运行容器的编排文件|一般不需要理会这个文件composer和start脚本都需要依赖这个文件|
|docker.conf|定义运行服务的容器名称和使用的镜像版本|注意container_name不要与系统中其他服务的容器重名| |docker.conf|定义运行服务的容器名称和使用的镜像版本|注意container_name不要与系统中其他服务的容器重名|
|php.ini|映射到容器中的php.ini配置文件|容器中已经安装的扩展都有单独的配置文件,这里不会有体现| |php.ini|映射到容器中的php.ini配置文件|容器中已经安装的扩展都有单独的配置文件,这里不会有体现|
## 服务端口
在.env文件中可以定义服务端口默认情况下docker容器是在host网络下启动的当然实际使用中也可以改成端口映射模式。

View File

@ -7,9 +7,7 @@ source docker.conf
export WORKERMAN_CONTAINER_NAME=$container_name export WORKERMAN_CONTAINER_NAME=$container_name
export WORKERMAN_IMAGE_VERSION=$image_version export WORKERMAN_IMAGE_VERSION=$image_version
# 判断WORKERMAN_CONTAINER_NAME的docker容器是否存在如果存在删除它 # docker-compose.yml中的services下面的名字
if docker ps -a --format '{{.Names}}' | grep -q "^$WORKERMAN_CONTAINER_NAME$"; then service_name="webman-jsonrpc"
docker rm -f $WORKERMAN_CONTAINER_NAME
fi
docker compose run --rm $WORKERMAN_CONTAINER_NAME composer "$@" docker compose run --rm "$service_name" composer "$@"

View File

@ -3,7 +3,7 @@ version: "3.1"
services: services:
webman-jsonrpc: webman-jsonrpc:
image: wandoubaba517/workerman:${WORKERMAN_IMAGE_VERSION} image: wandoubaba517/workerman:${WORKERMAN_IMAGE_VERSION}
container_name: webman-jsonrpc container_name: ${WORKERMAN_CONTAINER_NAME}
network_mode: host network_mode: host
restart: always restart: always
volumes: volumes:

167
run Executable file
View File

@ -0,0 +1,167 @@
#!/bin/bash
# 读取docker.conf文件
source docker.conf
# 设置系统环境变量
export WORKERMAN_CONTAINER_NAME=$container_name
export WORKERMAN_IMAGE_VERSION=$image_version
service_name="webman-jsonrpc"
# 检查docker-compose.yml文件是否存在
if [ ! -f "docker-compose.yml" ]; then
echo "Error: docker-compose.yml file not found"
exit 1
fi
# 定义帮助函数
show_help() {
echo "Usage: ./run [COMMAND] [OPTION]"
echo ""
echo "Commands:"
echo " start run once"
echo " status service status(daemon mode only)"
echo " reload reload files(daemon mode only)"
echo " restart restart service(daemon mode only)"
echo " stop stop service and container"
echo " init delete container and rebuild"
echo " destroy stop & remove the container"
echo " help show this help message"
echo "Options:"
echo " -d run in daemon mode.(start, restart, init)"
}
# 定义安装依赖的函数
install_dependencies() {
# 执行一次composer install
docker compose run --rm "$service_name" composer install
}
# 判断容器是否存在并且正在运行
is_container_running() {
if docker ps -a --format '{{.Names}}' | grep -q "$WORKERMAN_CONTAINER_NAME"; then
if docker ps --format '{{.Names}}' | grep -q "$WORKERMAN_CONTAINER_NAME"; then
return 0
else
return 1
fi
else
return 2
fi
}
# 判断是否需要在后台运行
is_daemon_mode() {
for arg in "$@"; do
if [[ "$arg" == "-d" ]]; then
return 1
fi
done
return 0
}
# 解析命令行参数
case "$1" in
start)
# 判断容器是否存在并且正在运行
if is_container_running; then
echo "container $WORKERMAN_CONTAINER_NAME is already running"
exit 1
elif [[ $? -eq 1 ]]; then
# 存在但未运行,直接启动
if is_daemon_mode "$@"; then
docker start -a "$WORKERMAN_CONTAINER_NAME"
else
docker start "$WORKERMAN_CONTAINER_NAME"
fi
else
# 不存在执行docker-compose up
if is_daemon_mode "$@"; then
docker compose up
else
docker compose up -d
fi
fi
;;
restart)
# 判断容器是否存在并且正在运行
if is_container_running; then
if is_daemon_mode "$@"; then
docker stop "$WORKERMAN_CONTAINER_NAME"
docker start -a "$WORKERMAN_CONTAINER_NAME"
else
docker restart "$WORKERMAN_CONTAINER_NAME"
fi
elif [[ $? -eq 1 ]]; then
# 存在但未运行,直接启动
if is_daemon_mode "$@"; then
docker start -a "$WORKERMAN_CONTAINER_NAME"
else
docker start "$WORKERMAN_CONTAINER_NAME"
fi
else
# 不存在执行docker-compose up
if is_daemon_mode "$@"; then
docker compose up
else
docker compose up -d
fi
fi
;;
stop)
docker stop "$WORKERMAN_CONTAINER_NAME"
;;
reload)
# 判断容器是否存在并且正在运行
if is_container_running; then
# 执行start.php reload命令
docker exec -it "$WORKERMAN_CONTAINER_NAME" php start.php reload
else
echo "container $WORKERMAN_CONTAINER_NAME is not running"
exit 1
fi
;;
status)
# 判断容器是否存在并且正在运行
if is_container_running; then
# 执行start.php status命令
docker exec -it "$WORKERMAN_CONTAINER_NAME" php start.php status
else
echo "container $WORKERMAN_CONTAINER_NAME is not running"
exit 1
fi
;;
init)
read -p "Will delete current container. Are you sure? (y/n)" confirm
if [ "$confirm" != "y" ]; then
echo "Aborted."
exit 1
fi
docker rm -f "$WORKERMAN_CONTAINER_NAME"
# 执行docker-compose up
if is_daemon_mode "$@"; then
docker compose up
else
docker compose up -d
fi
;;
destroy)
read -p "Are you sure you want to destroy the container? (y/n)" confirm
if [ "$confirm" = "y" ]; then
docker rm -f "$WORKERMAN_CONTAINER_NAME"
echo "Container $WORKERMAN_CONTAINER_NAME has been destroyed."
else
echo "Aborted."
exit 1
fi
;;
help)
show_help
;;
*)
echo "Error: Invalid command"
show_help
exit 1
;;
esac

61
start
View File

@ -1,61 +0,0 @@
#!/bin/bash
# 读取docker.conf文件
source docker.conf
# 设置系统环境变量
export WORKERMAN_CONTAINER_NAME=$container_name
export WORKERMAN_IMAGE_VERSION=$image_version
# 检查docker-compose.yml文件是否存在
if [ ! -f "docker-compose.yml" ]; then
echo "Error: docker-compose.yml file not found"
exit 1
fi
# 定义帮助函数
show_help() {
echo "Usage: ./start [COMMAND]"
echo ""
echo "Commands:"
echo " run once"
echo " start run once"
echo " -d run in daemon mode"
echo " status service status(daemon mode only)"
echo " reload reload files(daemon mode only)"
echo " restart restart service(daemon mode only)"
echo " stop stop service and container"
echo " help show this help message"
}
# 定义安装依赖的函数
install_dependencies() {
# 执行一次composer install
docker compose run --rm $WORKERMAN_CONTAINER_NAME composer install
}
# 根据参数执行不同的命令
if [[ $# -eq 0 || $1 == "start" ]]; then
install_dependencies
docker compose run --rm $WORKERMAN_CONTAINER_NAME php start.php start
elif [[ $1 == "-d" ]]; then
install_dependencies
docker compose up -d
elif [[ $1 == "help" ]]; then
show_help
else
# 判断WORKERMAN_CONTAINER_NAME的docker容器是否存在如果不存在直接退出
if ! docker ps -a --format '{{.Names}}' | grep -q "^$WORKERMAN_CONTAINER_NAME$"; then
echo "container not exist."
exit 1
fi
if ! docker ps --format '{{.Names}}' | grep -q "^$WORKERMAN_CONTAINER_NAME$"; then
echo "container is not running."
exit 1
fi
# 执行php start命令
docker exec -it $WORKERMAN_CONTAINER_NAME php start.php "$@"
if [[ $1 == "stop" ]]; then
docker stop $WORKERMAN_CONTAINER_NAME
fi
fi