|
| 1 | +# syntax=docker/dockerfile:1 |
| 2 | +ARG NODE_VERSION=20.16.0 |
| 3 | + |
| 4 | +################################################################################ |
| 5 | +# Use node image for base image for all stages. |
| 6 | +FROM node:${NODE_VERSION}-alpine AS base |
| 7 | + |
| 8 | +# Set working directory for all build stages. |
| 9 | +WORKDIR /usr/src/app |
| 10 | + |
| 11 | + |
| 12 | +################################################################################ |
| 13 | +# Create a stage for installing production dependecies. |
| 14 | +FROM base AS deps |
| 15 | + |
| 16 | +# Download dependencies as a separate step to take advantage of Docker's caching. |
| 17 | +# Leverage a cache mount to /root/.npm to speed up subsequent builds. |
| 18 | +# Leverage bind mounts to package.json and package-lock.json to avoid having to copy them |
| 19 | +# into this layer. |
| 20 | +RUN --mount=type=bind,source=package.json,target=package.json \ |
| 21 | + # workaround for npm optional dependencies bug: https://github.com/npm/cli/issues/4828 |
| 22 | + # --mount=type=bind,source=package-lock.json,target=package-lock.json \ |
| 23 | + --mount=type=cache,target=/root/.npm \ |
| 24 | + npm i --omit=dev |
| 25 | + |
| 26 | +################################################################################ |
| 27 | +# Create a stage for building the application. |
| 28 | +FROM deps AS dev-deps |
| 29 | + |
| 30 | + |
| 31 | +# Download additional development dependencies before building, as some projects require |
| 32 | +# "devDependencies" to be installed to build. If you don't need this, remove this step. |
| 33 | +RUN --mount=type=bind,source=package.json,target=package.json \ |
| 34 | + # workaround for npm optional dependencies bug: https://github.com/npm/cli/issues/4828 |
| 35 | + # --mount=type=bind,source=package-lock.json,target=package-lock.json \ |
| 36 | + --mount=type=cache,target=/root/.npm \ |
| 37 | + npm i |
| 38 | + |
| 39 | +FROM dev-deps AS build |
| 40 | +# Copy the rest of the source files into the image. |
| 41 | +COPY . . |
| 42 | +# Run the build script. |
| 43 | +RUN npx tsc -b |
| 44 | +RUN npx vite build |
| 45 | + |
| 46 | +################################################################################ |
| 47 | +# Create a new stage to run the application with minimal runtime dependencies |
| 48 | +# where the necessary files are copied from the build stage. |
| 49 | +FROM base AS final |
| 50 | + |
| 51 | +# Use production node environment by default. |
| 52 | +ENV NODE_ENV=production |
| 53 | + |
| 54 | +# Run the application as a non-root user. |
| 55 | +USER node |
| 56 | + |
| 57 | +# Copy package.json so that package manager commands can be used. |
| 58 | +COPY package.json . |
| 59 | + |
| 60 | +# Copy the production dependencies from the deps stage and also |
| 61 | +# the built application from the build stage into the image. |
| 62 | +COPY --from=deps /usr/src/app/node_modules ./node_modules |
| 63 | +COPY --from=build /usr/src/app/server ./ |
| 64 | +COPY --from=build /usr/src/app/dist ./dist |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +# Expose the port that the application listens on. |
| 69 | +EXPOSE 8080 |
| 70 | + |
| 71 | +# Run the application. |
| 72 | +CMD [ "node", "server.js" ] |
0 commit comments