BM: Baca dalam Bahasa Melayu
World D3: Lockers & Phone Lines
Lockers: named volumes
You will learn: to keep data in a volume, a locker outside the container.
Checked with Docker Engine 29 on 31 Oct 2026
The problem
Your laptop~
- No open hatches
Lunchboxes (containers)
- dbrunning
Frozen packs (images)
- postgres:18-alpine9 layers · 433MB · in use
Lockers (volumes)
- 26e7b8e4309c… (no name)24 files · used by db
1 containers (1 running), 1 images, 1 lockers.
Kancil swaps the database box for a fresh one. Then: ERROR: relation "orders" does not exist. Plain words: the new database has no orders table.
Watch
A volume: A locker outside the lunchbox, so data stays when the container is gone. is a locker outside the lunchbox. Docker keeps it until you delete it. Tap the pieces to build a database with a locker.
Tap the pieces in the right order.
Try it
Start db with the pgdata locker. Then make an orders table.
Your laptop~
- No open hatches
Lunchboxes (containers)
- No containers yet
Frozen packs (images)
- alpine:3.221 layers · 12.9MB
- nginx:1.30-alpine8 layers · 93.5MB
- postgres:18-alpine9 layers · 433MB
0 containers (0 running), 3 images, 0 lockers.
Hint
-v pgdata:/var/lib/postgresql makes the locker if it isn’t there yet.Predict
Guess first, then run it. Which file is still there?
Where does each file live? Guess what stays, then run it.
$ docker run -d --name kedai -v kedai-data:/data nginx:1.30-alpineQuestion 1 of 2
Lunchbox: container kedai
- Inside the lunchbox (writable layer)
/app/today.txt
Outside the lunchbox
- Locker kedai-data (volume)
/data/orders.db
Next, you run:
Throw the container away and start a new one.
$ docker rm -f kedai$ docker run -d --name kedai -v kedai-data:/data nginx:1.30-alpinePick Stays or Gone for every file first.
Try it
Throw db away. Start it again with the same locker. Then look at the orders.
Your laptop~
- No open hatches
Lunchboxes (containers)
- dbrunning
Frozen packs (images)
- alpine:3.221 layers · 12.9MB
- postgres:18-alpine9 layers · 433MB · in use
Lockers (volumes)
- pgdata24 files · used by db
1 containers (1 running), 2 images, 1 lockers.
Hint
Same-v pgdata:/var/lib/postgresql as before.Check
Which command deletes the orders for good?
Pick one answer.
You did it!
You can now keep data in a locker that outlives the container.
Go deeper (optional)
Real error: volume is in use. Plain words: remove the box first. Without -v, Postgres gets a nameless locker. A new box gets a new one, so docker volume ls collects leftovers.
Your laptop~
- No open hatches
Lunchboxes (containers)
- dbrunning
Frozen packs (images)
- alpine:3.221 layers · 12.9MB
- postgres:18-alpine9 layers · 433MB · in use
Lockers (volumes)
- pgdata24 files · used by db
1 containers (1 running), 2 images, 1 lockers.
Real rep (optional), on your own Docker:
Real mission
docker volume create practice-datadocker run --rm -v practice-data:/data alpine:3.22 sh -c "echo hi > /data/note.txt"docker run --rm -v practice-data:/data alpine:3.22 cat /data/note.txtdocker volume rm practice-data
0 of 4 steps done · Your ticks are saved.
Bahasa Melayu
Kancil tukar bekas pangkalan data dengan yang baru. Kemudian: ERROR: relation "orders" does not exist. Maksudnya: pangkalan data baru tiada jadual orders.
Volume (loker data) ialah loker di luar bekas. Docker simpan loker itu sampai anda padam. Tekan kepingan untuk bina pangkalan data yang ada loker.
Mulakan db dengan loker pgdata. Kemudian buat jadual orders.
-v pgdata:/var/lib/postgresql buat loker itu kalau belum ada.Teka dulu, kemudian jalankan. Fail mana yang masih ada?
Buang db. Mulakan semula dengan loker yang sama. Kemudian tengok pesanan.
-v pgdata:/var/lib/postgresql yang sama macam tadi.Arahan mana yang padam pesanan buat selamanya?
Kini anda boleh simpan data dalam loker di luar container.
You finished this before. Practice is always welcome!
Official docs: Docker docs: Volumes · Docker docs: docker volume · Postgres image on Docker Hub
You've learned for 25 minutes. A short break helps your brain.
Sprint done! Great focus. Stretch for a minute?