Docker Multi-Stage Builds
Compile in a fat image, ship a tiny runtime image.
- multi-stage
- slim image
- COPY from
- build target
- small docker image
Docker Multi-Stage Builds
Compile in a fat image, ship a tiny runtime image.
Commands
| Command | What it does |
|---|---|
FROM node:22 AS build | First stage: full toolchain |
RUN npm ci && npm run build | Compile inside the build stage |
FROM node:22-alpine AS prod | Second stage: minimal runtime |
COPY --from=build /app/dist ./dist | Copy only build output |
docker build --target build -t app:debug . | Build just the debug stage |
Example
FROM node:22 AS build
WORKDIR /app
COPY . .
RUN npm ci && npm run build
FROM node:22-alpine AS prod
WORKDIR /app
COPY --from=build /app/dist ./dist
CMD ["node", "dist/server.js"]
FAQ
How much smaller do images get?
Typically 5-10x: toolchains and caches stay in the build stage, only dist plus prod deps ship.
Can stages share files?
Yes via COPY --from=<stage>; stages can also inherit (FROM base AS ...) to share setup steps.
자주 묻는 질문
How much smaller do images get?
Typically 5-10x: toolchains and caches stay in the build stage, only dist plus prod deps ship.
Can stages share files?
Yes via COPY --from=<stage>; stages can also inherit (FROM base AS ...) to share setup steps.
Docker
Docker Install and Setup
Verify the Docker engine, configure context, and check versions.
Docker Images
Pull, list, tag, inspect, and remove images confidently.
Docker Containers
Run, list, stop, and clean up containers in daily work.
Docker Run Options
The flags that matter: detach, ports, env, volumes, limits.