From b4f3e2ba7aff7c5eeeccaf2dca728b590391f2b3 Mon Sep 17 00:00:00 2001 From: Sergio Nicolas Lavaise Date: Fri, 24 Apr 2026 13:50:05 -0300 Subject: [PATCH] Fix Docker build and entrypoint script --- Dockerfile | 5 ++-- docker-entrypoint.sh | 56 ++++++++++---------------------------------- 2 files changed, 14 insertions(+), 47 deletions(-) diff --git a/Dockerfile b/Dockerfile index 14c1f20..d5c490d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -37,7 +37,7 @@ COPY server/ server/ COPY nginx.conf /etc/nginx/http.d/default.conf # Create directories -RUN mkdir -p /run/nginx +RUN mkdir -p /run/nginx /run/mysqld /var/lib/mysql # Copy entrypoint script COPY docker-entrypoint.sh /docker-entrypoint.sh @@ -53,5 +53,4 @@ ENV DB_NAME=hospital ENV ADMIN_DNI=00000000 ENV ADMIN_PASSWORD=Admin123! -ENTRYPOINT ["/docker-entrypoint.sh"] -CMD ["nginx.sh", "node", "server/api-mariadb.js"] \ No newline at end of file +ENTRYPOINT ["/docker-entrypoint.sh"] \ No newline at end of file diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index eb4f693..67be71b 100644 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -2,11 +2,9 @@ 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 @@ -16,17 +14,13 @@ for i in {1..60}; do sleep 1 done -# Create database if it doesn't exist -echo "Creating database if not exists..." DB_NAME="${DB_NAME:-hospital}" +echo "Creating database if not exists..." 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 @@ -34,24 +28,12 @@ if [ -n "$DB_PASSWORD" ] && [ "$DB_PASSWORD" != "" ]; then 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 - -# Create initial admin user if not exists ADMIN_DNI="${ADMIN_DNI:-00000000}" ADMIN_PASSWORD="${ADMIN_PASSWORD:-Admin123!}" @@ -63,43 +45,29 @@ if [ -n "$TABLE_EXISTS" ]; then if [ "$USER_COUNT" = "0" ]; then echo "Creating initial admin user..." - - # Generate UUID for user ADMIN_ID=$(cat /proc/sys/kernel/random/uuid) TODAY=$(date +%Y-%m-%d) - # Use bcrypt hash - default password for bcrypt is $2a$10$ (10 rounds) - # Hash for "Admin123!" - this is a pre-computed hash, admin should change it - BCRYPT_HASH='$2a$10$8kOhKfxFzN9YxJxGfJ5EwONz1JQJNKJqk1JqNKJqNKJqNKJqNKJqu' + # Hash password using bcryptjs via node + HASH=$(node -e "const bcrypt = require('bcryptjs'); console.log(bcrypt.hashSync('$ADMIN_PASSWORD', 10));") - # Insert with a temp hash that won't work - we'll display the credentials to set manually mysql -h localhost -u "$DB_USER" -p"$DB_PASSWORD" "$DB_NAME" -e " INSERT INTO usuarios (id, apellido, nombre, dni, fechaNacimiento, email, rol, passwordHash, fechaCreacion) - VALUES ('$ADMIN_ID', 'Administrador', 'Sistema', '$ADMIN_DNI', '$TODAY', 'admin@hospital.local', 'admin', '', '$TODAY'); + VALUES ('$ADMIN_ID', 'Administrador', 'Sistema', '$ADMIN_DNI', '$TODAY', 'admin@hospital.local', 'admin', '$HASH', '$TODAY'); " echo "==========================================" - echo "ADMIN CREDENTIALS (change password on first login):" + echo "ADMIN CREDENTIALS:" echo "DNI: $ADMIN_DNI" echo "Password: $ADMIN_PASSWORD" echo "==========================================" - - # Now update with correct bcrypt hash - node -e " - const bcrypt = require('bcryptjs'); - const hash = bcrypt.hashSync('$ADMIN_PASSWORD', 10); - console.log(hash); - " > /tmp/hash.txt 2>/dev/null || true - - HASH=$(cat /tmp/hash.txt 2>/dev/null || echo "") - if [ -n "$HASH" ]; then - mysql -h localhost -u "$DB_USER" -p"$DB_PASSWORD" "$DB_NAME" -e "UPDATE usuarios SET passwordHash='$HASH' WHERE dni='$ADMIN_DNI';" - echo "Admin user created with password hash." - fi - else - echo "Admin user already exists, skipping creation." fi fi -# Run the command passed to the container -exec "$@" \ No newline at end of file +echo "=== Initialisation complete ===" + +# Start nginx in background +nginx & + +# Run the backend +exec node server/api-mariadb.js \ No newline at end of file