Docker, start to ship · Chapter 15 of 26
Back up like you mean it
By the end of this chapter you can back up a volume into a tar file, delete the volume, and prove the backup by restoring it. 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
Tapri's orders live in the volume chai-15-orders, and the order board, chai-15-board (nginx), serves them on port 8015. Ravi asks whether there is a backup. Bunty points at the volume: its label even says "Bunty says this is the backup". Kabir: "A backup is a copy somewhere else, and you have restored it at least once."
Chapter 13's volume survives a container, but not docker volume rm, a full disk or a broken server. And you cannot simply copy its folder: it is inside Docker's own storage, which belongs to root, and on many machines Docker's storage is not even on the machine you type on.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Back up chai-15-orders into the backup folder
Save the orders volume chai-15-orders into a tar file, so you have a copy you can restore.
1. Stop the order board, chai-15-board, so nothing writes to the volume.
2. Run a short-lived alpine container with --rm. Mount the volume and the backup folder into it.
3. In that container, run tar. Pack the volume's files into a tar file in the backup folder.
4. List the tar's contents with tar, in the terminal.
To pass: a tar file in backup/ (its name ends in .tar, .tar.gz or .tgz) has orders.csv and customers/notes.txt. No container, running or stopped, may still mount the backup folder.
- Task 2
Delete chai-15-orders, then restore it from your backup
Prove the backup works: delete the volume, then restore it from your tar.
1. Delete the container chai-15-board. A volume in use cannot be deleted. The data is in the volume, not in the container.
2. Delete the volume chai-15-orders.
3. Make a new, empty volume with the same name, chai-15-orders.
4. Run a short-lived alpine container with the new volume and the backup folder. Unpack your tar into the volume with tar.
To pass: chai-15-orders is a new volume (the old one is really deleted). Its orders.csv has all four orders, and its customers/notes.txt has the customers' notes. Both files sit at the top of the volume.
- Task 3
Bring chai-15-board back on port 8015
Start the order board again, as it was before.
1. Run a container named chai-15-board from nginx, in the background.
2. Publish its port 80 on the lab's port 8015.
3. Mount the volume chai-15-orders at /usr/share/nginx/html, read-only as before.
4. Run curl localhost:8015/orders.csv to see the orders.
To pass: chai-15-board is running, it mounts chai-15-orders at /usr/share/nginx/html, and localhost:8015/orders.csv shows order 1044.
- Task 4 · interview question
Interview: why stop the app before a backup
Interview question: why do you stop the app before you back up its volume?
1. Think about the app writing a file while tar reads that file.
2. Write one or two sentences in your own words. Say what the copy could look like.
3. Save them in answer.txt, in the chapter folder.
To pass: answer.txt says what can go wrong with a copy made while the app is still writing.
What you will learn
- A throwaway container: --rm, a volume and a folder of yours, and tar in between.
- tar -C: packing a folder's contents, not its name.
- Deleting a volume: what stops docker volume rm, and in what order things go.
- Restoring: the same container the other way round, and the check that proves it.
More in Data
Build "Back up like you mean it" 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.