Enhance deploy script to remove conflicting containers and streamline docker compose commands
Deploy ru-vps / deploy (push) Successful in 15s

This commit is contained in:
Dmitry
2026-05-27 22:18:18 +03:00
parent abeaf1ee45
commit 9b3fba0052
2 changed files with 16 additions and 10 deletions
+15 -10
View File
@@ -61,19 +61,24 @@ for dir in $dirs; do
docker compose config --quiet
# Remove containers whose container_name matches this stack but belong to a different project.
# docker compose down won't touch them because they carry no matching project labels.
while IFS= read -r cname; do
[ -z "$cname" ] && continue
existing="$(docker ps -aq --filter "name=^/${cname}$" 2>/dev/null || true)"
[ -z "$existing" ] && continue
owner="$(docker inspect --format '{{index .Config.Labels "com.docker.compose.project.working_dir"}}' "$existing" 2>/dev/null || true)"
if [ "$owner" != "$(pwd)" ]; then
echo "==> Removing conflicting container $cname (owner: ${owner:-none})"
docker rm -f "$existing"
fi
done < <(docker compose config 2>/dev/null | awk '/^[[:space:]]+container_name:/{gsub(/["'"'"']/, "", $2); print $2}')
if grep -qE '^[[:space:]]*build:' docker-compose.yml; then
docker compose up -d --build --remove-orphans || {
echo "==> up --build failed, tearing down and retrying"
docker compose down --remove-orphans 2>/dev/null || true
docker compose up -d --build --remove-orphans
}
docker compose up -d --build --remove-orphans
else
docker compose pull
docker compose up -d --remove-orphans || {
echo "==> up failed, tearing down and retrying"
docker compose down --remove-orphans 2>/dev/null || true
docker compose up -d --remove-orphans
}
docker compose up -d --remove-orphans
fi
# Health check: give containers a moment, then fail on crashed services