From 1ed9c2a61f79614801318cfd903e4dff4014e9eb Mon Sep 17 00:00:00 2001 From: Dennis Postma Date: Sat, 8 Feb 2025 02:36:25 +0100 Subject: [PATCH] Updated Dockerfile --- .dockerignore | 17 ++++++++++ Dockerfile | 41 +++-------------------- captain-definition | 4 --- docker-compose.yml | 75 ++++++++++++++++++++++++++++++++++++++++++ start.sh | 81 ---------------------------------------------- 5 files changed, 97 insertions(+), 121 deletions(-) create mode 100644 .dockerignore delete mode 100644 captain-definition create mode 100644 docker-compose.yml delete mode 100644 start.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..e365c96 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,17 @@ +node_modules +npm-debug.log +Dockerfile +.dockerignore +.git +.gitignore +README.md +.env +.env.* +docker-compose* +*.md +coverage +.vscode +.idea +dist +build +temp \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 86fdc57..c505a5e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,44 +1,13 @@ FROM node:23.7.0-alpine -# Install dependencies and setup environment in a single layer -RUN npm install -g npm@11.1.0 && \ - apk add --no-cache \ - redis \ - mariadb \ - mariadb-client \ - tmux \ - mariadb-server-utils \ - mariadb-connector-c && \ - # Create non-root user and groups - addgroup -S appgroup && \ - adduser -S appuser -G appgroup && \ - adduser appuser mysql && \ - # Setup MariaDB directories and permissions - mkdir -p /run/mysqld /var/lib/mysql /var/log/mysql && \ - chown -R mysql:mysql /run/mysqld /var/lib/mysql /var/log/mysql && \ - chmod 755 /run/mysqld /var/lib/mysql /var/log/mysql && \ - mysql_install_db --user=mysql --datadir=/var/lib/mysql && \ - touch /var/log/mysql/error.log && \ - chown mysql:mysql /var/log/mysql/error.log && \ - chmod 644 /var/log/mysql/error.log - WORKDIR /usr/src/app -# Copy application files COPY package*.json ./ -COPY start.sh ./start.sh + +RUN npm ci + COPY . . -# Setup application permissions -RUN npm ci --only=production && \ - chmod +x ./start.sh && \ - chown -R appuser:appgroup . +EXPOSE 4000 -USER appuser - -EXPOSE 80 6379 3306 - -HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ - CMD mariadb-admin ping -h localhost || exit 1 - -CMD ["./start.sh"] \ No newline at end of file +CMD ["node", "run", "start"] \ No newline at end of file diff --git a/captain-definition b/captain-definition deleted file mode 100644 index c939ec0..0000000 --- a/captain-definition +++ /dev/null @@ -1,4 +0,0 @@ -{ - "schemaVersion": 2, - "dockerfilePath" :"./Dockerfile" -} \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..613c432 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,75 @@ +version: '3.8' + +services: + app: + build: + context: . + dockerfile: Dockerfile + ports: + - "${PORT}:${PORT}" + environment: + - ENV=${ENV} + - HOST=${HOST} + - PORT=${PORT} + - JWT_SECRET=${JWT_SECRET} + - CLIENT_URL=${CLIENT_URL} + - REDIS_URL=${REDIS_URL} + - DB_HOST=mariadb + - DB_USER=${DB_USER} + - DB_PASS=${DB_PASS} + - DB_PORT=${DB_PORT} + - DB_NAME=${DB_NAME} + - ALLOW_DIAGONAL_MOVEMENT=${ALLOW_DIAGONAL_MOVEMENT} + - DEFAULT_CHARACTER_ZONE=${DEFAULT_CHARACTER_ZONE} + - DEFAULT_CHARACTER_POS_X=${DEFAULT_CHARACTER_POS_X} + - DEFAULT_CHARACTER_POS_Y=${DEFAULT_CHARACTER_POS_Y} + - SMTP_HOST=${SMTP_HOST} + - SMTP_PORT=${SMTP_PORT} + - SMTP_USER=${SMTP_USER} + - SMTP_PASSWORD=${SMTP_PASSWORD} + volumes: + - app-public:/user/src/app/public + - app-logs:/user/src/app/logs + depends_on: + - mariadb + - redis + restart: unless-stopped + networks: + - app-network + + mariadb: + image: mariadb:10.6 + environment: + - MYSQL_ROOT_PASSWORD=${DB_PASS} + - MYSQL_DATABASE=${DB_NAME} + - MYSQL_USER=${DB_USER} + - MYSQL_PASSWORD=${DB_PASS} + volumes: + - mariadb-data:/var/lib/mysql + ports: + - "${DB_PORT}:3306" + restart: unless-stopped + networks: + - app-network + command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci'] + + redis: + image: redis:7-alpine + command: redis-server --appendonly yes + volumes: + - redis-data:/data + ports: + - "6379:6379" + restart: unless-stopped + networks: + - app-network + +networks: + app-network: + driver: bridge + +volumes: + app-public: + app-logs: + mariadb-data: + redis-data: \ No newline at end of file diff --git a/start.sh b/start.sh deleted file mode 100644 index 9eecb82..0000000 --- a/start.sh +++ /dev/null @@ -1,81 +0,0 @@ -#!/bin/sh -set -eo pipefail - -# Configuration -MAX_MYSQL_WAIT=60 -APP_NAME="nodeapp" -MYSQL_SOCKET="/run/mysqld/mysqld.sock" -MYSQL_PID="/run/mysqld/mysqld.pid" -MYSQL_ERROR_LOG="/var/log/mysql/error.log" - -# Cleanup function -cleanup() { - echo "Shutting down services..." - tmux kill-session -t $APP_NAME 2>/dev/null || true - redis-cli shutdown 2>/dev/null || true - if [ -f "$MYSQL_PID" ]; then - mysqladmin -u root shutdown 2>/dev/null || true - fi - exit 0 -} - -# Error handler -error_handler() { - echo "Error occurred in script at line: $1" - cleanup - exit 1 -} - -trap cleanup SIGTERM SIGINT -trap 'error_handler ${LINENO}' ERR - -# Start Redis -echo "Starting Redis..." -redis-server --daemonize yes -until redis-cli ping > /dev/null 2>&1; do - sleep 1 -done -echo "Redis started successfully" - -# Start MySQL -echo "Starting MySQL..." -mariadbd \ - --datadir=/var/lib/mysql \ - --pid-file=$MYSQL_PID \ - --socket=$MYSQL_SOCKET \ - --log-error=$MYSQL_ERROR_LOG \ - --bind-address=0.0.0.0 & - -# Wait for MySQL -echo "Waiting for MySQL to be ready..." -COUNTER=0 -until mysqladmin ping -h localhost --silent 2>/dev/null || [ $COUNTER -gt $MAX_MYSQL_WAIT ]; do - echo "Still waiting... (${COUNTER}s)" - sleep 2 - COUNTER=$((COUNTER+2)) -done - -if [ $COUNTER -gt $MAX_MYSQL_WAIT ]; then - echo "MySQL failed to start within $MAX_MYSQL_WAIT seconds" - echo "Last few lines of MySQL error log:" - tail -n 20 $MYSQL_ERROR_LOG - exit 1 -fi -echo "MySQL is ready!" - -# Run migrations -echo "Running database migrations..." -npx mikro-orm-esm migration:up - -# Start application -echo "Starting application..." -tmux new-session -d -s $APP_NAME "npm run start" -echo "App is running in tmux session. Attach with: tmux attach-session -t $APP_NAME" - -# Monitor processes -while true; do - sleep 30 - redis-cli ping > /dev/null 2>&1 || { echo "Redis died"; exit 1; } - mysqladmin ping -h localhost --silent || { echo "MySQL died"; exit 1; } - tmux has-session -t $APP_NAME 2>/dev/null || { echo "Application died"; exit 1; } -done \ No newline at end of file