selfhosting/run-docker-compose.sh

51 lines
1.3 KiB
Bash
Executable file

#!/bin/bash
# examples:
# ./run-docker-compose.sh --env-file ./.env up -d
# ./run-docker-compose.sh --env-file ./.env down
# Exit on errors
set -e
# Ensure arguments are provided
if [ $# -eq 0 ]; then
echo "Usage: $0 <docker compose arguments>"
exit 1
fi
# Store the user-provided arguments
ARGS="$@"
CONFIG_FILE=".env"
# Check if config file exists
if [ ! -f "$CONFIG_FILE" ]; then
echo "Error: Config file '$CONFIG_FILE' not found"
exit 1
fi
# Extract DEPLOY variable from .env file
DEPLOY=$(grep '^DEPLOY=' "$CONFIG_FILE" | cut -d '=' -f 2-)
if [ -z "$DEPLOY" ]; then
echo "Error: DEPLOY variable not set in $CONFIG_FILE"
exit 1
fi
# Find all directories containing docker-compose*.yml files
DIRS=$(find . -type f -name 'docker-compose*.yml' -exec dirname {} \; | sort -u)
# Iterate through the filtered directories
for dir in $DIRS; do
# Extract directory name (e.g., "01-blog" from "./01-blog")
dir_name=$(basename "$dir")
# Check if directory name is in DEPLOY list
if echo "$DEPLOY" | grep -qw "$dir_name"; then
compose_file=$(find "$dir" -maxdepth 1 -type f -name 'docker-compose*.yml' | head -1)
echo "Running: docker compose --env-file .env -f $compose_file $ARGS"
docker compose --env-file .env -f "$compose_file" $ARGS
fi
done