From 582c6f58a4f64b782371676e8a23cf266b236f97 Mon Sep 17 00:00:00 2001 From: Wuesteon Date: Tue, 9 Dec 2025 15:59:06 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(ci):=20prevent=20container?= =?UTF-8?q?=20name=20conflict=20in=20staging=20deployment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Remove stale containers before deploying (fixes 'name already in use' error) - Always use --force-recreate flag for consistent deployment behavior - Add troubleshooting docs for container name conflicts --- .github/workflows/cd-staging-tagged.yml | 15 +++++++++------ cicd/DEPLOYMENT.md | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/.github/workflows/cd-staging-tagged.yml b/.github/workflows/cd-staging-tagged.yml index 5efce798b..22d98cc86 100644 --- a/.github/workflows/cd-staging-tagged.yml +++ b/.github/workflows/cd-staging-tagged.yml @@ -361,15 +361,18 @@ jobs: # Service name matches docker-compose service name (with hyphens) SERVICE_NAME="$IMAGE_NAME" + CONTAINER_NAME="${IMAGE_NAME}-staging" - if docker compose ps -a | grep -q "$IMAGE_NAME"; then - echo "Updating existing service: \$SERVICE_NAME" - docker compose up -d --no-deps --force-recreate \$SERVICE_NAME - else - echo "Service \$SERVICE_NAME not found in compose, starting..." - docker compose up -d \$SERVICE_NAME + # Remove any stale container with the same name (prevents "name already in use" error) + if docker ps -a --format '{{.Names}}' | grep -q "^\$CONTAINER_NAME\$"; then + echo "Removing stale container: \$CONTAINER_NAME" + docker rm -f \$CONTAINER_NAME 2>/dev/null || true fi + # Always use --force-recreate to ensure the new image is used + echo "Deploying service: \$SERVICE_NAME" + docker compose up -d --no-deps --force-recreate \$SERVICE_NAME + # Wait for startup sleep 10 docker compose ps \$SERVICE_NAME diff --git a/cicd/DEPLOYMENT.md b/cicd/DEPLOYMENT.md index 0f33ae385..d767cb347 100644 --- a/cicd/DEPLOYMENT.md +++ b/cicd/DEPLOYMENT.md @@ -543,6 +543,20 @@ docker logs chat-backend-staging --tail 200 # - Port conflicts ``` +### Container name conflict error + +If you see: `Error: The container name "/xxx-staging" is already in use` + +```bash +# SSH to server and remove the stale container +ssh deploy@46.224.108.214 +docker rm -f todo-web-staging # Replace with actual container name + +# Then re-run the deployment +``` + +This can happen if a container was created outside of docker-compose or if a previous deployment failed mid-way. + --- ## Managing Tags