From 22ac0aa7819c13248012546421dfa52026649e6f Mon Sep 17 00:00:00 2001 From: "Z.p8ibwg4jri" Date: Sun, 5 Apr 2026 19:52:41 +0000 Subject: [PATCH] +++ fix --- docker/php/Dockerfile | 27 ++++++++-- docker/php/entrypoint.sh | 107 ++++++++++++++++++++++++++------------- 2 files changed, 95 insertions(+), 39 deletions(-) diff --git a/docker/php/Dockerfile b/docker/php/Dockerfile index 1c26448..a53099c 100644 --- a/docker/php/Dockerfile +++ b/docker/php/Dockerfile @@ -39,11 +39,30 @@ RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* -# Set working directory -WORKDIR /var/www/html +# Build application in staging area +WORKDIR /app-src -# Copy application code to staging area -COPY . /app-src/ +# Install PHP dependencies first for better Docker layer caching +COPY composer.json composer.lock ./ +RUN composer install --no-interaction --prefer-dist --no-progress --no-scripts + +# Install Node dependencies first for better Docker layer caching +COPY package.json package-lock.json* ./ +RUN if [ -f package-lock.json ]; then \ + npm ci --no-audit --no-fund; \ + else \ + npm install --no-audit --no-fund; \ + fi + +# Copy full source and build frontend assets +COPY . . +RUN mkdir -p bootstrap/cache storage/framework/cache storage/framework/sessions storage/framework/views storage/logs storage/app +RUN if [ ! -f .env ] && [ -f .env.example ]; then cp .env.example .env; fi +RUN composer dump-autoload --optimize --no-interaction +RUN npm run build + +# Runtime working directory +WORKDIR /var/www/html # Copy PHP configuration COPY docker/php/php.ini /usr/local/etc/php/conf.d/custom.ini diff --git a/docker/php/entrypoint.sh b/docker/php/entrypoint.sh index 63fd855..79f8721 100755 --- a/docker/php/entrypoint.sh +++ b/docker/php/entrypoint.sh @@ -1,10 +1,42 @@ #!/bin/bash -set -e +set -Eeuo pipefail echo "=========================================" echo " TerManager2 - Entrypoint" echo "=========================================" +warn() { + echo "[!] $*" +} + +retry() { + local attempts="$1" + local delay="$2" + shift 2 + + local n=1 + until "$@"; do + if [ "$n" -ge "$attempts" ]; then + return 1 + fi + warn "Command failed (attempt ${n}/${attempts}). Retrying in ${delay}s..." + sleep "$delay" + n=$((n + 1)) + done +} + +upsert_env() { + local key="$1" + local value="$2" + local env_file="$3" + + if grep -q "^${key}=" "$env_file"; then + sed -i "s|^${key}=.*|${key}=${value}|" "$env_file" + else + echo "${key}=${value}" >> "$env_file" + fi +} + # ----------------------------------------------- # 0. Sync application code from image to volume # ----------------------------------------------- @@ -20,6 +52,7 @@ fi # ----------------------------------------------- mkdir -p storage/framework/{cache,sessions,views} mkdir -p storage/logs +mkdir -p storage/app mkdir -p bootstrap/cache chown -R www-data:www-data storage bootstrap/cache @@ -33,41 +66,51 @@ fi # Override .env values with Docker environment variables (if set) # This ensures Dokploy env vars take precedence -if [ -n "$APP_KEY" ]; then - sed -i "s|^APP_KEY=.*|APP_KEY=${APP_KEY}|" .env +if [ -n "${APP_KEY:-}" ]; then + upsert_env "APP_KEY" "${APP_KEY}" .env fi -if [ -n "$APP_URL" ]; then - sed -i "s|^APP_URL=.*|APP_URL=${APP_URL}|" .env +if [ -n "${APP_URL:-}" ]; then + upsert_env "APP_URL" "${APP_URL}" .env fi -if [ -n "$DB_HOST" ]; then - sed -i "s|^DB_HOST=.*|DB_HOST=${DB_HOST}|" .env +if [ -n "${DB_HOST:-}" ]; then + upsert_env "DB_HOST" "${DB_HOST}" .env fi -if [ -n "$DB_DATABASE" ]; then - sed -i "s|^DB_DATABASE=.*|DB_DATABASE=${DB_DATABASE}|" .env +if [ -n "${DB_DATABASE:-}" ]; then + upsert_env "DB_DATABASE" "${DB_DATABASE}" .env fi -if [ -n "$DB_USERNAME" ]; then - sed -i "s|^DB_USERNAME=.*|DB_USERNAME=${DB_USERNAME}|" .env +if [ -n "${DB_USERNAME:-}" ]; then + upsert_env "DB_USERNAME" "${DB_USERNAME}" .env fi -if [ -n "$DB_PASSWORD" ]; then - sed -i "s|^DB_PASSWORD=.*|DB_PASSWORD=${DB_PASSWORD}|" .env +if [ -n "${DB_PASSWORD:-}" ]; then + upsert_env "DB_PASSWORD" "${DB_PASSWORD}" .env fi -if [ -n "$REDIS_HOST" ]; then - sed -i "s|^REDIS_HOST=.*|REDIS_HOST=${REDIS_HOST}|" .env +if [ -n "${REDIS_HOST:-}" ]; then + upsert_env "REDIS_HOST" "${REDIS_HOST}" .env fi -if [ -n "$REDIS_PASSWORD" ]; then - sed -i "s|^REDIS_PASSWORD=.*|REDIS_PASSWORD=${REDIS_PASSWORD}|" .env +if [ -n "${REDIS_PASSWORD:-}" ]; then + upsert_env "REDIS_PASSWORD" "${REDIS_PASSWORD}" .env fi echo "[✓] .env file ready." # ----------------------------------------------- -# 2. Application key (persisted in storage volume) +# 2. Composer install (must run before artisan commands) +# ----------------------------------------------- +if [ ! -f vendor/autoload.php ]; then + echo "[*] Installing Composer dependencies..." + retry 3 5 composer install --no-interaction --prefer-dist --optimize-autoloader --no-progress +else + echo "[✓] Composer dependencies already installed." +fi + +# ----------------------------------------------- +# 3. Application key (persisted in storage volume) # ----------------------------------------------- KEY_FILE="/var/www/html/storage/app/.app_key" if [ -f "$KEY_FILE" ]; then # Restore key from persistent volume STORED_KEY=$(cat "$KEY_FILE") - sed -i "s|^APP_KEY=.*|APP_KEY=${STORED_KEY}|" .env + upsert_env "APP_KEY" "$STORED_KEY" .env echo "[✓] Application key restored from storage." elif grep -q "^APP_KEY=$" .env; then # No stored key and .env has empty key: generate new one @@ -82,27 +125,21 @@ else echo "[✓] Application key already set, saved to persistent storage." fi -# ----------------------------------------------- -# 3. Composer install -# ----------------------------------------------- -if [ ! -f vendor/autoload.php ]; then - echo "[*] Installing Composer dependencies..." - composer install --no-interaction --optimize-autoloader -else - echo "[✓] Composer dependencies already installed." -fi - # ----------------------------------------------- # 4. NPM install & build assets # ----------------------------------------------- if [ ! -d node_modules ]; then echo "[*] Installing NPM dependencies..." - npm ci + if [ -f package-lock.json ]; then + retry 3 5 npm ci --no-audit --no-fund + else + retry 3 5 npm install --no-audit --no-fund + fi else echo "[✓] NPM dependencies already installed." fi -if [ ! -d public/build ]; then +if [ ! -f public/build/manifest.json ]; then echo "[*] Building frontend assets..." npm run build else @@ -123,15 +160,15 @@ fi # 7. Run migrations # ----------------------------------------------- echo "[*] Running database migrations..." -php artisan migrate --force +retry 10 3 php artisan migrate --force # ----------------------------------------------- # 8. Cache config/routes/views # ----------------------------------------------- echo "[*] Caching configuration..." -php artisan config:cache -php artisan route:cache -php artisan view:cache +php artisan config:cache || warn "config:cache failed; continuing startup." +php artisan route:cache || warn "route:cache failed; continuing startup." +php artisan view:cache || warn "view:cache failed; continuing startup." echo "=========================================" echo " TerManager2 - Ready!"