Docker, start to ship · Chapter 13 of 26
Volumes
By the end of this chapter you can keep a container's data in a named volume, so it outlives the container. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Data
- 4 tasks
- Docker
- In the all labs plan
The first 2 chapters of Docker, start to ship are free. This one comes with the all labs plan.
Why you need this
Bunty wanted a clean restart of the orders cache, so he removed the redis container chai-13-bunty and ran a new one. Twelve orders vanished. His notes are in incident.txt, and Meera has typed the last order in again by hand. Ravi needs the cache to keep its orders through exactly that: a container replaced by a new one, for an upgrade, a new setting or a clean start.
So far every container has been the only home of its data. Replacing a container is the everyday Docker move (chapter 7 did it to ship 1.1), and with what you know, it throws the data away each time.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Create the volume chai-13-orders
Make a place for the orders that stays when containers are deleted.
1. Create a named volume called chai-13-orders.
2. List the volumes to see it.
To pass: a volume named exactly chai-13-orders exists.
- Task 2
Run chai-13-redis on the volume and save Meera's order
Run redis on the volume and save Meera's order in it.
1. Start a container named chai-13-redis from redis:7-alpine, in the background.
2. Mount the volume chai-13-orders at /data. Redis keeps its data in /data.
3. Run redis-cli inside the container. Save the key order:1 with the value 2 masala chai, table 4, exactly as written.
4. Read the key back with GET order:1.
To pass: chai-13-redis is running from redis, with chai-13-orders mounted at /data, and GET order:1 returns 2 masala chai, table 4.
- Task 3
Replace chai-13-redis and find the order still there
Prove the order lives in the volume, not in the container.
1. Stop chai-13-redis with docker stop. As it stops, redis writes its data into the volume.
2. Delete the container chai-13-redis.
3. Start a new chai-13-redis from redis:7-alpine, with chai-13-orders at /data again.
4. Run GET order:1 in the new container.
To pass: chai-13-redis is running, it was created after redis wrote the order into the volume, and GET order:1 still returns 2 masala chai, table 4. Do not use docker rm -f on the running container: redis has no time to write its data. A restart is not enough: it keeps the same container.
- Task 4 · interview question
Interview: where a container's data lives without a volume
Interview question: when no volume is mounted, where does a container keep the files it writes?
1. Write one or two sentences in your own words.
2. Say where those files are kept.
3. Say what happens to them when the container is deleted.
4. Save the sentences in answer.txt, in the chapter folder.
To pass: answer.txt names the part of the container where those files are written.
What you will learn
- Where a container writes: what docker diff shows, and what docker rm takes away.
- Named volumes: docker volume create, and -v name:/path on docker run.
- Anonymous volumes: why redis already had one, and why it did not help.
- Saving before you remove: why redis wants a polite stop.
More in Data
Build "Volumes" yourself
Tapri’s menu site works on one laptop and nowhere else. You ship the machine. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Docker, start to ship are free. This one comes with the all labs plan.