|
1 | | -# ---------- Base builder (installs deps) ---------- |
2 | | -FROM node:20-alpine AS deps |
3 | | -WORKDIR /app |
| 1 | +# syntax=docker/dockerfile:1 |
4 | 2 |
|
5 | | -# If you have a package-lock.json, keep npm. If you use yarn/pnpm, adjust. |
6 | | -COPY package*.json ./ |
7 | | -RUN npm ci --omit=dev |
| 3 | +# Comments are provided throughout this file to help you get started. |
| 4 | +# If you need more help, visit the Dockerfile reference guide at |
| 5 | +# https://docs.docker.com/go/dockerfile-reference/ |
8 | 6 |
|
9 | | -# ---------- Runtime image ---------- |
10 | | -FROM node:20-alpine AS runner |
11 | | -ENV NODE_ENV=production |
12 | | -WORKDIR /app |
| 7 | +# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7 |
13 | 8 |
|
14 | | -# Copy node_modules from deps stage |
15 | | -COPY --from=deps /app/node_modules ./node_modules |
| 9 | +ARG NODE_VERSION=22.21.0 |
| 10 | + |
| 11 | +FROM node:${NODE_VERSION}-alpine |
| 12 | + |
| 13 | +# Use production node environment by default. |
| 14 | +ENV NODE_ENV production |
16 | 15 |
|
17 | | -# Copy app source |
18 | | -COPY . . |
19 | 16 |
|
20 | | -# Security: run as non-root user |
21 | | -RUN addgroup -S appgroup && adduser -S appuser -G appgroup |
22 | | -USER appuser |
| 17 | +WORKDIR /usr/src/app |
23 | 18 |
|
24 | | -# Healthcheck (optional but nice). If you have a /health route, use it. |
25 | | -# Replace /health with your actual health endpoint. |
26 | | -HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \ |
27 | | - CMD wget -qO- http://localhost:3001/health || exit 1 |
| 19 | +# Download dependencies as a separate step to take advantage of Docker's caching. |
| 20 | +# Leverage a cache mount to /root/.npm to speed up subsequent builds. |
| 21 | +# Leverage a bind mounts to package.json and package-lock.json to avoid having to copy them into |
| 22 | +# into this layer. |
| 23 | +RUN --mount=type=bind,source=package.json,target=package.json \ |
| 24 | + --mount=type=bind,source=package-lock.json,target=package-lock.json \ |
| 25 | + --mount=type=cache,target=/root/.npm \ |
| 26 | + npm ci --omit=dev |
| 27 | + |
| 28 | +# Run the application as a non-root user. |
| 29 | +USER node |
| 30 | + |
| 31 | +# Copy the rest of the source files into the image. |
| 32 | +COPY . . |
28 | 33 |
|
29 | | -# Expose the app port (matches your .env PORT) |
| 34 | +# Expose the port that the application listens on. |
30 | 35 | EXPOSE 3001 |
31 | 36 |
|
32 | | -# Start command. Change if your service uses a different entry (e.g. dist/index.js). |
33 | | -# Common cases: |
34 | | -# - "node src/server.js" |
35 | | -# - "node index.js" |
36 | | -# - "npm start" (if start script is set properly) |
37 | | -CMD ["npm", "start"] |
| 37 | +# Run the application. |
| 38 | +CMD npm start |
0 commit comments