#!/bin/bash set -e echo "=== Starting MariaDB ===" # Start MariaDB in background mysqld --user=mysql & MYSQL_PID=$! # Wait for MariaDB to be ready echo "Waiting for MariaDB to be ready..." for i in {1..60}; do if mysqladmin ping -h localhost --silent 2>/dev/null; then echo "MariaDB is ready!" break fi sleep 1 done # Create database if it doesn't exist echo "Creating database if not exists..." DB_NAME="${DB_NAME:-hospital}" mysql -h localhost -u root -e "CREATE DATABASE IF NOT EXISTS \`$DB_NAME\`;" # Create user and grant permissions if DB_PASSWORD is set if [ -n "$DB_PASSWORD" ] && [ "$DB_PASSWORD" != "" ]; then echo "Creating user and setting permissions..." DB_USER="${DB_USER:-hospital}" # Check if user exists USER_EXISTS=$(mysql -h localhost -u root -N -e "SELECT COUNT(*) FROM mysql.user WHERE User='$DB_USER' AND Host='%';") if [ "$USER_EXISTS" = "0" ]; then mysql -h localhost -u root -e "CREATE USER '$DB_USER'@'%' IDENTIFIED BY '$DB_PASSWORD';" mysql -h localhost -u root -e "GRANT ALL PRIVILEGES ON $DB_NAME.* TO '$DB_USER'@'%';" mysql -h localhost -u root -e "FLUSH PRIVILEGES;" else echo "User already exists, updating password..." mysql -h localhost -u root -e "ALTER USER '$DB_USER'@'%' IDENTIFIED BY '$DB_PASSWORD';" mysql -h localhost -u root -e "GRANT ALL PRIVILEGES ON $DB_NAME.* TO '$DB_USER'@'%';" mysql -h localhost -u root -e "FLUSH PRIVILEGES;" fi fi echo "=== MariaDB initialization complete ===" # If called with nginx.sh, start nginx if [ "$1" = "nginx.sh" ]; then echo "Starting nginx..." nginx shift exec "$@" fi # Run the command passed to the container exec "$@"