From 01fcd690e205453a6d9001661a359458c5abdee3 Mon Sep 17 00:00:00 2001 From: Michael Schiemer Date: Fri, 31 Oct 2025 23:37:29 +0100 Subject: [PATCH] feat: Shared code volume for nginx and app containers - Add app-code volume to both nginx and app containers - App container clones code once into shared volume - Nginx container uses shared volume (read-only) and waits for code - Update entrypoint.sh to handle storage-preserving git clone - Fix nginx configuration to use app:9000 for PHP-FPM This eliminates duplicate code cloning and ensures both containers always use the same code version from the shared volume. --- .../stacks/application/docker-compose.yml | 2 ++ docker/entrypoint.sh | 21 ++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/deployment/stacks/application/docker-compose.yml b/deployment/stacks/application/docker-compose.yml index d47f2730..a960ec87 100644 --- a/deployment/stacks/application/docker-compose.yml +++ b/deployment/stacks/application/docker-compose.yml @@ -13,6 +13,7 @@ services: - APP_ENV=${APP_ENV:-production} - APP_DEBUG=${APP_DEBUG:-false} - APP_URL=${APP_URL:-https://michaelschiemer.de} + - APP_KEY=${APP_KEY:-} # Git Repository (optional - if set, container will clone/pull code on start) - GIT_REPOSITORY_URL=${GIT_REPOSITORY_URL:-} - GIT_BRANCH=${GIT_BRANCH:-main} @@ -39,6 +40,7 @@ services: - QUEUE_DRIVER=redis - QUEUE_CONNECTION=default volumes: + - app-code:/var/www/html - app-storage:/var/www/html/storage - app-logs:/var/www/html/storage/logs - /etc/timezone:/etc/timezone:ro diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index 885929b2..77ac5be7 100755 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -49,15 +49,26 @@ if [ -n "$GIT_REPOSITORY_URL" ]; then echo "📥 Cloning repository from $GIT_REPOSITORY_URL (branch: $GIT_BRANCH)..." # Remove existing files if they exist (from image build) + # But preserve storage directory if it's a volume mount if [ "$(ls -A $GIT_TARGET_DIR 2>/dev/null)" ]; then - echo "🗑️ Cleaning existing files..." - rm -rf "$GIT_TARGET_DIR"/* "$GIT_TARGET_DIR"/.* 2>/dev/null || true + echo "🗑️ Cleaning existing files (preserving storage)..." + find "$GIT_TARGET_DIR" -mindepth 1 -maxdepth 1 ! -name "storage" -exec rm -rf {} \; 2>/dev/null || true fi - # Clone repository - git clone --branch "$GIT_BRANCH" --depth 1 "$GIT_URL_WITH_AUTH" "$GIT_TARGET_DIR" || { + # Clone into temporary directory first, then move contents (preserving storage) + TEMP_CLONE="${GIT_TARGET_DIR}.tmp" + rm -rf "$TEMP_CLONE" 2>/dev/null || true + if git clone --branch "$GIT_BRANCH" --depth 1 "$GIT_URL_WITH_AUTH" "$TEMP_CLONE"; then + # Remove only files/dirs that are not storage (which might be a volume mount) + find "$GIT_TARGET_DIR" -mindepth 1 -maxdepth 1 ! -name "storage" -exec rm -rf {} \; 2>/dev/null || true + # Move contents from temp directory to target (preserving storage) + find "$TEMP_CLONE" -mindepth 1 -maxdepth 1 ! -name "." ! -name ".." -exec mv {} "$GIT_TARGET_DIR/" \; 2>/dev/null || true + rm -rf "$TEMP_CLONE" 2>/dev/null || true + echo "✅ Repository cloned successfully" + else echo "❌ Git clone failed. Falling back to image contents." - } + rm -rf "$TEMP_CLONE" 2>/dev/null || true + fi else echo "🔄 Pulling latest changes from $GIT_BRANCH..." cd "$GIT_TARGET_DIR"