1- FROM node:20-alpine AS base
1+ # syntax=docker/dockerfile:1
22
3- WORKDIR /app
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/
46
5- RUN npm i -g pnpm
7+ # Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7
68
7- COPY package*.json pnpm-lock.yaml* ./
9+ ARG NODE_VERSION=22.18.0
10+ ARG PNPM_VERSION=10.11.0
811
9- RUN pnpm install
12+ # ###############################################################################
13+ # Use node image for base image for all stages.
14+ FROM node:${NODE_VERSION}-alpine as base
1015
16+ # Set working directory for all build stages.
17+ WORKDIR /usr/src/app
18+
19+ # Install pnpm.
20+ RUN --mount=type=cache,target=/root/.npm \
21+ npm install -g pnpm@${PNPM_VERSION}
22+
23+ # ###############################################################################
24+ # Create a stage for installing production dependecies.
25+ FROM base as deps
26+
27+ # Download dependencies as a separate step to take advantage of Docker's caching.
28+ # Leverage a cache mount to /root/.local/share/pnpm/store to speed up subsequent builds.
29+ # Leverage bind mounts to package.json and pnpm-lock.yaml to avoid having to copy them
30+ # into this layer.
31+ RUN --mount=type=bind,source=package.json,target=package.json \
32+ --mount=type=bind,source=pnpm-lock.yaml,target=pnpm-lock.yaml \
33+ --mount=type=cache,target=/root/.local/share/pnpm/store \
34+ pnpm install --prod --frozen-lockfile
35+
36+ # ###############################################################################
37+ # Create a stage for building the application.
38+ FROM deps as build
39+
40+ # Download additional development dependencies before building, as some projects require
41+ # "devDependencies" to be installed to build. If you don't need this, remove this step.
42+ RUN --mount=type=bind,source=package.json,target=package.json \
43+ --mount=type=bind,source=pnpm-lock.yaml,target=pnpm-lock.yaml \
44+ --mount=type=cache,target=/root/.local/share/pnpm/store \
45+ pnpm install --frozen-lockfile
46+
47+ # Copy the rest of the source files into the image.
1148COPY . .
49+ # Run the build script.
50+ RUN pnpm run build
51+
52+ # ###############################################################################
53+ # Create a new stage to run the application with minimal runtime dependencies
54+ # where the necessary files are copied from the build stage.
55+ FROM base as final
56+
57+ # Use production node environment by default.
58+ ENV NODE_ENV production
59+
60+ # Run the application as a non-root user.
61+ USER node
62+
63+ # Copy package.json so that package manager commands can be used.
64+ COPY package.json .
65+
66+ # Copy the production dependencies from the deps stage and also
67+ # the built application from the build stage into the image.
68+ COPY --from=deps /usr/src/app/node_modules ./node_modules
69+ COPY --from=build /usr/src/app/dist ./dist
1270
13- RUN npm run build
1471
72+ # Expose the port that the application listens on.
1573EXPOSE 3000
1674
17- CMD ["npm" , "start" ]
75+ # Run the application.
76+ CMD ["node" , "dist/app.js" ]
0 commit comments