From 44dac027ac523a6cc42b35c97c99c8fb6b90577c Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Sat, 17 May 2025 17:09:46 +0700 Subject: [PATCH] retention php debian image + mysqldump/mariadb-dump auto detect --- .docker/php/Dockerfile | 100 +++++++++++++--------------- .docker/php/DockerfileDebian | 91 +++++++++++++++++++++++++ .docker/php/entrypoint.sh | 51 +++++++------- app/Repositories/ToolRepository.php | 15 +++-- docker-compose.yml | 3 + 5 files changed, 177 insertions(+), 83 deletions(-) create mode 100644 .docker/php/DockerfileDebian diff --git a/.docker/php/Dockerfile b/.docker/php/Dockerfile index a1ce506c..8b8a3247 100644 --- a/.docker/php/Dockerfile +++ b/.docker/php/Dockerfile @@ -1,79 +1,72 @@ # 👷‍♀️ 第一阶段:构建阶段,包含所有开发依赖 -FROM php:8.2-fpm AS builder +FROM php:8.2-fpm-alpine AS builder -# 安装依赖 -RUN apt-get update && apt-get install -y \ - git \ - unzip \ +RUN apk add --no-cache \ + $PHPIZE_DEPS \ libzip-dev \ libpng-dev \ - libjpeg-dev \ - libwebp-dev \ - libfreetype6-dev \ - libicu-dev \ + libjpeg-turbo-dev \ + freetype-dev \ + icu-dev \ libxml2-dev \ - libgmp-dev \ - libonig-dev \ - curl \ - wget \ - rsync \ - pkg-config \ - build-essential \ - && docker-php-ext-configure gd --with-freetype --with-jpeg --with-webp \ - && docker-php-ext-install -j$(nproc) \ - bcmath \ - pdo_mysql \ - mysqli \ - gd \ - pcntl \ - sockets \ - gmp \ - zip \ - intl \ - opcache + libwebp-dev \ + gmp-dev \ + oniguruma-dev \ + linux-headers + +RUN docker-php-ext-configure gd --with-freetype --with-jpeg --with-webp + +RUN docker-php-ext-install -j$(nproc) \ + bcmath \ + pdo_mysql \ + mysqli \ + gd \ + pcntl \ + sockets \ + gmp \ + zip \ + intl \ + opcache # 检查 pecl.php.net 可用性 RUN curl -Is https://pecl.php.net | head -n 1 | grep "200" \ || (echo "❌ pecl.php.net unreachable, aborting build." && exit 1) + # 安装 redis 扩展 RUN pecl channel-update pecl.php.net \ && pecl install redis \ && docker-php-ext-enable redis # 👨‍🍳 第二阶段:运行阶段,仅包含必要运行环境 -FROM php:8.2-fpm +FROM php:8.2-fpm-alpine -# 安装运行所需依赖(无需 dev 工具) -RUN apt-get update && apt-get install -y \ - libzip4 \ - libpng-dev \ - libjpeg-dev \ - libwebp-dev \ - libfreetype6 \ - libicu-dev \ - libxml2 \ - libgmp-dev \ - libonig-dev \ - git \ - curl \ - wget \ - rsync \ - unzip \ - bash \ - netcat-openbsd \ - default-mysql-client \ - && apt-get clean && rm -rf /var/lib/apt/lists/* - -# 复制构建好的 PHP 扩展配置 +# 复制已构建的扩展 COPY --from=builder /usr/local/lib/php/extensions /usr/local/lib/php/extensions COPY --from=builder /usr/local/etc/php/conf.d/ /usr/local/etc/php/conf.d/ +# 安装运行时所需依赖 +RUN apk add --no-cache \ + wget \ + rsync \ + libzip \ + libpng \ + libjpeg-turbo \ + libwebp \ + freetype \ + icu \ + libxml2 \ + gmp \ + oniguruma \ + git \ + mysql-client \ + mariadb-connector-c + # 配置 www.conf RUN sed -i \ -e 's/;catch_workers_output = .*/catch_workers_output = yes/g' \ - -e 's/;php_admin_flag\[log_errors\] = .*/php_admin_flag[log_errors] = on/g' \ - -e 's/;php_admin_value\[error_log\] = .*/php_admin_value[error_log] = \/dev\/stderr/g' \ + -e 's/;php_admin_flag\[log_errors\] = .*/php_admin_flag\[log_errors\] = on/g' \ + -e 's/;php_admin_value\[error_log\] = .*/php_admin_value\[error_log\] = \/dev\/stderr/g' \ /usr/local/etc/php-fpm.d/www.conf # 配置 PHP 错误日志输出到 stderr @@ -84,7 +77,6 @@ RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local ENV LOG_CHANNEL=stderr -# 复制入口脚本 COPY entrypoint.sh /usr/local/bin/entrypoint.sh RUN chmod +x /usr/local/bin/entrypoint.sh diff --git a/.docker/php/DockerfileDebian b/.docker/php/DockerfileDebian new file mode 100644 index 00000000..a1ce506c --- /dev/null +++ b/.docker/php/DockerfileDebian @@ -0,0 +1,91 @@ +# 👷‍♀️ 第一阶段:构建阶段,包含所有开发依赖 +FROM php:8.2-fpm AS builder + +# 安装依赖 +RUN apt-get update && apt-get install -y \ + git \ + unzip \ + libzip-dev \ + libpng-dev \ + libjpeg-dev \ + libwebp-dev \ + libfreetype6-dev \ + libicu-dev \ + libxml2-dev \ + libgmp-dev \ + libonig-dev \ + curl \ + wget \ + rsync \ + pkg-config \ + build-essential \ + && docker-php-ext-configure gd --with-freetype --with-jpeg --with-webp \ + && docker-php-ext-install -j$(nproc) \ + bcmath \ + pdo_mysql \ + mysqli \ + gd \ + pcntl \ + sockets \ + gmp \ + zip \ + intl \ + opcache + +# 检查 pecl.php.net 可用性 +RUN curl -Is https://pecl.php.net | head -n 1 | grep "200" \ + || (echo "❌ pecl.php.net unreachable, aborting build." && exit 1) + +# 安装 redis 扩展 +RUN pecl channel-update pecl.php.net \ + && pecl install redis \ + && docker-php-ext-enable redis + +# 👨‍🍳 第二阶段:运行阶段,仅包含必要运行环境 +FROM php:8.2-fpm + +# 安装运行所需依赖(无需 dev 工具) +RUN apt-get update && apt-get install -y \ + libzip4 \ + libpng-dev \ + libjpeg-dev \ + libwebp-dev \ + libfreetype6 \ + libicu-dev \ + libxml2 \ + libgmp-dev \ + libonig-dev \ + git \ + curl \ + wget \ + rsync \ + unzip \ + bash \ + netcat-openbsd \ + default-mysql-client \ + && apt-get clean && rm -rf /var/lib/apt/lists/* + +# 复制构建好的 PHP 扩展配置 +COPY --from=builder /usr/local/lib/php/extensions /usr/local/lib/php/extensions +COPY --from=builder /usr/local/etc/php/conf.d/ /usr/local/etc/php/conf.d/ + +# 配置 www.conf +RUN sed -i \ + -e 's/;catch_workers_output = .*/catch_workers_output = yes/g' \ + -e 's/;php_admin_flag\[log_errors\] = .*/php_admin_flag[log_errors] = on/g' \ + -e 's/;php_admin_value\[error_log\] = .*/php_admin_value[error_log] = \/dev\/stderr/g' \ + /usr/local/etc/php-fpm.d/www.conf + +# 配置 PHP 错误日志输出到 stderr +RUN echo "error_log = /dev/stderr" >> /usr/local/etc/php/conf.d/error-logging.ini + +# 安装 Composer +RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer + +ENV LOG_CHANNEL=stderr + +# 复制入口脚本 +COPY entrypoint.sh /usr/local/bin/entrypoint.sh +RUN chmod +x /usr/local/bin/entrypoint.sh + +ENTRYPOINT ["entrypoint.sh"] diff --git a/.docker/php/entrypoint.sh b/.docker/php/entrypoint.sh index 165e9780..2b0f2838 100644 --- a/.docker/php/entrypoint.sh +++ b/.docker/php/entrypoint.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh # 定义颜色 COLOR_RED='\033[0;31m' @@ -24,6 +24,31 @@ echo_error() { echo -e "${COLOR_RED}[ERROR]${COLOR_RESET} $*" } +wait_for_service() { + name="$1" + host="$2" + port="$3" + maxWaitSeconds="$4" + waited=0 + + echo_info "🔍 Checking $name at $host:$port..." + + until nc -z "$host" "$port" >/dev/null 2>&1; do + if [ "$waited" -ge "$maxWaitSeconds" ]; then + echo_error "❌ $name not available after ${maxWaitSeconds}s. Exiting." + exit 1 + fi + echo_info "⏳ Waiting for $name... (${waited}s elapsed)" + sleep 2 + waited=$((waited + 2)) + done + + echo_success "✅ $name is available." +} + +wait_for_service "MySQL" mysql 3306 30 +wait_for_service "Redis" redis 6379 30 + # 正式开始 echo_info "Starting container for SERVICE_NAME=$SERVICE_NAME..." @@ -36,30 +61,6 @@ VENDOR_DIR="${ROOT_PATH}/vendor" chown -R www-data:www-data $ROOT_PATH -mysql_timeout=30 -until nc -z mysql 3306; do - echo_info "Waiting for MySQL to be ready..." - sleep 2 - ((mysql_timeout--)) - if [ $mysql_timeout -le 0 ]; then - echo "❌ MySQL connection timeout." - exit 1 - fi -done -echo_success "MySQL is ready." - -redis_timeout=30 -until nc -z redis 6379; do - echo_info "Waiting for Redis to be ready..." - sleep 2 - ((redis_timeout--)) - if [ $redis_timeout -le 0 ]; then - echo "❌ Redis connection timeout." - exit 1 - fi -done -echo_success "Redis is ready." - if [ "$SERVICE_NAME" = "php" ]; then if [ ! -f "$ENV_FILE" ]; then echo_info ".env file: $ENV_FILE not exists, copy $SOURCE_DIR to $TARGET_DIR ..." diff --git a/app/Repositories/ToolRepository.php b/app/Repositories/ToolRepository.php index ebf88085..87a61255 100644 --- a/app/Repositories/ToolRepository.php +++ b/app/Repositories/ToolRepository.php @@ -93,10 +93,17 @@ class ToolRepository extends BaseRepository $connectionName = config('database.default'); $config = config("database.connections.$connectionName"); $filename = sprintf('%s/%s.database.%s.sql', $this->getBackupExportPath(), basename(base_path()), date('Ymd.His')); - $command = sprintf( - 'mysqldump --user=%s --password=%s --host=%s --port=%s --single-transaction --no-create-db --no-tablespaces %s >> %s 2>&1', - $config['username'], $config['password'], $config['host'], $config['port'], $config['database'], $filename, - ); + if (command_exists("mariadb-dump")) { + $command = sprintf( + 'mariadb-dump --user=%s --password=%s --host=%s --port=%s --single-transaction --no-create-db --no-tablespaces --ssl=0 %s >> %s 2>&1', + $config['username'], $config['password'], $config['host'], $config['port'], $config['database'], $filename, + ); + } else { + $command = sprintf( + 'mysqldump --user=%s --password=%s --host=%s --port=%s --single-transaction --no-create-db --no-tablespaces --ssl-mode=DISABLED %s >> %s 2>&1', + $config['username'], $config['password'], $config['host'], $config['port'], $config['database'], $filename, + ); + } $result = exec($command, $output, $result_code); do_log(sprintf( "command: %s, output: %s, result_code: %s, result: %s, filename: %s", diff --git a/docker-compose.yml b/docker-compose.yml index cf294a92..57481690 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -38,6 +38,7 @@ services: max-file: "3" networks: - appnet + restart: always scheduler: image: nexusphp_php @@ -57,6 +58,7 @@ services: max-file: "3" networks: - appnet + restart: always cleanup: image: nexusphp_php @@ -76,6 +78,7 @@ services: max-file: "3" networks: - appnet + restart: always openresty: build: