mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-24 12:07:23 +08:00
retention php debian image + mysqldump/mariadb-dump auto detect
This commit is contained in:
+46
-54
@@ -1,79 +1,72 @@
|
|||||||
# 👷♀️ 第一阶段:构建阶段,包含所有开发依赖
|
# 👷♀️ 第一阶段:构建阶段,包含所有开发依赖
|
||||||
FROM php:8.2-fpm AS builder
|
FROM php:8.2-fpm-alpine AS builder
|
||||||
|
|
||||||
# 安装依赖
|
RUN apk add --no-cache \
|
||||||
RUN apt-get update && apt-get install -y \
|
$PHPIZE_DEPS \
|
||||||
git \
|
|
||||||
unzip \
|
|
||||||
libzip-dev \
|
libzip-dev \
|
||||||
libpng-dev \
|
libpng-dev \
|
||||||
libjpeg-dev \
|
libjpeg-turbo-dev \
|
||||||
libwebp-dev \
|
freetype-dev \
|
||||||
libfreetype6-dev \
|
icu-dev \
|
||||||
libicu-dev \
|
|
||||||
libxml2-dev \
|
libxml2-dev \
|
||||||
libgmp-dev \
|
libwebp-dev \
|
||||||
libonig-dev \
|
gmp-dev \
|
||||||
curl \
|
oniguruma-dev \
|
||||||
wget \
|
linux-headers
|
||||||
rsync \
|
|
||||||
pkg-config \
|
RUN docker-php-ext-configure gd --with-freetype --with-jpeg --with-webp
|
||||||
build-essential \
|
|
||||||
&& docker-php-ext-configure gd --with-freetype --with-jpeg --with-webp \
|
RUN docker-php-ext-install -j$(nproc) \
|
||||||
&& docker-php-ext-install -j$(nproc) \
|
bcmath \
|
||||||
bcmath \
|
pdo_mysql \
|
||||||
pdo_mysql \
|
mysqli \
|
||||||
mysqli \
|
gd \
|
||||||
gd \
|
pcntl \
|
||||||
pcntl \
|
sockets \
|
||||||
sockets \
|
gmp \
|
||||||
gmp \
|
zip \
|
||||||
zip \
|
intl \
|
||||||
intl \
|
opcache
|
||||||
opcache
|
|
||||||
|
|
||||||
# 检查 pecl.php.net 可用性
|
# 检查 pecl.php.net 可用性
|
||||||
RUN curl -Is https://pecl.php.net | head -n 1 | grep "200" \
|
RUN curl -Is https://pecl.php.net | head -n 1 | grep "200" \
|
||||||
|| (echo "❌ pecl.php.net unreachable, aborting build." && exit 1)
|
|| (echo "❌ pecl.php.net unreachable, aborting build." && exit 1)
|
||||||
|
|
||||||
|
|
||||||
# 安装 redis 扩展
|
# 安装 redis 扩展
|
||||||
RUN pecl channel-update pecl.php.net \
|
RUN pecl channel-update pecl.php.net \
|
||||||
&& pecl install redis \
|
&& pecl install redis \
|
||||||
&& docker-php-ext-enable 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/lib/php/extensions /usr/local/lib/php/extensions
|
||||||
COPY --from=builder /usr/local/etc/php/conf.d/ /usr/local/etc/php/conf.d/
|
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
|
# 配置 www.conf
|
||||||
RUN sed -i \
|
RUN sed -i \
|
||||||
-e 's/;catch_workers_output = .*/catch_workers_output = yes/g' \
|
-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_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_value\[error_log\] = .*/php_admin_value\[error_log\] = \/dev\/stderr/g' \
|
||||||
/usr/local/etc/php-fpm.d/www.conf
|
/usr/local/etc/php-fpm.d/www.conf
|
||||||
|
|
||||||
# 配置 PHP 错误日志输出到 stderr
|
# 配置 PHP 错误日志输出到 stderr
|
||||||
@@ -84,7 +77,6 @@ RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local
|
|||||||
|
|
||||||
ENV LOG_CHANNEL=stderr
|
ENV LOG_CHANNEL=stderr
|
||||||
|
|
||||||
# 复制入口脚本
|
|
||||||
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
|
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
|
||||||
RUN chmod +x /usr/local/bin/entrypoint.sh
|
RUN chmod +x /usr/local/bin/entrypoint.sh
|
||||||
|
|
||||||
|
|||||||
@@ -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"]
|
||||||
+26
-25
@@ -1,4 +1,4 @@
|
|||||||
#!/bin/bash
|
#!/bin/sh
|
||||||
|
|
||||||
# 定义颜色
|
# 定义颜色
|
||||||
COLOR_RED='\033[0;31m'
|
COLOR_RED='\033[0;31m'
|
||||||
@@ -24,6 +24,31 @@ echo_error() {
|
|||||||
echo -e "${COLOR_RED}[ERROR]${COLOR_RESET} $*"
|
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..."
|
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
|
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 [ "$SERVICE_NAME" = "php" ]; then
|
||||||
if [ ! -f "$ENV_FILE" ]; then
|
if [ ! -f "$ENV_FILE" ]; then
|
||||||
echo_info ".env file: $ENV_FILE not exists, copy $SOURCE_DIR to $TARGET_DIR ..."
|
echo_info ".env file: $ENV_FILE not exists, copy $SOURCE_DIR to $TARGET_DIR ..."
|
||||||
|
|||||||
@@ -93,10 +93,17 @@ class ToolRepository extends BaseRepository
|
|||||||
$connectionName = config('database.default');
|
$connectionName = config('database.default');
|
||||||
$config = config("database.connections.$connectionName");
|
$config = config("database.connections.$connectionName");
|
||||||
$filename = sprintf('%s/%s.database.%s.sql', $this->getBackupExportPath(), basename(base_path()), date('Ymd.His'));
|
$filename = sprintf('%s/%s.database.%s.sql', $this->getBackupExportPath(), basename(base_path()), date('Ymd.His'));
|
||||||
$command = sprintf(
|
if (command_exists("mariadb-dump")) {
|
||||||
'mysqldump --user=%s --password=%s --host=%s --port=%s --single-transaction --no-create-db --no-tablespaces %s >> %s 2>&1',
|
$command = sprintf(
|
||||||
$config['username'], $config['password'], $config['host'], $config['port'], $config['database'], $filename,
|
'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);
|
$result = exec($command, $output, $result_code);
|
||||||
do_log(sprintf(
|
do_log(sprintf(
|
||||||
"command: %s, output: %s, result_code: %s, result: %s, filename: %s",
|
"command: %s, output: %s, result_code: %s, result: %s, filename: %s",
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ services:
|
|||||||
max-file: "3"
|
max-file: "3"
|
||||||
networks:
|
networks:
|
||||||
- appnet
|
- appnet
|
||||||
|
restart: always
|
||||||
|
|
||||||
scheduler:
|
scheduler:
|
||||||
image: nexusphp_php
|
image: nexusphp_php
|
||||||
@@ -57,6 +58,7 @@ services:
|
|||||||
max-file: "3"
|
max-file: "3"
|
||||||
networks:
|
networks:
|
||||||
- appnet
|
- appnet
|
||||||
|
restart: always
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
image: nexusphp_php
|
image: nexusphp_php
|
||||||
@@ -76,6 +78,7 @@ services:
|
|||||||
max-file: "3"
|
max-file: "3"
|
||||||
networks:
|
networks:
|
||||||
- appnet
|
- appnet
|
||||||
|
restart: always
|
||||||
|
|
||||||
openresty:
|
openresty:
|
||||||
build:
|
build:
|
||||||
|
|||||||
Reference in New Issue
Block a user