From 20a6c416e35b9723d7c62204c73e3a859f592f2d Mon Sep 17 00:00:00 2001 From: denshooter Date: Sat, 13 Sep 2025 01:24:34 +0200 Subject: [PATCH] Fix container name conflicts in zero-downtime deployment - Use unique timestamp-based container names to avoid conflicts - Clean up existing temporary containers before starting new ones - Generate unique names like 'portfolio-app-temp-1234567890' - Prevents 'container name already in use' errors - Ensures reliable zero-downtime deployments --- .gitea/workflows/ci-cd.yml | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/.gitea/workflows/ci-cd.yml b/.gitea/workflows/ci-cd.yml index 461d15b..3ec275b 100644 --- a/.gitea/workflows/ci-cd.yml +++ b/.gitea/workflows/ci-cd.yml @@ -109,9 +109,18 @@ jobs: if [ "$CURRENT_CONTAINER_RUNNING" = "true" ]; then echo "๐Ÿ”„ Performing rolling update..." - # Start new container with temporary name (no port mapping needed for health check) + # Generate unique container name + TIMESTAMP=$(date +%s) + TEMP_CONTAINER_NAME="portfolio-app-temp-$TIMESTAMP" + echo "๐Ÿ”ง Using temporary container name: $TEMP_CONTAINER_NAME" + + # Clean up any existing temporary containers + echo "๐Ÿงน Cleaning up any existing temporary containers..." + docker rm -f portfolio-app-new portfolio-app-temp-* || true + + # Start new container with unique temporary name (no port mapping needed for health check) docker run -d \ - --name portfolio-app-new \ + --name $TEMP_CONTAINER_NAME \ --restart unless-stopped \ --network portfolio_net \ -e NODE_ENV=${{ vars.NODE_ENV }} \ @@ -134,7 +143,7 @@ jobs: # Health check new container using docker exec for i in {1..20}; do - if docker exec portfolio-app-new curl -f http://localhost:3000/api/health > /dev/null 2>&1; then + if docker exec $TEMP_CONTAINER_NAME curl -f http://localhost:3000/api/health > /dev/null 2>&1; then echo "โœ… New container is healthy!" break fi @@ -150,7 +159,7 @@ jobs: docker rm portfolio-app || true # Rename new container - docker rename portfolio-app-new portfolio-app + docker rename $TEMP_CONTAINER_NAME portfolio-app # Update port mapping docker stop portfolio-app