From ee9673026838f36acdd8ea8c12457b8d99e4355d Mon Sep 17 00:00:00 2001 From: kuaifan Date: Sat, 4 Apr 2026 08:23:17 +0000 Subject: [PATCH] =?UTF-8?q?feat(install):=20=E5=AE=89=E8=A3=85=E5=92=8C?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=AB=AF=E5=8F=A3=E6=97=B6=E6=A3=80=E6=B5=8B?= =?UTF-8?q?=E7=AB=AF=E5=8F=A3=E6=98=AF=E5=90=A6=E8=A2=AB=E5=8D=A0=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 通过 Docker 试绑定端口的方式检测占用,避免安装流程走到最后才因端口冲突失败。 仅在首次安装或端口变更时检测,重装且端口不变时跳过。 Co-Authored-By: Claude Opus 4.6 (1M context) --- cmd | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/cmd b/cmd index 9443b54da..0d2d95384 100755 --- a/cmd +++ b/cmd @@ -502,6 +502,19 @@ DooTask 管理脚本 EOF } +# 检测端口是否被占用 +# 参数1: 端口号, 参数2: 当前端口号(可选,相同则跳过检测) +check_port() { + local port=$1 + local current_port=$2 + if [[ "$port" -gt 0 ]] && [[ "$port" != "$current_port" ]]; then + if ! docker run --rm -p "${port}:80" --entrypoint true nginx:alpine 2>/dev/null; then + error "端口 ${port} 已被占用,请指定其他端口" + exit 1 + fi + fi +} + # 安装函数 handle_install() { check_sudo @@ -562,8 +575,17 @@ handle_install() { done # 设置端口 + local old_port=$(env_get APP_PORT) [[ "$port" -gt 0 ]] && env_set APP_PORT "$port" + # 检测端口占用(首次安装或端口变更时) + local new_port=$(env_get APP_PORT) + if [ -z "$(docker_name nginx)" ] || [[ "$new_port" != "$old_port" ]]; then + check_port "$new_port" + local ssl_port=$(env_get APP_SSL_PORT) + check_port "$ssl_port" + fi + # 启动PHP容器 $COMPOSE up php -d @@ -764,6 +786,7 @@ case "$1" in ;; "port") shift 1 + check_port "$1" "$(env_get APP_PORT)" env_set APP_PORT "$1" $COMPOSE up -d success "修改成功"