Add Docker with MariaDB support
- Dockerfile now includes MariaDB server - Add docker-entrypoint.sh to initialize DB, create user, and set permissions - docker-compose.yml with configurable environment variables - Nginx config proxies API to backend
This commit is contained in:
+30
-13
@@ -9,31 +9,48 @@ RUN npm ci
|
|||||||
COPY . .
|
COPY . .
|
||||||
RUN npm run build
|
RUN npm run build
|
||||||
|
|
||||||
# Stage 2: Backend + Nginx
|
# Stage 2: Full stack with MariaDB
|
||||||
FROM node:20-alpine
|
FROM node:20-alpine
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
COPY --from=builder /app/dist dist
|
# Install MariaDB and nginx
|
||||||
|
RUN apk add --no-cache mariadb mariadb-client mariadb-server-utils nginx
|
||||||
|
|
||||||
|
# Initialize MariaDB data directory
|
||||||
|
RUN mkdir -p /run/mysqld && \
|
||||||
|
chown -R mysql:mysql /run/mysqld && \
|
||||||
|
chown -R mysql:mysql /var/lib/mysql
|
||||||
|
|
||||||
|
# Copy MariaDB config
|
||||||
|
RUN sed -i 's/\[mysqld\]/[mysqld]\ncharacter-set-server=utf8mb4\ncollation-server=utf8mb4_unicode_ci/' /etc/my.cnf.d/mariadb.cnf || true
|
||||||
|
|
||||||
|
# Copy built frontend
|
||||||
|
COPY --from=builder /app/dist /usr/share/nginx/html
|
||||||
|
|
||||||
|
# Copy server files
|
||||||
COPY server/package*.json server/
|
COPY server/package*.json server/
|
||||||
RUN cd server && npm ci
|
RUN cd server && npm ci
|
||||||
|
|
||||||
COPY server/server.js server/
|
COPY server/ server/
|
||||||
COPY server/db.js server/
|
|
||||||
|
|
||||||
# Install nginx
|
|
||||||
RUN apk add --no-cache nginx && mkdir -p /usr/share/nginx/html
|
|
||||||
|
|
||||||
# Copy nginx config
|
# Copy nginx config
|
||||||
COPY nginx.conf /etc/nginx/http.d/default.conf
|
COPY nginx.conf /etc/nginx/http.d/default.conf
|
||||||
|
|
||||||
# Create nginx directories
|
# Create directories
|
||||||
RUN mkdir -p /run/nginx
|
RUN mkdir -p /run/nginx
|
||||||
|
|
||||||
# Copy built frontend to nginx html directory
|
# Copy entrypoint script
|
||||||
RUN cp -r dist/* /usr/share/nginx/html/
|
COPY docker-entrypoint.sh /docker-entrypoint.sh
|
||||||
|
RUN chmod +x /docker-entrypoint.sh
|
||||||
|
|
||||||
EXPOSE 80 4000
|
EXPOSE 80 4000 3306
|
||||||
|
|
||||||
# Start both nginx and the backend
|
ENV DB_HOST=localhost
|
||||||
CMD nginx && node server/server.js
|
ENV DB_PORT=3306
|
||||||
|
ENV DB_USER=root
|
||||||
|
ENV DB_PASSWORD=
|
||||||
|
ENV DB_NAME=hospital
|
||||||
|
|
||||||
|
ENTRYPOINT ["/docker-entrypoint.sh"]
|
||||||
|
CMD ["nginx.sh", "node", "server/api-mariadb.js"]
|
||||||
+6
-5
@@ -6,9 +6,10 @@ services:
|
|||||||
ports:
|
ports:
|
||||||
- "8077:80"
|
- "8077:80"
|
||||||
- "4000:4000"
|
- "4000:4000"
|
||||||
volumes:
|
environment:
|
||||||
- hospital-data:/app
|
- DB_HOST=localhost
|
||||||
|
- DB_PORT=3306
|
||||||
|
- DB_USER=hospital
|
||||||
|
- DB_PASSWORD=hospital123
|
||||||
|
- DB_NAME=hospital
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
|
||||||
volumes:
|
|
||||||
hospital-data:
|
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
#!/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..30}; 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..."
|
||||||
|
mysql -h localhost -u root -e "CREATE DATABASE IF NOT EXISTS \`${DB_NAME:-hospital}\`;"
|
||||||
|
|
||||||
|
# Create user and grant permissions if DB_PASSWORD is set
|
||||||
|
if [ -n "$DB_PASSWORD" ] && [ "$DB_PASSWORD" != "" ]; then
|
||||||
|
echo "Creating user and setting permissions..."
|
||||||
|
|
||||||
|
# Check if user exists
|
||||||
|
USER_EXISTS=$(mysql -h localhost -u root -N -e "SELECT COUNT(*) FROM mysql.user WHERE User='${DB_USER:-root}' AND Host='%';")
|
||||||
|
|
||||||
|
if [ "$USER_EXISTS" = "0" ]; then
|
||||||
|
mysql -h localhost -u root -e "CREATE USER '${DB_USER:-root}'@'%' IDENTIFIED BY '${DB_PASSWORD}';"
|
||||||
|
mysql -h localhost -u root -e "GRANT ALL PRIVILEGES ON ${DB_NAME:-hospital}.* TO '${DB_USER:-root}'@'%';"
|
||||||
|
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:-root}'@'%' IDENTIFIED BY '${DB_PASSWORD}';"
|
||||||
|
mysql -h localhost -u root -e "GRANT ALL PRIVILEGES ON ${DB_NAME:-hospital}.* TO '${DB_USER:-root}'@'%';"
|
||||||
|
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
|
||||||
|
exec "$@"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Otherwise wait for the process
|
||||||
|
wait
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import * as mariadb from 'mariadb';
|
import * as mariadb from 'mariadb';
|
||||||
import * as bcrypt from 'bcryptjs';
|
import bcrypt from 'bcryptjs';
|
||||||
|
const { compareSync, hashSync } = bcrypt;
|
||||||
|
|
||||||
// UUID generator
|
// UUID generator
|
||||||
export function generateUUID() {
|
export function generateUUID() {
|
||||||
@@ -279,11 +280,11 @@ export async function deleteUsuario(id) {
|
|||||||
export async function verifyPassword(dni, password) {
|
export async function verifyPassword(dni, password) {
|
||||||
const row = await queryOne('SELECT passwordHash FROM usuarios WHERE dni = ?', [dni]);
|
const row = await queryOne('SELECT passwordHash FROM usuarios WHERE dni = ?', [dni]);
|
||||||
if (!row) return false;
|
if (!row) return false;
|
||||||
return bcrypt.compareSync(password, row.passwordHash);
|
return compareSync(password, row.passwordHash);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function hashPassword(password) {
|
export function hashPassword(password) {
|
||||||
return bcrypt.hashSync(password, 10);
|
return hashSync(password, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ========== PACIENTES ==========
|
// ========== PACIENTES ==========
|
||||||
|
|||||||
Reference in New Issue
Block a user