|
1 | 1 | # syntax=docker/dockerfile:1 |
2 | 2 |
|
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/ |
6 | | - |
7 | | -# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7 |
8 | | - |
9 | 3 | ARG NODE_VERSION=22 |
10 | 4 | ARG PNPM_VERSION=9 |
11 | 5 |
|
12 | | -FROM node:${NODE_VERSION}-alpine |
| 6 | +FROM node:${NODE_VERSION}-slim AS node-base |
13 | 7 |
|
14 | | -# Use production node environment by default. |
15 | | -ENV NODE_ENV production |
| 8 | +#### |
| 9 | +FROM node-base AS deps |
16 | 10 |
|
17 | | -# Install pnpm. |
18 | 11 | RUN --mount=type=cache,target=/root/.npm \ |
19 | 12 | npm install -g pnpm@${PNPM_VERSION} |
20 | 13 |
|
21 | | -WORKDIR /usr/src/app |
| 14 | +WORKDIR /app/ |
22 | 15 |
|
23 | | -# Download dependencies as a separate step to take advantage of Docker's caching. |
24 | | -# Leverage a cache mount to /root/.local/share/pnpm/store to speed up subsequent builds. |
25 | | -# Leverage a bind mounts to package.json and pnpm-lock.yaml to avoid having to copy them into |
26 | | -# into this layer. |
27 | 16 | RUN --mount=type=bind,source=package.json,target=package.json \ |
28 | 17 | --mount=type=bind,source=pnpm-lock.yaml,target=pnpm-lock.yaml \ |
29 | 18 | --mount=type=cache,target=/root/.local/share/pnpm/store \ |
30 | | - pnpm install --prod --frozen-lockfile |
| 19 | + pnpm install --frozen-lockfile |
31 | 20 |
|
32 | | -# Run the application as a non-root user. |
33 | | -USER node |
| 21 | +#### |
| 22 | +FROM node-base |
34 | 23 |
|
35 | | -# Copy the rest of the source files into the image. |
36 | | -COPY . . |
| 24 | +WORKDIR /app/ |
37 | 25 |
|
38 | | -# Expose the port that the application listens on. |
39 | | -EXPOSE 3000 |
| 26 | +RUN chown node:node ./ |
| 27 | + |
| 28 | +COPY --chown=node:node ./ ./ |
| 29 | +COPY --chown=node:node --from=deps /app/node_modules/ ./node_modules/ |
| 30 | + |
| 31 | +USER node |
40 | 32 |
|
41 | | -# Run the application. |
42 | | -CMD npm exec |
| 33 | +CMD ["npm", "run", "dev"] |
0 commit comments