Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion scripts/check_running_container.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ check_running_container() {
exit 1
fi
echo "🧹 Stopping stale container: $NEW_CONTAINER"
stop_and_clean_container "$NEW_CONTAINER"
if ! stop_and_clean_container "$NEW_CONTAINER"; then
echo "❌ Failed to clean up stale container. Aborting deployment."
exit 1
fi

else
local CONTAINER_NAME="${CONTAINERS[0]}"
Expand Down
5 changes: 4 additions & 1 deletion scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ if ! reload_nginx $NEW_PORT; then
fi

# 5. Stop and remove the previous container
stop_and_clean_container $RUNNING_CONTAINER
if ! stop_and_clean_container "$RUNNING_CONTAINER"; then
echo "❌ Deployment switched successfully, but failed to clean up the previous container."
exit 1
fi

echo "✅ Finish Deploy"
28 changes: 25 additions & 3 deletions scripts/stop_and_clean_container.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
#!/bin/bash

readonly CONTAINER_STOP_TIMEOUT_SECONDS=30
readonly DOCKER_COMMAND_TIMEOUT_SECONDS=45

run_docker_command_with_timeout() {
timeout --kill-after=5s "${DOCKER_COMMAND_TIMEOUT_SECONDS}s" "$@"
}

stop_and_clean_container() {
local CONTAINER_NAME=$1
if [ -z "${CONTAINER_NAME}" ]; then
echo "⚠️ No container name provided to stop and clean."
return 0
fi

echo "🛑 Stopping and removing container: ${CONTAINER_NAME}"
docker compose stop "${CONTAINER_NAME}"
docker compose rm -f "${CONTAINER_NAME}"
docker image prune -a -f
if ! run_docker_command_with_timeout docker compose stop --timeout "${CONTAINER_STOP_TIMEOUT_SECONDS}" "${CONTAINER_NAME}"; then
echo "⚠️ Graceful stop failed or timed out. Force removing container: ${CONTAINER_NAME}"
if ! run_docker_command_with_timeout docker rm -f "${CONTAINER_NAME}"; then
echo "❌ Failed to remove container: ${CONTAINER_NAME}"
return 1
fi
elif ! run_docker_command_with_timeout docker compose rm -f "${CONTAINER_NAME}"; then
echo "⚠️ Container removal failed or timed out. Force removing container: ${CONTAINER_NAME}"
if ! run_docker_command_with_timeout docker rm -f "${CONTAINER_NAME}"; then
echo "❌ Failed to remove container: ${CONTAINER_NAME}"
return 1
fi
fi

if ! run_docker_command_with_timeout docker image prune -a -f; then
echo "⚠️ Image cleanup failed or timed out. Continuing deployment."
fi
}
Loading