BM: Baca dalam Bahasa Melayu
World D2: Recipes
BuildKit cache mounts and build secrets
You will learn: to lend a key to one build step, and to keep downloads on a shelf between builds.
Checked with Docker Engine 29 on 31 Oct 2026
The problem
Your laptop~/api
- No open hatches
Lunchboxes (containers)
- No containers yet
Frozen packs (images)
- node:24-alpine4 layers · 244MB
0 containers (0 running), 1 images, 0 lockers.
You passed an npm token with ARG. The build even warned you: SecretsUsedInArgOrEnv. Now look at docker history: anyone with the image can read the token!
Watch
Tap the line that borrows the key.
0 of 1 found
A build secret: A key lent to one RUN step only. It never lands in a layer. is a key lent to one RUN step. When the step ends, the key goes back. No layer keeps it.
Try it
Build the safe recipe as api:1.0. Try it plain first and read the error. Then lend the key from token.txt.
Your laptop~/api
- No open hatches
Lunchboxes (containers)
- No containers yet
Frozen packs (images)
- node:24-alpine4 layers · 244MB
0 containers (0 running), 1 images, 0 lockers.
Hint
--secret id=npm_token,src=token.txt lends the file’s text as the secret.Try it
Downloads are slow too. A cache mount: A pantry shelf BuildKit keeps between bakes, for downloads. It is not in the image. is a pantry shelf BuildKit keeps between builds. It is not in the image. Build the RUN line that uses it.
Tap the pieces in the right order.
Predict
You built api:1.0 with the secret. Will docker history api:1.0 show test-token-123?
Pick one answer.
Check
Your laptop~/api
- No open hatches
Lunchboxes (containers)
- No containers yet
Frozen packs (images)
- node:24-alpine4 layers · 244MB
0 containers (0 running), 1 images, 0 lockers.
No token anywhere. The RUN layer only says npm ci. token.txt is in .dockerignore too, so it never reached the kitchen.
Check
Safe? Swipe right for yes, left for “needs work”.
Swipe right for good, left for needs work. Or use the buttons.
Pass the npm token with --build-arg
You did it!
You can now lend secrets safely and keep downloads on a shelf.
Go deeper (optional)
Without env=, the secret is a file at /run/secrets/<id>. Leave out required=true, and a missing secret is simply not there. Secrets do not change the cache: a new token does not bake again. Use test tokens only while practising.
Real mission
RUN --mount=type=secret,id=demo test -f /run/secrets/demodocker build --secret id=demo,src=token.txt -t demo:1.0 .
0 of 4 steps done · Your ticks are saved.
Bahasa Melayu
Anda beri token npm melalui ARG. Build pun sudah beri amaran: SecretsUsedInArgOrEnv. Sekarang tengok docker history: sesiapa yang ada image boleh baca token itu!
Rahsia binaan ialah kunci yang dipinjamkan kepada satu langkah RUN. Bila langkah itu tamat, kunci itu dipulangkan. Tiada layer (lapisan) yang menyimpannya.
Bina resepi selamat ini sebagai api:1.0. Cuba cara biasa dulu dan baca ralatnya. Kemudian pinjamkan kunci dari token.txt.
--secret id=npm_token,src=token.txt pinjamkan isi fail itu sebagai rahsia.Muat turun pun lambat. Cache mount ialah rak dapur yang BuildKit simpan antara build. Ia tiada dalam image. Bina baris RUN yang menggunakannya.
Anda bina api:1.0 dengan rahsia itu. Adakah docker history api:1.0 tunjuk test-token-123?
Tiada token di mana-mana. Layer RUN cuma tulis npm ci. token.txt ada dalam .dockerignore juga, jadi ia tak pernah sampai ke dapur.
Selamat? Leret ke kanan untuk ya, ke kiri untuk “perlu dibaiki”.
Kini anda boleh pinjamkan rahsia dan simpan muat turun di rak.
You finished this before. Practice is always welcome!
Official docs: Docker docs: Build secrets · Docker docs: Optimize cache usage (cache mounts) · Dockerfile reference: RUN --mount · Build check: SecretsUsedInArgOrEnv
You've learned for 25 minutes. A short break helps your brain.
Sprint done! Great focus. Stretch for a minute?