From b2f7d45a1f5b7cb3c69f094e27fe9884a402f787 Mon Sep 17 00:00:00 2001 From: Dennis Postma Date: Fri, 7 Feb 2025 02:01:16 +0100 Subject: [PATCH] ? --- Dockerfile | 59 ++++++++++++++++++++++++---------------- docker-start.sh | 71 +++++++++++++++++++++++++++++++++++++++++-------- tsconfig.json | 6 ++++- 3 files changed, 101 insertions(+), 35 deletions(-) diff --git a/Dockerfile b/Dockerfile index cac34cf..49cd303 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,33 +1,46 @@ -# Use the official Node.js 22.4.1 image -FROM node:23.7.0-alpine +# Build stage +FROM node:23.7.0-alpine AS builder +WORKDIR /usr/src/app +COPY package*.json ./ +RUN npm ci +COPY . . +RUN npm run build -# Install Redis, MySQL, and tmux -RUN apk add --no-cache redis mysql mysql-client tmux && \ - mkdir -p /run/mysqld && \ +# Production stage +FROM node:23.7.0-alpine +LABEL maintainer="Your Name" +LABEL version="1.0" +LABEL description="NQ Server Application" + +# Install dependencies with versions +RUN apk add --no-cache \ + redis=7.0.15-r0 \ + mysql=10.11.6-r0 \ + mysql-client=10.11.6-r0 \ + tmux=3.3a-r0 + +# Create non-root user +RUN addgroup -S appgroup && adduser -S appuser -G appgroup + +# Setup MySQL +RUN mkdir -p /run/mysqld && \ chown -R mysql:mysql /run/mysqld && \ mysql_install_db --user=mysql --datadir=/var/lib/mysql -# Set the working directory in the container -WORKDIR /usr/src/ +WORKDIR /usr/src/app +COPY --from=builder /usr/src/app/dist ./dist +COPY --from=builder /usr/src/app/package*.json ./ +COPY docker-start.sh ./dist/start.sh -# Copy package.json and package-lock.json (if available) -COPY package*.json ./ +RUN npm ci --only=production && \ + chmod +x ./dist/start.sh && \ + chown -R appuser:appgroup . -# Install application dependencies -RUN npm install +USER appuser -# Copy the rest of your application code to the container -COPY . . - -# Build the application -RUN npm run build - -# Expose the ports your Node.js application, Redis, and MySQL will listen on EXPOSE 80 6379 3306 -# Copy and make the startup script executable -COPY docker-start.sh /usr/src/dist/start.sh -RUN chmod +x /usr/src/dist/start.sh +HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ + CMD mysqladmin ping -h localhost || exit 1 -# Use the shell script as the entry point -CMD ["/usr/src/dist/start.sh"] \ No newline at end of file +CMD ["./dist/start.sh"] \ No newline at end of file diff --git a/docker-start.sh b/docker-start.sh index 360d9a1..6d9b026 100644 --- a/docker-start.sh +++ b/docker-start.sh @@ -1,24 +1,73 @@ #!/bin/sh +set -e -# Start Redis in the background +# Configuration +MAX_MYSQL_WAIT=60 +APP_NAME="nodeapp" + +# Cleanup function +cleanup() { + echo "Cleaning up..." + tmux kill-session -t $APP_NAME 2>/dev/null || true + redis-cli shutdown || true + mysqladmin -u root shutdown || true + exit 0 +} + +# Setup cleanup trap +trap cleanup SIGTERM SIGINT + +# Start Redis +echo "Starting Redis..." redis-server --daemonize yes +if ! redis-cli ping > /dev/null 2>&1; then + echo "Failed to start Redis" + exit 1 +fi # Start MySQL +echo "Starting MySQL..." mysqld --user=mysql --datadir=/var/lib/mysql & -# Wait for MySQL to be ready +# Wait for MySQL with timeout +echo "Waiting for MySQL to be ready..." +COUNTER=0 while ! mysqladmin ping -h localhost --silent; do - echo "Waiting for MySQL to be ready..." - sleep 2 + if [ $COUNTER -gt $MAX_MYSQL_WAIT ]; then + echo "MySQL failed to start within $MAX_MYSQL_WAIT seconds" + exit 1 + fi + echo "Still waiting..." + sleep 2 + COUNTER=$((COUNTER+2)) done echo "MySQL is ready!" -# Run database migrations -npx mikro-orm migration:up +# Run migrations with error handling +echo "Running database migrations..." +if ! npx mikro-orm migration:up; then + echo "Migration failed" + exit 1 +fi -# Start the Node.js application in a tmux session -tmux new-session -d -s nodeapp "npm run start" -echo "App is running in tmux session. Attach with: tmux attach-session -t nodeapp" +# Start application in tmux +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" -# Keep container running -tail -f /dev/null \ No newline at end of file +# Monitor key processes +while true; do + if ! redis-cli ping > /dev/null 2>&1; then + echo "Redis died" + exit 1 + fi + if ! mysqladmin ping -h localhost --silent; then + echo "MySQL died" + exit 1 + fi + if ! tmux has-session -t $APP_NAME 2>/dev/null; then + echo "Application died" + exit 1 + fi + sleep 30 +done \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 2e13cea..5cd9ed0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,6 +8,10 @@ "moduleDetection": "force", "allowJs": true, "declaration": true, + "noImplicitAny": true, + "resolveJsonModule": true, + "sourceMap": true, + "isolatedModules": true, // Best practices "strict": true, @@ -16,7 +20,7 @@ // Output "outDir": "./dist", - "rootDir": "./", + "rootDir": "./src", "baseUrl": ".", "paths": {