#!/usr/bin/env bash set -euo pipefail # Completely restart ONE project stack (prod or a test/staging copy). # Can be run from any directory. Everything is scoped to the Compose project # name so running this from the test checkout never touches the prod stack. # # ./docker/restart.sh # rebuild-less restart of this checkout's stack # ./docker/restart.sh --prune-all # also run host-wide `docker system/builder prune` # # (old behaviour; skip it when another stack shares the host) DOCKER_DIR=$(cd "$(dirname "$0")" && pwd) ROOT_DIR=$(cd "$DOCKER_DIR/.." && pwd) PRUNE_ALL=0 for arg in "$@"; do case "$arg" in --prune-all) PRUNE_ALL=1 ;; *) echo "Unknown option: $arg" >&2; exit 1 ;; esac done # Read the identifiers from docker/.env (same file Compose uses). STACK_NAME is # the container-name prefix; COMPOSE_PROJECT_NAME is the compose project. read_env() { [ -f "$DOCKER_DIR/.env" ] && grep -E "^$1=" "$DOCKER_DIR/.env" | tail -n1 | cut -d= -f2- | tr -d "\"'" || true; } STACK="$(read_env STACK_NAME)"; STACK="${STACK:-escapepage}" PROJECT="$(read_env COMPOSE_PROJECT_NAME)"; PROJECT="${PROJECT:-$STACK}" echo "Restarting stack: $STACK (compose project: $PROJECT)" echo "Stopping and removing containers..." (cd "$DOCKER_DIR" && docker compose -p "$PROJECT" -f compose.yaml -f compose.override.yaml down -v --remove-orphans) || true # Belt-and-suspenders: drop anything still lingering for THIS stack only. docker rm -f \ "${STACK}-db" "${STACK}-php" "${STACK}-nginx" "${STACK}-mercure" \ "${STACK}-mailer" "${STACK}-php-worker" "${STACK}-php-cron" 2>/dev/null || true for net in $(docker network ls -q --filter "name=^${PROJECT}_" 2>/dev/null); do docker network rm "$net" || true done if [ "$PRUNE_ALL" -eq 1 ]; then echo "Host-wide prune (containers, networks, build cache)..." docker system prune -f || true docker builder prune -af || true else echo "Skipping host-wide prune (pass --prune-all to force it)." fi echo "Setting permissions for var/volumes/db and var directories..." sudo chown -R 1000:1000 "$ROOT_DIR/var/volumes/db" || true sudo chmod -R 777 "$ROOT_DIR/var/volumes/db" || true sudo mkdir -p "$ROOT_DIR/var/cache" "$ROOT_DIR/var/log/cron" "$ROOT_DIR/var/log/php" "$ROOT_DIR/var/sessions" sudo chown -R 1000:1000 "$ROOT_DIR/var" || true sudo chmod -R 777 "$ROOT_DIR/var" || true echo "Running setup script..." "$DOCKER_DIR/setup.sh" --no-build