144 lines
4.0 KiB
Bash
144 lines
4.0 KiB
Bash
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
# Simple setup script to bootstrap the Dockerized dev stack for this project.
|
|
# - Builds and starts containers (php, nginx, mariadb, mailpit via override)
|
|
# - Installs composer dependencies
|
|
# - Ensures APP_SECRET is set (generates if empty)
|
|
# - Creates and migrates the database
|
|
# - Installs/imports JS dependencies
|
|
# - Prints helpful info on success
|
|
|
|
# Usage:
|
|
# ./docker/setup.sh # full setup
|
|
# ./docker/setup.sh --no-build # skip image rebuild
|
|
# ./docker/setup.sh --down # stop and remove containers (down)
|
|
# ./docker/setup.sh --recreate # force recreate containers
|
|
|
|
ROOT_DIR=$(cd "$(dirname "$0")"/.. && pwd)
|
|
DOCKER_DIR="$ROOT_DIR/docker"
|
|
# Determine the docker compose command (V2 'docker compose' or V1 'docker-compose')
|
|
if docker compose version >/dev/null 2>&1; then
|
|
DOCKER_COMPOSE="docker compose"
|
|
elif command -v docker-compose >/dev/null 2>&1; then
|
|
DOCKER_COMPOSE="docker-compose"
|
|
else
|
|
echo "Error: Neither 'docker compose' nor 'docker-compose' was found. Please install Docker Compose." >&2
|
|
exit 1
|
|
fi
|
|
|
|
# Helper to run docker compose from the project root
|
|
dc() { (cd "$ROOT_DIR" && $DOCKER_COMPOSE "$@"); }
|
|
|
|
REBUILD=1
|
|
RECREATE=0
|
|
DOWN_ONLY=0
|
|
|
|
for arg in "$@"; do
|
|
case "$arg" in
|
|
--no-build) REBUILD=0 ;;
|
|
--recreate) RECREATE=1 ;;
|
|
--down) DOWN_ONLY=1 ;;
|
|
*) echo "Unknown option: $arg" >&2; exit 1 ;;
|
|
esac
|
|
done
|
|
|
|
need() { command -v "$1" >/dev/null 2>&1 || { echo "Error: '$1' is required but not installed." >&2; exit 1; }; }
|
|
|
|
need docker
|
|
|
|
if [ "$DOWN_ONLY" -eq 1 ]; then
|
|
dc down
|
|
exit 0
|
|
fi
|
|
|
|
BUILD_ARGS=()
|
|
if [ "$REBUILD" -eq 1 ]; then
|
|
BUILD_ARGS+=("--build")
|
|
fi
|
|
if [ "$RECREATE" -eq 1 ]; then
|
|
BUILD_ARGS+=("--force-recreate")
|
|
fi
|
|
|
|
# Start stack
|
|
dc up -d "${BUILD_ARGS[@]}"
|
|
|
|
# Helper to run commands in php container
|
|
pexec() { dc exec -T php "$@"; }
|
|
|
|
# Wait for database to be healthy (mariadb/mysql)
|
|
printf "Waiting for database to be healthy..."
|
|
# Use docker inspect health status
|
|
DB_HEALTH=""
|
|
for i in {1..60}; do
|
|
DB_ID=$(dc ps -q database 2>/dev/null || true)
|
|
if [ -n "$DB_ID" ]; then
|
|
DB_HEALTH=$(docker inspect -f '{{.State.Health.Status}}' "$DB_ID" 2>/dev/null || true)
|
|
fi
|
|
if [ "$DB_HEALTH" = "healthy" ]; then
|
|
echo " OK"
|
|
break
|
|
fi
|
|
printf "."
|
|
sleep 2
|
|
if [ "$i" -eq 60 ]; then
|
|
echo -e "\nWarning: database health check not healthy yet, continuing anyway."
|
|
fi
|
|
done
|
|
|
|
# Ensure composer is available and install dependencies
|
|
pexec composer install --no-interaction
|
|
|
|
# Ensure APP_SECRET is set
|
|
if grep -q '^APP_SECRET=$' "$ROOT_DIR/.env" 2>/dev/null; then
|
|
echo "Generating APP_SECRET in .env.local..."
|
|
mkdir -p "$ROOT_DIR"
|
|
SECRET=$(openssl rand -hex 16)
|
|
# Write to .env.local so we don't commit it
|
|
if [ ! -f "$ROOT_DIR/.env.local" ]; then
|
|
printf "APP_SECRET=%s\n" "$SECRET" > "$ROOT_DIR/.env.local"
|
|
elif ! grep -q '^APP_SECRET=' "$ROOT_DIR/.env.local"; then
|
|
printf "APP_SECRET=%s\n" "$SECRET" >> "$ROOT_DIR/.env.local"
|
|
fi
|
|
fi
|
|
|
|
# Prepare DB
|
|
echo "Creating database if it doesn't exist..."
|
|
pexec php bin/console doctrine:database:create --if-not-exists
|
|
echo "Running migrations..."
|
|
pexec php bin/console doctrine:migrations:migrate -n
|
|
|
|
# Import JS deps (Importmap/Asset Mapper)
|
|
if [ -f "$ROOT_DIR/importmap.php" ]; then
|
|
pexec php bin/console importmap:install || true
|
|
fi
|
|
|
|
# Build assets if using Webpack Encore
|
|
if [ -f "$ROOT_DIR/package.json" ]; then
|
|
echo "Installing npm dependencies..."
|
|
pexec npm install
|
|
echo "Building assets..."
|
|
pexec npm run build
|
|
fi
|
|
|
|
APP_URL=http://localhost:8080
|
|
MAILPIT_URL=http://localhost:8025
|
|
|
|
cat <<EOT
|
|
|
|
Setup complete!
|
|
|
|
Open the app: $APP_URL
|
|
Mailpit (dev): $MAILPIT_URL
|
|
|
|
Common commands:
|
|
(cd "$ROOT_DIR" && $DOCKER_COMPOSE logs -f nginx)
|
|
(cd "$ROOT_DIR" && $DOCKER_COMPOSE logs -f php)
|
|
(cd "$ROOT_DIR" && $DOCKER_COMPOSE logs -f php-worker)
|
|
(cd "$ROOT_DIR" && $DOCKER_COMPOSE exec php bash)
|
|
(cd "$ROOT_DIR" && $DOCKER_COMPOSE exec php npm run watch)
|
|
(cd "$ROOT_DIR" && $DOCKER_COMPOSE down)
|
|
|
|
You can re-run this script any time. Use --no-build to skip rebuilding images.
|
|
EOT
|