Skip to content
Open
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
27 changes: 26 additions & 1 deletion docker-rollout
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,33 @@ scale() {

main() {
# shellcheck disable=SC2086 # COMPOSE_FILES and ENV_FILES must be unquoted to allow multiple files
if [ -z "$($COMPOSE_COMMAND $COMPOSE_FILES $ENV_FILES ps --quiet "$SERVICE")" ]; then
ALL_CONTAINER_IDS=$($COMPOSE_COMMAND $COMPOSE_FILES $ENV_FILES ps -a --quiet "$SERVICE")
# shellcheck disable=SC2086 # COMPOSE_FILES and ENV_FILES must be unquoted to allow multiple files
RUNNING_CONTAINER_IDS=$($COMPOSE_COMMAND $COMPOSE_FILES $ENV_FILES ps --quiet "$SERVICE")

# Run the original script when no containers exist
if [ -z "$ALL_CONTAINER_IDS" ]; then
echo "==> Service '$SERVICE' is not running. Starting the service."
# shellcheck disable=SC2086 # COMPOSE_FILES and ENV_FILES must be unquoted to allow multiple files
$COMPOSE_COMMAND $COMPOSE_FILES $ENV_FILES up --detach --no-recreate "$SERVICE"
exit 0
fi

# Identify stopped replicas (present in ps -a but not in ps --quiet)
STOPPED_CONTAINER_IDS=""
for id in $ALL_CONTAINER_IDS; do
if ! echo "$RUNNING_CONTAINER_IDS" | grep -qx "$id"; then
STOPPED_CONTAINER_IDS="$STOPPED_CONTAINER_IDS $id"
fi
done

# Remove only the stopped replicas and let compose create fresh ones; running containers
# are preserved via --no-recreate so the zero-downtime contract isn't broken if config drifted.
if [ -n "$STOPPED_CONTAINER_IDS" ]; then
echo "==> Service '$SERVICE' has stopped containers:$STOPPED_CONTAINER_IDS. Replacing only those."
# shellcheck disable=SC2086 # DOCKER_ARGS and STOPPED_CONTAINER_IDS must be unquoted to allow multiple arguments
docker $DOCKER_ARGS rm $STOPPED_CONTAINER_IDS
# shellcheck disable=SC2086 # COMPOSE_FILES and ENV_FILES must be unquoted to allow multiple files
$COMPOSE_COMMAND $COMPOSE_FILES $ENV_FILES up --detach --no-recreate "$SERVICE"
exit 0
fi
Expand Down