BM: Baca dalam Bahasa Melayu
World D4: Set Meals
.env and variables
You will learn: to fill blanks in compose.yaml from a .env file.
Checked with Docker Engine 29 on 31 Oct 2026
The problem
Your compose.yaml says POSTGRES_PASSWORD: example. You push it to a public repo. Who can read the password now?
Pick one answer.
Watch
db:
image: postgres:18-alpine
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD:?add DB_PASSWORD to .env}Your laptop~/kedai-kancil
- No open hatches
Lunchboxes (containers)
- No containers yet
Frozen packs (images)
- kedai-kancil-api:latest6 layers · 244MB
- kedai-kancil-web:latest9 layers · 92.6MB
- nginx:1.30-alpine8 layers · 93.5MB
- node:24-alpine4 layers · 244MB
- postgres:18-alpine9 layers · 433MB
- redis:8-alpine7 layers · 162MB
0 containers (0 running), 6 images, 0 lockers.
An order slip with a blank. ${DB_PASSWORD} is a blank that Compose fills in. That is interpolation: Blanks like ${DB_PASSWORD} in compose.yaml that Compose fills in from your shell or a .env file.. :? means: stop if it stays empty.
Try it
Fill the blank. Put the password in a .env file: A file next to compose.yaml with lines like DB_PASSWORD=devpass. Keep it out of Git. next to compose.yaml. Then start the shop.
Your laptop~/kedai-kancil
- No open hatches
Lunchboxes (containers)
- No containers yet
Frozen packs (images)
- kedai-kancil-api:latest6 layers · 244MB
- kedai-kancil-web:latest9 layers · 92.6MB
- nginx:1.30-alpine8 layers · 93.5MB
- node:24-alpine4 layers · 244MB
- postgres:18-alpine9 layers · 433MB
- redis:8-alpine7 layers · 162MB
0 containers (0 running), 6 images, 0 lockers.
Hint
One line makes the file:echo "DB_PASSWORD=devpass" > .envPredict
The web hatch is a blank with a default: "${WEB_PORT:-8080}:80". Your .env has no WEB_PORT line. Which hatch opens?
Pick one answer.
Check
Swipe right if it is safe to commit to Git. Swipe left if not.
Swipe right for good, left for needs work. Or use the buttons.
compose.yaml with ${DB_PASSWORD} in it
You did it!
You can now fill blanks in compose.yaml from a .env file.
Go deeper (optional)
Shell variables beat .env: WEB_PORT=9090 docker compose up -d opens hatch 9090. --env-file prod.env reads another file. env_file: on a service is different: it hands variables to the box itself. docker compose config shows every blank filled in. More on real secrets: World C3.
Real mission
echo "DB_PASSWORD=devpass" > .envecho ".env" >> .gitignoredocker compose config
0 of 3 steps done · Your ticks are saved.
Bahasa Melayu
compose.yaml anda tulis POSTGRES_PASSWORD: example. Anda push ke repo awam. Siapa boleh baca kata laluan itu sekarang?
db:
image: postgres:18-alpine
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD:?add DB_PASSWORD to .env}Slip pesanan dengan tempat kosong. ${DB_PASSWORD} ialah tempat kosong yang Compose isi. Itulah interpolation (isi tempat kosong). :? maksudnya: berhenti kalau ia kekal kosong.
Isi tempat kosong itu. Letak kata laluan dalam fail .env di sebelah compose.yaml. Kemudian hidupkan kedai.
echo "DB_PASSWORD=devpass" > .envTingkap web ialah tempat kosong dengan nilai lalai: "${WEB_PORT:-8080}:80". Fail .env anda tiada baris WEB_PORT. Tingkap mana yang dibuka?
Leret ke kanan kalau selamat untuk commit ke Git. Leret ke kiri kalau tidak.
Kini anda boleh isi tempat kosong dalam compose.yaml dari fail .env.
You finished this before. Practice is always welcome!
Official docs: Interpolation · Environment variables precedence · docker compose config
You've learned for 25 minutes. A short break helps your brain.
Sprint done! Great focus. Stretch for a minute?