#!/bin/bash # Function to check if a command exists command_exists() { command -v "$1" >/dev/null 2>&1 } # Determine if we use podman or docker if command_exists podman; then CONTAINER_CMD="podman" VOLUME_FLAG=":Z" echo "Using Podman for container management." elif command_exists docker; then CONTAINER_CMD="docker" VOLUME_FLAG="" echo "Using Docker for container management." else echo "Error: Neither Podman nor Docker found. Please install one of them first." exit 1 fi # Stop and remove container if it exists echo "Checking for existing container..." if $CONTAINER_CMD ps -a --format '{{.Names}}' | grep -q "^fastapi-domains$"; then echo "Stopping and removing existing fastapi-domains container..." $CONTAINER_CMD stop fastapi-domains $CONTAINER_CMD rm fastapi-domains fi # Create volume if it doesn't exist echo "Creating volume for persistent data storage..." $CONTAINER_CMD volume create domain-data # Build the container image echo "Building container image..." $CONTAINER_CMD build -t fastapi-domains:latest . # Run the container echo "Starting container..." $CONTAINER_CMD run --name fastapi-domains \ -p 8000:8000 \ -v domain-data:/home/appuser/app/data${VOLUME_FLAG} \ -e DB_DIR=/home/appuser/app/data \ --security-opt no-new-privileges:true \ --read-only \ --tmpfs /tmp \ --cap-drop ALL \ --user 1000:1000 \ -d fastapi-domains:latest # Check if container started successfully if [ $? -eq 0 ]; then echo "Container started successfully!" echo "Application is available at: http://localhost:8000" echo "" echo "Container logs:" $CONTAINER_CMD logs fastapi-domains else echo "Failed to start container." exit 1 fi