#!/bin/bash # Soil Water Balance Model Docker Deployment Script set -e IMAGE_NAME="soil-water-model" CONTAINER_NAME="soil-water-model-container" PORT="8000" echo "๐Ÿš€ Starting Soil Water Balance Model deployment..." # Function to check if container is running check_container() { if docker ps -q -f name="$CONTAINER_NAME" | grep -q .; then echo "โš ๏ธ Container '$CONTAINER_NAME' is already running." read -p "Do you want to stop and restart it? (y/N): " -r if [[ $REPLY =~ ^[Yy]$ ]]; then echo "๐Ÿ›‘ Stopping existing container..." docker stop "$CONTAINER_NAME" docker rm "$CONTAINER_NAME" else echo "โŒ Deployment cancelled." exit 1 fi fi } # Function to build image build_image() { echo "๐Ÿ”จ Building Docker image..." docker build -t "$IMAGE_NAME" . echo "โœ… Image built successfully!" } # Function to run container run_container() { echo "๐Ÿƒ Running container..." docker run -d \ --name "$CONTAINER_NAME" \ -p "$PORT:8000" \ --restart unless-stopped \ "$IMAGE_NAME" echo "โœ… Container started successfully!" } # Function to show status show_status() { echo "๐Ÿ“Š Container Status:" docker ps -f name="$CONTAINER_NAME" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" # Wait a moment for container to start sleep 3 # Test health endpoint echo "๐Ÿ” Testing health endpoint..." if curl -f http://localhost:"$PORT"/health > /dev/null 2>&1; then echo "โœ… Health check passed!" echo "๐ŸŒ API is available at: http://localhost:$PORT" echo "๐Ÿ“š API documentation: http://localhost:$PORT/docs" else echo "โŒ Health check failed. Container might still be starting..." echo " Please wait a moment and check the logs:" echo " docker logs $CONTAINER_NAME" fi } # Main deployment process main() { check_container build_image run_container show_status echo "" echo "๐ŸŽ‰ Deployment completed successfully!" echo "" echo "Useful commands:" echo " View logs: docker logs $CONTAINER_NAME" echo " Stop container: docker stop $CONTAINER_NAME" echo " Restart container: docker restart $CONTAINER_NAME" echo " Remove container: docker rm $CONTAINER_NAME" } # Handle command line arguments case "${1:-}" in "build") build_image ;; "run") check_container run_container show_status ;; "stop") echo "๐Ÿ›‘ Stopping container..." docker stop "$CONTAINER_NAME" 2>/dev/null || echo "Container not running" ;; "restart") echo "๐Ÿ”„ Restarting container..." docker restart "$CONTAINER_NAME" 2>/dev/null || (echo "Container not running, starting new one..." && run_container) show_status ;; "logs") echo "๐Ÿ“‹ Showing container logs..." docker logs -f "$CONTAINER_NAME" ;; "status") show_status ;; "clean") echo "๐Ÿงน Cleaning up..." docker stop "$CONTAINER_NAME" 2>/dev/null || true docker rm "$CONTAINER_NAME" 2>/dev/null || true docker rmi "$IMAGE_NAME" 2>/dev/null || true echo "โœ… Cleanup completed!" ;; *) main ;; esac