122 lines
3.3 KiB
Bash
122 lines
3.3 KiB
Bash
#!/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
|