From 6ab0fc1770d0cbedecda69787ffc18ac366e3179 Mon Sep 17 00:00:00 2001 From: "allen.yan" Date: Mon, 9 Jun 2025 17:14:04 +0800 Subject: [PATCH] =?UTF-8?q?Laravel=20=E7=AC=AC=E4=B8=80=E7=89=88=20?= =?UTF-8?q?=E6=95=B4=E5=90=88=E5=9C=A8=E4=B8=80=E8=B5=B7=2020250609?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 27 ++++++++------- Dockerfile.git | 21 ------------ Dockerfile.queue | 17 ---------- docker-compose.yml | 39 ++++----------------- docker/entrypoint.git.sh | 36 ++++++++++---------- docker/entrypoint.sh | 67 +++++++++++++++++-------------------- docker/supervisord.conf | 13 +++++-- docker/supervisord.git.conf | 12 ------- 8 files changed, 81 insertions(+), 151 deletions(-) delete mode 100644 Dockerfile.git delete mode 100644 Dockerfile.queue delete mode 100644 docker/supervisord.git.conf diff --git a/Dockerfile b/Dockerfile index 797f518..f72a6cd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,29 +1,32 @@ FROM php:8.3-fpm +# ---- system & PHP extensions ------------------------------------------------- RUN apt-get update && apt-get install -y \ - git unzip zip curl libpng-dev libjpeg62-turbo-dev libfreetype6-dev \ + git unzip zip curl ca-certificates supervisor cron nano \ + libpng-dev libjpeg62-turbo-dev libfreetype6-dev \ libonig-dev libxml2-dev libzip-dev libpq-dev libicu-dev libxslt-dev \ - libsqlite3-dev sqlite3 cron supervisor bash nano default-mysql-client \ + libsqlite3-dev sqlite3 default-mysql-client \ && curl -fsSL https://deb.nodesource.com/setup_22.x | bash - \ && apt-get install -y nodejs \ && npm install -g npm \ && docker-php-ext-configure gd --with-freetype --with-jpeg \ && docker-php-ext-install -j$(nproc) gd pdo_mysql zip bcmath intl xsl pcntl sockets \ && docker-php-ext-install opcache \ - && pecl install redis && docker-php-ext-enable redis + && pecl install redis && docker-php-ext-enable redis \ + && apt-get clean && rm -rf /var/lib/apt/lists/* +# ---- composer --------------------------------------------------------------- COPY --from=composer:latest /usr/bin/composer /usr/bin/composer -COPY ./docker/php/php.ini /usr/local/etc/php/conf.d/custom-php.ini -COPY ./docker/php/opcache.ini /usr/local/etc/php/conf.d/opcache.ini -COPY ./data/html/ /var/www/html/ -RUN chown -R www-data:www-data /var/www/html +# ---- config files ----------------------------------------------------------- +COPY docker/php/php.ini /usr/local/etc/php/conf.d/custom-php.ini +COPY docker/php/opcache.ini /usr/local/etc/php/conf.d/opcache.ini +COPY docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf +COPY docker/entrypoint.sh /entrypoint.sh +COPY docker/entrypoint.git.sh /entrypoint.git.sh +WORKDIR /var/www -WORKDIR /var/www/html - -COPY ./docker/entrypoint.sh /entrypoint.sh -RUN chmod +x /entrypoint.sh +RUN chmod +x /entrypoint.sh /entrypoint.git.sh ENTRYPOINT ["/entrypoint.sh"] - CMD ["docker-php-entrypoint", "php-fpm"] \ No newline at end of file diff --git a/Dockerfile.git b/Dockerfile.git deleted file mode 100644 index 0264039..0000000 --- a/Dockerfile.git +++ /dev/null @@ -1,21 +0,0 @@ -FROM debian:bullseye-slim - -# 安裝必要工具 -RUN apt-get update && apt-get install -y \ - git supervisor bash curl ca-certificates \ - && apt-get clean && rm -rf /var/lib/apt/lists/* - -# 建立目錄 -RUN mkdir -p /app /var/log /etc/supervisor - -# 複製 supervisord 設定與啟動腳本 -COPY ./docker/supervisord.git.conf /etc/supervisor/supervisord.conf -COPY ./docker/entrypoint.git.sh /entrypoint.sh - -# 權限設定 -RUN chmod +x /entrypoint.sh - -WORKDIR /app - -# 使用 supervisord 啟動 -CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisord.conf"] \ No newline at end of file diff --git a/Dockerfile.queue b/Dockerfile.queue deleted file mode 100644 index 342457d..0000000 --- a/Dockerfile.queue +++ /dev/null @@ -1,17 +0,0 @@ -FROM php:8.3-fpm - -# 安裝必要套件與 PHP 擴充 -RUN apt-get update && apt-get install -y \ - git unzip zip curl libzip-dev libonig-dev libxml2-dev supervisor \ - && docker-php-ext-install pdo_mysql zip bcmath \ - && pecl install redis && docker-php-ext-enable redis - -COPY ./data/html/ /var/www/html/ - -RUN chown -R www-data:www-data /var/www/html - -WORKDIR /var/www/html - -COPY ./docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf - -CMD ["supervisord", "-n", "-c", "/etc/supervisor/conf.d/supervisord.conf"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 48090d8..a6d3062 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,16 +1,4 @@ services: - git_worker: - build: - context: . - dockerfile: Dockerfile.git - container_name: ${APP_NAME}_git_worker - volumes: - - ./data:/app - - ./data/git/logs/:/var/log/ - environment: - - REPO_URL=${REPO_URL} - - BRANCH=${REPO_BRANCH} - restart: unless-stopped mariadb: image: mariadb:10.6 container_name: ${APP_NAME}_db @@ -29,19 +17,19 @@ services: - "3306:3306" networks: - app_network + app: build: context: . dockerfile: Dockerfile container_name: ${APP_NAME}_app volumes: - - ./data/html:/var/www/html - - ./data/php/logs:/var/www/logs - - ./docker/php/php.ini:/usr/local/etc/php/conf.d/custom-php.ini - - ./docker/php/opcache.ini:/usr/local/etc/php/conf.d/opcache.ini + - ./data:/var/www depends_on: - mariadb environment: + REPO_URL: ${REPO_URL} + BRANCH: ${REPO_BRANCH:-main} APP_NAME: ${APP_NAME} APP_URL: ${APP_URL} DB_HOST: ${DB_HOST} @@ -51,6 +39,7 @@ services: DB_PASSWORD: ${DB_PASSWORD} networks: - app_network + nginx: image: nginx:stable-alpine container_name: ${APP_NAME}_nginx @@ -68,20 +57,6 @@ services: NGINX_PORT: ${APP_PORT} networks: - app_network - queue: - build: - context: . - dockerfile: Dockerfile.queue - container_name: ${APP_NAME}_queue - volumes: - - ./data/html:/var/www/html - - ./data/queue/logs:/var/www/logs - - ./docker/supervisord.conf:/etc/supervisor/conf.d/supervisord.conf - depends_on: - - nginx - networks: - - app_network + networks: - app_network: - name: ${APP_NAME}_network - external: true \ No newline at end of file + app_network: \ No newline at end of file diff --git a/docker/entrypoint.git.sh b/docker/entrypoint.git.sh index 2f4219d..aa745f8 100644 --- a/docker/entrypoint.git.sh +++ b/docker/entrypoint.git.sh @@ -1,22 +1,24 @@ -#!/bin/bash +#!/usr/bin/env bash +set -e -REPO_URL=${REPO_URL} -BRANCH=${BRANCH} -TARGET_DIR=/app/html +BRANCH=${BRANCH:-main} +TARGET_DIR=/var/www/html -# 若資料夾為空則 clone -if [ -z "$(ls -A "$TARGET_DIR")" ]; then - echo "[git-worker] Cloning repository into $TARGET_DIR..." - git clone --branch "$BRANCH" "$REPO_URL" "$TARGET_DIR" -else - echo "[git-worker] Repository exists in $TARGET_DIR, skipping clone." -fi - -# 定時 git pull -echo "[git-worker] Starting periodic git pull..." +echo "[git-worker] Loop started for branch $BRANCH" while true; do - cd "$TARGET_DIR" || exit - echo "[git-worker] Pulling latest changes from $BRANCH..." + cd "$TARGET_DIR" + git remote update + LOCAL=$(git rev-parse @) + REMOTE=$(git rev-parse "origin/$BRANCH") + + if [ "$LOCAL" != "$REMOTE" ]; then + echo "[git-worker] Detected new commits, pulling..." git pull origin "$BRANCH" - sleep 300 + + echo "[git-worker] Restarting queue-worker..." + supervisorctl restart queue-worker + else + echo "[git-worker] No changes." + fi + sleep 300 done \ No newline at end of file diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index b739cd1..c5873bc 100644 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -1,51 +1,44 @@ -#!/bin/bash +#!/usr/bin/env bash +set -e -cd /var/www/html +REPO_URL=${REPO_URL} +BRANCH=${BRANCH:-main} +TARGET_DIR=/var/www/html -APP_URL="${APP_URL%/}" - - - -echo "Running composer install..." -composer install --no-interaction --prefer-dist - -if [ ! -f ".env" ]; then - echo "Copying .env.example to .env" - cp .env.example .env +# 1) 初次 clone 或拉取 +if [ -z "$(ls -A "$TARGET_DIR")" ]; then + echo "[entrypoint] Cloning $BRANCH from $REPO_URL ..." + git clone --branch "$BRANCH" "$REPO_URL" "$TARGET_DIR" +else + echo "[entrypoint] Repository already present, skipping first clone." fi -echo "Updating .env configurations..." +cd "$TARGET_DIR" -update_env_var() { - local key=$1 - local value=$2 - grep -q "^${key}=" .env && \ - sed -i "s|^${key}=.*|${key}=${value}|" .env || \ - echo "${key}=${value}" >> .env -} +# 2) Laravel 基礎安裝 +composer install --no-interaction --prefer-dist -update_env_var "APP_NAME" "$APP_NAME" -update_env_var "APP_URL" "$APP_URL" -update_env_var "DB_HOST" "$DB_HOST" -update_env_var "DB_PORT" "$DB_PORT" -update_env_var "DB_DATABASE" "$DB_DATABASE" -update_env_var "DB_USERNAME" "$DB_USERNAME" -update_env_var "DB_PASSWORD" "$DB_PASSWORD" +[ -f .env ] || cp .env.example .env -echo "Generating Laravel app key..." -php artisan key:generate +update_env() { local k=$1 v=$2; grep -q "^$k=" .env && sed -i "s|^$k=.*|$k=$v|" .env || echo "$k=$v" >> .env; } +update_env APP_NAME "$APP_NAME" +update_env APP_URL "${APP_URL%/}" +update_env DB_HOST "$DB_HOST" +update_env DB_PORT "$DB_PORT" +update_env DB_DATABASE "$DB_DATABASE" +update_env DB_USERNAME "$DB_USERNAME" +update_env DB_PASSWORD "$DB_PASSWORD" -echo "Running migrations..." +php artisan key:generate --force php artisan migrate --force -echo "Installing npm packages..." -[ ! -d "node_modules" ] && npm install - -echo "Building front-end assets..." +[ -d node_modules ] || npm install npm run build -echo "Setting permissions for storage and bootstrap/cache..." -chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache -chmod -R 775 /var/www/html/storage /var/www/html/bootstrap/cache +chown -R www-data:www-data storage bootstrap/cache +chmod -R 775 storage bootstrap/cache + +echo "[entrypoint] Starting supervisord ..." +supervisord -c /etc/supervisor/conf.d/supervisord.conf & exec "$@" \ No newline at end of file diff --git a/docker/supervisord.conf b/docker/supervisord.conf index 57041c7..2cffd3d 100644 --- a/docker/supervisord.conf +++ b/docker/supervisord.conf @@ -8,15 +8,22 @@ directory=/var/www/html command=php artisan queue:work --daemon --timeout=3600 --tries=1 --queue=default autostart=true autorestart=true -priority=1 stdout_logfile=/var/www/logs/queue.log stderr_logfile=/var/www/logs/queue_error.log +stopsignal=INT [program:scheduler] directory=/var/www/html -command=sh -c "while true; do php artisan schedule:run; sleep 60; done" +command=/bin/sh -c "while true; do php artisan schedule:run; sleep 60; done" autostart=true autorestart=true -priority=2 stdout_logfile=/var/www/logs/schedule.log stderr_logfile=/var/www/logs/schedule_error.log + +[program:git-worker] +command=/bin/sh /entrypoint.git.sh +autostart=true +autorestart=true +stdout_logfile=/var/www/logs/git-worker.log +stderr_logfile=/var/www/logs/git-worker_error.log +startsecs=0 diff --git a/docker/supervisord.git.conf b/docker/supervisord.git.conf deleted file mode 100644 index c92000a..0000000 --- a/docker/supervisord.git.conf +++ /dev/null @@ -1,12 +0,0 @@ -[supervisord] -nodaemon=true -logfile=/var/log/supervisord.log -pidfile=/var/run/supervisord.pid - -[program:git-worker] -command=/entrypoint.sh -autostart=true -autorestart=true -startsecs=0 -stdout_logfile=/var/log/git-worker.log -stderr_logfile=/var/log/git-worker.err.log \ No newline at end of file