-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
70 lines (49 loc) · 1.83 KB
/
Dockerfile
File metadata and controls
70 lines (49 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Use Node.js LTS version
FROM node:22-alpine AS base
# Install security updates, dumb-init for proper signal handling, and lnav for log analysis
RUN apk add --no-cache dumb-init lnav && \
apk upgrade
# Enable corepack and set Yarn version
RUN corepack enable && corepack prepare yarn@4.4.0 --activate
# Create non-root user early for security
RUN addgroup -g 1001 -S nodejs && \
adduser -S nestjs -u 1001 -G nodejs
# Set working directory
WORKDIR /app
# Change ownership of working directory
RUN chown -R nestjs:nodejs /app
# Switch to non-root user for dependency installation
USER nestjs
# Copy package files with proper ownership
COPY --chown=nestjs:nodejs package.json yarn.lock .yarnrc.yml ./
# Install dependencies with cache optimization
RUN yarn install --immutable --check-cache
# === Build stage ===
FROM base AS builder
# Copy source code
COPY --chown=nestjs:nodejs . .
# Build the application
RUN yarn build
# Remove development dependencies to reduce image size
RUN yarn workspaces focus --production
# === Production stage ===
FROM base AS production
# Copy built application and production dependencies from builder stage
COPY --from=builder --chown=nestjs:nodejs /app/dist ./dist
COPY --from=builder --chown=nestjs:nodejs /app/node_modules ./node_modules
COPY --from=builder --chown=nestjs:nodejs /app/package.json ./
# Create logs directory with proper permissions
RUN mkdir -p logs && chown -R nestjs:nodejs logs
# Create additional directories that the app might need
RUN mkdir -p tmp uploads && chown -R nestjs:nodejs tmp uploads
# Switch to non-root user
USER nestjs
# Set environment variables
ENV NODE_ENV=production
ENV PORT=3001
# Expose port (will use PORT env var, defaulting to 3001)
EXPOSE $PORT
# Use dumb-init to handle signals properly
ENTRYPOINT ["dumb-init", "--"]
# Start the application
CMD ["node", "dist/main"]