Loading Depth Study
Preparing your workspace
Docker images are built from layers, and multi-stage builds allow build tools to remain outside the final runtime image.
Each Dockerfile instruction can contribute to an image layer. Good instruction ordering can improve build-cache reuse. Multi-stage builds use separate build and runtime stages, allowing compilers, package managers, and development dependencies to exist only in the build stage. The final image contains only what is needed to run the application. This can reduce image size and attack surface.
A TypeScript Node.js application can compile source code in a build stage and copy only the compiled application and production dependencies into the final image.
FROM node:22-alpine AS build WORKDIR /app COPY package*.json ./ RUN npm ci COPY . . RUN npm run build FROM node:22-alpine AS production WORKDIR /app COPY package*.json ./ RUN npm ci --omit=dev COPY --from=build /app/dist ./dist CMD ["node", "dist/server.js"]
The final production image contains the runtime files without the full development build environment.