#!/bin/bash # CentOS 环境部署 HTTPS 版本的 Docker Compose 脚本 # 功能:清除之前的容器并重新部署 set -e # Exit on error echo "==========================================" echo "Starting HTTPS deployment..." echo "==========================================" # Get script directory SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" cd "$SCRIPT_DIR" # Detect docker compose command (new version: "docker compose" or old version: "docker-compose") if docker compose version >/dev/null 2>&1; then DOCKER_COMPOSE="docker compose" elif docker-compose version >/dev/null 2>&1; then DOCKER_COMPOSE="docker-compose" else echo "Error: Neither 'docker compose' nor 'docker-compose' command found" echo "Please install Docker Compose" exit 1 fi echo "Using: $DOCKER_COMPOSE" # Define container names MARKETING_CONTAINER="marketing-site-https" NGINX_CONTAINER="nginx-https" NETWORK_NAME="marketing-network" echo "" echo "Step 1: Stopping and removing existing containers..." echo "----------------------------------------" # Stop containers if they exist if [ "$(docker ps -a -q -f name=$MARKETING_CONTAINER)" ]; then echo "Stopping container: $MARKETING_CONTAINER" docker stop $MARKETING_CONTAINER || true echo "Removing container: $MARKETING_CONTAINER" docker rm $MARKETING_CONTAINER || true else echo "Container $MARKETING_CONTAINER does not exist, skipping" fi if [ "$(docker ps -a -q -f name=$NGINX_CONTAINER)" ]; then echo "Stopping container: $NGINX_CONTAINER" docker stop $NGINX_CONTAINER || true echo "Removing container: $NGINX_CONTAINER" docker rm $NGINX_CONTAINER || true else echo "Container $NGINX_CONTAINER does not exist, skipping" fi echo "" echo "Step 2: Cleaning up Docker network (if exists)..." echo "----------------------------------------" # Remove network if it exists and is not in use if [ "$(docker network ls -q -f name=$NETWORK_NAME)" ]; then echo "Attempting to remove network: $NETWORK_NAME" docker network rm $NETWORK_NAME 2>/dev/null || echo "Network $NETWORK_NAME may still be in use, skipping removal" else echo "Network $NETWORK_NAME does not exist, skipping" fi echo "" echo "Step 3: Cleaning up Docker images and build cache..." echo "----------------------------------------" # Remove dangling images (untagged images) before build echo "Removing dangling images..." docker image prune -f || true # Get project name from directory name or docker-compose PROJECT_NAME=$(basename "$SCRIPT_DIR" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]//g') if [ -z "$PROJECT_NAME" ] || [ "$PROJECT_NAME" = "site" ]; then PROJECT_NAME="site" fi # Try to remove old images with same name to avoid creating dangling images # Docker Compose creates images with format: _ echo "Removing old images: ${PROJECT_NAME}_marketing-site" docker images --format "{{.Repository}}:{{.Tag}}" | grep -E "^${PROJECT_NAME}_marketing-site" | while read image; do if [ ! -z "$image" ]; then echo "Removing old image: $image" docker rmi "$image" 2>/dev/null || true fi done || true # Remove build cache to ensure fresh build echo "Removing build cache..." docker builder prune -f || true echo "" echo "Step 4: Starting services with docker-compose (no cache)..." echo "----------------------------------------" # Start services with docker-compose (will automatically create network and volumes) # Use --no-cache to ensure fresh build without using cached layers $DOCKER_COMPOSE -f docker-compose-https.yml build --no-cache $DOCKER_COMPOSE -f docker-compose-https.yml up -d echo "" echo "Step 4.5: Cleaning up dangling images after build..." echo "----------------------------------------" # Remove dangling images again after build (new images may have replaced old ones) echo "Removing dangling images created during build..." docker image prune -f || true echo "" echo "Step 5: Checking container status..." echo "----------------------------------------" # Wait a few seconds for containers to start sleep 3 # Display container status docker ps -a | grep -E "($MARKETING_CONTAINER|$NGINX_CONTAINER)" || true echo "" echo "==========================================" echo "Deployment completed!" echo "==========================================" echo "" echo "View logs command:" echo " $DOCKER_COMPOSE -f docker-compose-https.yml logs -f" echo "" echo "Stop services command:" echo " $DOCKER_COMPOSE -f docker-compose-https.yml down" echo ""