World map

 

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

The orders table shows one order, nasi lemak. db is removed and started again. Now psql says: relation orders does not exist. Docker transcript: $ docker exec db psql -U postgres -c "select * from orders;" id | dish ----+------------ 1 | nasi lemak (1 row) $ docker rm -f db db $ docker run -d --name db -e POSTGRES_PASSWORD=secret postgres:18-alpine ff26fb60d1866060f2de23803b7bab68ce36c64329453308649d0970d4545128 $ docker exec db psql -U postgres -c "select * from orders" ERROR: relation "orders" does not exist LINE 1: select * from orders ^

Your laptop~

  • No open hatches

Lunchboxes (containers)

  • dbrunning
    from postgres:18-alpine no name → /var/lib/postgresql

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.

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-alpine

Question 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-alpine

Pick 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
    from postgres:18-alpine pgdata → /var/lib/postgresql

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.

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.

docker volume ls lists pgdata. docker volume rm pgdata is refused: the volume is in use by the db container. Docker transcript: $ docker volume ls DRIVER VOLUME NAME local pgdata $ docker volume rm pgdata Error response from daemon: remove pgdata: volume is in use - [39d563ae0d5fc9e0296b4ce2c08de5892b25946ff7bbd9e4c310bfec677599ac]

Your laptop~

  • No open hatches

Lunchboxes (containers)

  • dbrunning
    from postgres:18-alpine pgdata → /var/lib/postgresql

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

  1. docker volume create practice-data
  2. docker run --rm -v practice-data:/data alpine:3.22 sh -c "echo hi > /data/note.txt"
  3. docker run --rm -v practice-data:/data alpine:3.22 cat /data/note.txt
  4. docker volume rm practice-data

0 of 4 steps done · Your ticks are saved.