BM: Baca dalam Bahasa Melayu
World D2: Recipes
Multi-stage builds
You will learn: to cook in one stage and pack only the finished dish.
Checked with Docker Engine 29 on 31 Oct 2026
The problem
Your Kedai Kancil page is built with Node. Your one-stage image is 244MB, for one HTML page! What takes the space?
Pick one answer.
Watch
Your laptop~/kedai
- No open hatches
Lunchboxes (containers)
- No containers yet
Frozen packs (images)
- nginx:1.30-alpine8 layers · 93.5MB
- node:24-alpine4 layers · 244MB
0 containers (0 running), 2 images, 0 lockers.
A multi-stage build: Cook in one stage, pack in another. Only the finished dish goes in the final image. has two kitchens. The build stage cooks with Node. The last stage starts fresh from nginx. COPY --from=build carries over only the dish: dist/.
Try it
Put this two-stage recipe in order.
First line at the top. Put these in order. Use the arrows, or drag.
- FROM node:24-alpine AS build
- COPY --from=build /src/dist/ /usr/share/nginx/html/
- FROM nginx:1.30-alpine
- RUN npm run build
- RUN npm ci
Hint
Each stage starts with its own FROM. Cook before you serve.Try it
Build only the cooking stage as kedai:build. Then build the full recipe as kedai:1.0. Compare their sizes.
Your laptop~/kedai
- No open hatches
Lunchboxes (containers)
- No containers yet
Frozen packs (images)
- nginx:1.30-alpine8 layers · 93.5MB
- node:24-alpine4 layers · 244MB
0 containers (0 running), 2 images, 0 lockers.
Hint
--target build stops after the stage called build.Predict
Is npm inside kedai:1.0?
Pick one answer.
Check
Your laptop~/kedai
- No open hatches
Lunchboxes (containers)
- No containers yet
Frozen packs (images)
- nginx:1.30-alpine8 layers · 93.5MB
- node:24-alpine4 layers · 244MB
0 containers (0 running), 2 images, 0 lockers.
which npm finds nothing in kedai:1.0. In the build stage, npm is there. Diners never see the kitchen.
You did it!
You can now cook in one stage and ship only the dish.
Go deeper (optional)
Stages without a name are called by number: COPY --from=0. Names are clearer. COPY --from can also copy from any image, like --from=nginx:1.30-alpine. Fewer tools in the final image also means fewer security holes.
Real mission
docker images
0 of 3 steps done · Your ticks are saved.
Bahasa Melayu
Laman Kedai Kancil anda dibina dengan Node. Image satu peringkat anda 244MB, untuk satu laman HTML! Apa yang ambil ruang itu?
Binaan berbilang peringkat ada dua dapur. Peringkat build memasak dengan Node. Peringkat akhir mula baru dari nginx. COPY --from=build bawa hidangan sahaja: dist/.
Susun resepi dua peringkat ini.
Bina peringkat memasak sahaja sebagai kedai:build. Kemudian bina resepi penuh sebagai kedai:1.0. Bandingkan saiznya.
--target build berhenti selepas peringkat bernama build.Adakah npm di dalam kedai:1.0?
which npm tak jumpa apa-apa dalam kedai:1.0. Di peringkat build, npm ada. Pelanggan tak pernah nampak dapur.
Kini anda boleh masak di satu peringkat dan hantar hidangan sahaja.
You finished this before. Practice is always welcome!
Official docs: Docker docs: Multi-stage builds · Docker docs: Multi-stage builds (concepts)
You've learned for 25 minutes. A short break helps your brain.
Sprint done! Great focus. Stretch for a minute?