From bd145607b0f01030d530b99520e3acf9e7ea8dbf Mon Sep 17 00:00:00 2001 From: root Date: Sun, 5 Apr 2026 20:28:31 +0200 Subject: [PATCH] Auto-persist APP_KEY in storage volume across redeploys - Key saved to storage/app/.app_key (persistent volume) - On redeploy: restored automatically from volume - On first deploy: generated and saved - No manual Dokploy env var needed --- docker/php/entrypoint.sh | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/docker/php/entrypoint.sh b/docker/php/entrypoint.sh index 0fa6e78..e34c869 100755 --- a/docker/php/entrypoint.sh +++ b/docker/php/entrypoint.sh @@ -59,21 +59,26 @@ fi echo "[✓] .env file ready." # ----------------------------------------------- -# 2. Application key +# 2. Application key (persisted in storage volume) # ----------------------------------------------- -if grep -q "^APP_KEY=$" .env; then +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 + echo "[✓] Application key restored from storage." +elif grep -q "^APP_KEY=$" .env; then + # No stored key and .env has empty key: generate new one echo "[*] Generating application key..." php artisan key:generate --ansi - echo "" - echo "╔══════════════════════════════════════════════════════╗" - echo "║ IMPORTANT: Save this APP_KEY in Dokploy env vars! ║" - echo "║ Otherwise encrypted data will be lost on redeploy. ║" - echo "╚══════════════════════════════════════════════════════╝" - echo "" - echo " APP_KEY=$(grep '^APP_KEY=' .env | cut -d= -f2-)" - echo "" + # Save to persistent volume + grep '^APP_KEY=' .env | cut -d= -f2- > "$KEY_FILE" + echo "[✓] Application key saved to persistent storage." else - echo "[✓] Application key already set." + # .env already has a key (from env var override or .env.example), persist it + grep '^APP_KEY=' .env | cut -d= -f2- > "$KEY_FILE" + echo "[✓] Application key already set, saved to persistent storage." fi # -----------------------------------------------