# Build stage FROM node:18-alpine AS builder WORKDIR /app # Copy package files first (better caching) COPY package*.json ./ COPY yarn.lock* ./ # Install all dependencies (including dev for build) # This layer will be cached unless package.json changes RUN yarn install --frozen-lockfile && yarn cache clean # Copy source code (separate from dependencies) COPY tsconfig*.json ./ COPY eslint.config.mjs ./ COPY scripts ./scripts COPY src ./src COPY public ./public # Build the application with minification RUN yarn build:prod # Clean dev dependencies RUN yarn install --production --frozen-lockfile # Production stage FROM node:18-alpine AS production # Install ffmpeg from Alpine packages (architecture-aware) RUN apk update && apk add --no-cache ffmpeg # Set ffmpeg paths ENV FFMPEG_PATH=/usr/bin/ffmpeg ENV FFPROBE_PATH=/usr/bin/ffprobe ENV NODE_ENV=production WORKDIR /app # Copy built application and dependencies COPY --from=builder /app/dist ./dist COPY --from=builder /app/node_modules ./node_modules COPY --from=builder /app/public ./public COPY --from=builder /app/package*.json ./ # Create necessary directories RUN mkdir -p downloads database # Create non-root user RUN addgroup -g 1001 -S nodejs RUN adduser -S quixotic -u 1001 # Change ownership of app directory RUN chown -R quixotic:nodejs /app USER quixotic # Expose port EXPOSE 3000 # Health check HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \ CMD node -e "require('http').get('http://localhost:3000/health', (res) => { process.exit(res.statusCode === 200 ? 0 : 1) })" # Start the application CMD ["node", "dist/server.js"]