BM: Baca dalam Bahasa Melayu
World D2: Recipes
Kek lapis: layers and cache
You will learn: why the order of your recipe lines makes builds fast or slow.
Checked with Docker Engine 29 on 31 Oct 2026
The problem
You change one word in server.js. The build runs npm ci again, and you wait two minutes. Why?
Pick one answer.
Watch
Your laptop~/diary
- No open hatches
Lunchboxes (containers)
- No containers yet
Frozen packs (images)
- nginx:1.30-alpine8 layers · 93.5MB
0 containers (0 running), 1 images, 0 lockers.
Each recipe line bakes one layer: One layer of kek lapis. Change one, and every layer above it is baked again., like kek lapis. Docker keeps old layers. Change style.css: only its layer and the layers on top bake again. The rest say CACHED.
Try it
This recipe bakes npm ci again after every code change. Move the lines so it stays CACHED when server.js changes.
Each recipe line adds one cake layer. Put the lines in a good order, then build.
Dockerfile (the recipe)
FROM node:24-alpineWORKDIR /appCOPY . .COPY package.json package-lock.json ./RUN npm ci --omit=devCMD ["node", "server.js"]
You built it once. Next, you change server.js.
The cake (your image)
CMD ["node", "server.js"]RUN npm ci --omit=devCOPY package.json package-lock.json ./COPY . .WORKDIR /appFROM node:24-alpine
Hint
Copy the package files and install first. Copy the code last.Predict
Now the order is good. You add a package, so package.json changes. What bakes again?
Pick one answer.
Try it
This recipe is in good order. Its layers sit in the build cache: Layers kept from the last bake. A step that did not change is reused, not baked again.. Change the code, build api:1.1, and count the CACHED lines.
Your laptop~/api
- No open hatches
Lunchboxes (containers)
- No containers yet
Frozen packs (images)
- api:1.08 layers · 244MB
- node:24-alpine4 layers · 244MB
0 containers (0 running), 2 images, 0 lockers.
Hint
>> adds a line to the end of a file.Check
Good cache habit? Swipe right for yes, left for “needs work”.
Swipe right for good, left for needs work. Or use the buttons.
Copy the package files and install before copying the code
You did it!
You can now order a recipe so rebuilds stay fast.
Go deeper (optional)
docker history api:1.1 lists the layers, newest on top, with their sizes. Lines like ENV and CMD show 0B: notes, not layers. Deleting a file in a later layer only hides it. The image does not shrink.
Real mission
docker history api:1.1
0 of 3 steps done · Your ticks are saved.
Bahasa Melayu
Anda tukar satu perkataan dalam server.js. Build jalankan npm ci semula, dan anda tunggu dua minit. Kenapa?
Setiap baris resepi bakar satu layer (lapisan), macam kek lapis. Docker simpan layer lama. Tukar style.css: hanya layernya dan layer di atasnya dibakar semula. Yang lain tulis CACHED.
Resepi ini bakar npm ci semula selepas setiap perubahan kod. Alihkan baris supaya ia kekal CACHED bila server.js berubah.
Sekarang susunannya bagus. Anda tambah satu pakej, jadi package.json berubah. Apa yang dibakar semula?
Resepi ini sudah tersusun. Layernya ada dalam cache binaan. Tukar kod, bina api:1.1, dan kira baris CACHED.
>> tambah satu baris di hujung fail.Tabiat cache yang bagus? Leret ke kanan untuk ya, ke kiri untuk “perlu dibaiki”.
Kini anda boleh susun resepi supaya build semula kekal laju.
You finished this before. Practice is always welcome!
Official docs: Docker docs: Understanding image layers · Docker docs: Build cache · Docker docs: Optimize cache usage · docker history
You've learned for 25 minutes. A short break helps your brain.
Sprint done! Great focus. Stretch for a minute?