-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathDockerfile
72 lines (52 loc) · 1.88 KB
/
Dockerfile
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
71
72
ARG PNPM_VERSION=9.15.5
ARG NODE_VERSION=20.13.1-alpine3.19
FROM node:${NODE_VERSION} AS build
# Use a more specific working directory
ENV HOME=/opt/app
# Create non-root user early in build stage
RUN addgroup -g 101 app && adduser -u 100 -D -G app -s /bin/false app
# Set ownership and permissions
WORKDIR $HOME
RUN chown app:app $HOME
# Install pnpm globally before switching to non-root user
USER root
RUN npm install -g pnpm@${PNPM_VERSION}
# Switch to non-root user for remaining operations
USER app
# Add package files with specific ownership
COPY --chown=app:app package.json pnpm-lock.yaml ./
# Install dependencies
RUN pnpm install --frozen-lockfile --ignore-scripts
# Copy application code
COPY --chown=app:app . .
# Build application
RUN pnpm build:docker
# Clean up development dependencies
RUN pnpm install --prod --frozen-lockfile --ignore-scripts && \
rm -rf .pnpm-store
# Production image
FROM node:${NODE_VERSION}
# Update system and install security packages
RUN apk update && \
apk upgrade --no-cache && \
apk add --no-cache dumb-init tini && \
rm -rf /var/cache/apk/*
# Create non-root user
RUN addgroup -g 101 app && adduser -u 100 -D -G app -s /bin/false app
# Set up application directory
WORKDIR /opt/app
# Copy only necessary files from build stage
COPY --chown=app:app --from=build /opt/app/dist ./dist
COPY --chown=app:app --from=build /opt/app/node_modules ./node_modules
COPY --chown=app:app --from=build /opt/app/package.json ./
# Set secure defaults
USER app
ENV NODE_ENV=production \
NODE_OPTIONS="--max-old-space-size=2048 --max-http-header-size=8192"
# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:3000/health || exit 1
EXPOSE 3000
# Use tini as init system
ENTRYPOINT ["/sbin/tini", "--"]
CMD ["node", "--enable-source-maps", "dist/index.js"]