diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..6aee601 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,19 @@ +node_modules +npm-debug.log +.env.local +.env.development.local +.env.test.local +.env.production.local +.git +.gitignore +README.md +LICENSE +.vscode +.nyc_output +coverage +.coverage +dist +build +*.log +.DS_Store +Thumbs.db \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 81eddda..5844c1e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,28 +1,77 @@ -FROM node:20-alpine +# Build stage +FROM node:20-alpine AS builder -# Install build dependencies for node-gyp -RUN apk add --no-cache python3 make g++ +# Install build dependencies for node-gyp and native modules +RUN apk add --no-cache \ + python3 \ + py3-pip \ + make \ + g++ \ + sqlite-dev \ + pkgconfig # Set environment variable for node-gyp to find Python ENV PYTHON=/usr/bin/python3 +# Install distutils for Python (required by node-gyp) +RUN pip3 install --break-system-packages setuptools + # Create working directory WORKDIR /opt/app # Copy dependency files COPY package*.json ./ COPY babel.config.js tsconfig.json ./ -COPY .env ./ -# Install dependencies -RUN npm install +# Configure npm for better Docker builds +RUN npm config set unsafe-perm true + +# Install all dependencies (including dev dependencies for build) +RUN npm ci -# Copy source +# Copy source code COPY src ./src -# Build project +# Build the project RUN npm run build +# Runtime stage +FROM node:20-alpine AS runtime + +# Install runtime dependencies only +RUN apk add --no-cache \ + python3 \ + py3-pip \ + sqlite + +# Set environment variable for node-gyp to find Python +ENV PYTHON=/usr/bin/python3 + +# Install distutils for Python (required by some runtime native modules) +RUN pip3 install --break-system-packages setuptools + +# Create working directory +WORKDIR /opt/app + +# Copy package files +COPY package*.json ./ + +# Configure npm +RUN npm config set unsafe-perm true + +# Install only production dependencies +RUN npm ci --only=production && npm cache clean --force + +# Copy built application from builder stage +COPY --from=builder /opt/app/dist ./dist + +# Copy other necessary files +COPY src/views ./src/views +COPY src/public ./src/public + +# Copy .env file if it exists +COPY .env* ./ + EXPOSE 3000 CMD ["npm", "start"] diff --git a/package.json b/package.json index 3c49198..a3def09 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "start": "node dist/index.js", "build": "tsc", "dev": "nodemon --exec ts-node src/index.ts", - "test": "tsc --noEmit && jest" + "test": "tsc --noEmit && jest", + "rebuild": "npm rebuild" }, "author": "Emmo00", "license": "MIT",