BM: Baca dalam Bahasa Melayu
World D2: Recipes
Run as a non-root USER
You will learn: to run your app as a normal user, so a break-in can do less.
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)
- api:1.06 layers · 244MB
- node:24-alpine4 layers · 244MB
0 containers (0 running), 2 images, 0 lockers.
Your API runs as root inside the box. A bug lets a stranger in. Now the stranger is root too, with every key in the kitchen.
Watch
Tap the line that picks who runs the app.
0 of 1 found
A non-root user: A normal user with few rights. If someone breaks in, they can do less. has few rights, like a kitchen helper. USER node switches to one. The node images already have a user called node.
Try it
Add USER node to the end of the recipe. Build api:1.1. Then ask whoami.
Your laptop~/api
- No open hatches
Lunchboxes (containers)
- No containers yet
Frozen packs (images)
- api:1.06 layers · 244MB
- node:24-alpine4 layers · 244MB
0 containers (0 running), 2 images, 0 lockers.
Hint
USER can be the last line. It sets who runs CMD.Predict
A recipe says USER node, and after it RUN apk add --no-cache curl. What happens?
Pick one answer.
Check
Your laptop~/api
- No open hatches
Lunchboxes (containers)
- No containers yet
Frozen packs (images)
- api:1.06 layers · 244MB
- node:24-alpine4 layers · 244MB
0 containers (0 running), 2 images, 0 lockers.
Real error: Permission denied, exit code 99. Plain words: only root may install. Install first, then switch with USER.
Check
Good habit? Swipe right for yes, left for “needs work”.
Swipe right for good, left for needs work. Or use the buttons.
Install packages first, then add USER near the end
You did it!
You can now run your app as a non-root user.
Go deeper (optional)
docker run -u 1000 picks a user for one run. A recipe USER is the safe default. Must the app write a folder? Give it to the user: COPY --chown=node:node . .. In World D5 you add more locks.
Real mission
docker run --rm hello:1.0 whoami
0 of 3 steps done · Your ticks are saved.
Bahasa Melayu
API anda berjalan sebagai root di dalam bekas. Satu pepijat biarkan orang asing masuk. Sekarang orang asing itu pun root, dengan setiap kunci dapur.
Pengguna bukan root ada hak yang sedikit, macam pembantu dapur. USER node tukar kepada pengguna itu. Image node sudah ada pengguna bernama node.
Tambah USER node di hujung resepi. Bina api:1.1. Kemudian tanya whoami.
Satu resepi tulis USER node, dan selepasnya RUN apk add --no-cache curl. Apa jadi?
Ralat sebenar: Permission denied, kod keluar 99. Maksudnya: hanya root boleh memasang. Pasang dulu, kemudian tukar dengan USER.
Tabiat yang bagus? Leret ke kanan untuk ya, ke kiri untuk “perlu dibaiki”.
Kini anda boleh jalankan aplikasi sebagai pengguna bukan root.
You finished this before. Practice is always welcome!
Official docs: Dockerfile reference: USER · Docker docs: Best practices (USER)
You've learned for 25 minutes. A short break helps your brain.
Sprint done! Great focus. Stretch for a minute?