|
1 |
| -# syntax=docker/dockerfile:1 |
| 1 | +# Use a multi-stage build for efficiency |
| 2 | +FROM node:20 AS builder |
2 | 3 |
|
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 |
| -ARG NODE_VERSION=20 |
10 |
| - |
11 |
| -################################################################################ |
12 |
| -# Use node image for base image for all stages. |
13 |
| -FROM node:${NODE_VERSION}-alpine as base |
14 |
| - |
15 |
| -# Set working directory for all build stages. |
16 | 4 | WORKDIR /app
|
17 | 5 |
|
| 6 | +COPY package*.json ./ |
18 | 7 |
|
19 |
| -################################################################################ |
20 |
| -# Create a stage for installing production dependecies. |
21 |
| -FROM base as deps |
22 |
| - |
23 |
| -# Download dependencies as a separate step to take advantage of Docker's caching. |
24 |
| -# Leverage a cache mount to /root/.npm to speed up subsequent builds. |
25 |
| -# Leverage bind mounts to package.json and package-lock.json to avoid having to copy them |
26 |
| -# into this layer. |
27 |
| -RUN --mount=type=bind,source=package.json,target=package.json \ |
28 |
| - --mount=type=bind,source=package-lock.json,target=package-lock.json \ |
29 |
| - --mount=type=cache,target=/root/.npm \ |
30 |
| - npm ci --omit=dev |
| 8 | +RUN npm ci |
31 | 9 |
|
32 |
| -################################################################################ |
33 |
| -# Create a stage for building the application. |
34 |
| -FROM deps as build |
35 |
| - |
36 |
| -# Download additional development dependencies before building, as some projects require |
37 |
| -# "devDependencies" to be installed to build. If you don't need this, remove this step. |
38 |
| -RUN --mount=type=bind,source=package.json,target=package.json \ |
39 |
| - --mount=type=bind,source=package-lock.json,target=package-lock.json \ |
40 |
| - --mount=type=cache,target=/root/.npm \ |
41 |
| - npm ci |
42 |
| - |
43 |
| -# Copy the rest of the source files into the image. |
44 | 10 | COPY . .
|
45 |
| -# Run the build script. |
46 |
| -RUN npm run build |
47 | 11 |
|
48 |
| -################################################################################ |
49 |
| -# Create a new stage to run the application with minimal runtime dependencies |
50 |
| -# where the necessary files are copied from the build stage. |
51 |
| -FROM base as final |
| 12 | +# Build the application |
| 13 | +RUN npm run build |
52 | 14 |
|
53 |
| -# Use production node environment by default. |
54 |
| -ENV NODE_ENV production |
| 15 | +# Production stage |
| 16 | +FROM node:20 |
55 | 17 |
|
56 |
| -# Run the application as a non-root user. |
57 |
| -USER node |
| 18 | +WORKDIR /app |
58 | 19 |
|
59 |
| -# Copy package.json so that package manager commands can be used. |
60 |
| -COPY package.json . |
| 20 | +COPY --from=builder /app/dist ./dist |
| 21 | +COPY package*.json ./ |
61 | 22 |
|
62 |
| -# Copy the production dependencies from the deps stage and also |
63 |
| -# the built application from the build stage into the image. |
64 |
| -COPY --from=deps /app/node_modules ./node_modules |
65 |
| -COPY --from=build /app/dist ./dist |
| 23 | +RUN npm ci --omit=dev |
66 | 24 |
|
| 25 | +# We want jq and curl in the final image, but we don't need the support files |
| 26 | +RUN apt-get update && \ |
| 27 | + apt-get install -y jq curl tini && \ |
| 28 | + apt-get clean && \ |
| 29 | + rm -rf /usr/share/doc /usr/share/man /usr/share/zsh |
67 | 30 |
|
68 |
| -# Expose the port that the application listens on. |
69 | 31 | EXPOSE 3000
|
70 | 32 |
|
71 |
| -# Run the application. |
72 | 33 | ENTRYPOINT ["/usr/bin/tini", "--", "node", "dist/apps/api/main.js"]
|
0 commit comments