Docker, start to ship · Chapter 14 of 26
Bind mounts and live code
By the end of this chapter you can lend a folder of yours to a container, read-only or writable, and change a running site without a rebuild. 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
Meera changes today's special on the menu site most mornings. Bunty's way: edit site/index.html, build a new image, remove the old container, run a new one, check the page. docker images already shows chai-14-bunty:v7, and nginx's logs vanish with every container he removes.
Everything so far put files into a container by copying them into an image (chapter 7). That is right for shipping: the image is the release. While you work on the files, though, every change costs a build and a new container, and what the container writes stays inside it.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Serve the site folder from chai-14-web on port 8014
Serve the site folder from nginx, without copying it into an image.
1. Port 8014 is taken by Bunty's container chai-14-bunty. Delete that container.
2. Start a container named chai-14-web from nginx, in the background.
3. Publish its port 80 on the lab's port 8014.
4. Bind-mount this chapter's site folder at /usr/share/nginx/html, read-only.
To pass: chai-14-web is running, the site folder is bind-mounted read-only at /usr/share/nginx/html, its port 80 is on the lab's port 8014, and localhost:8014 shows the Tapri menu page.
- Task 2
Make today's special ginger chai, with no rebuild
Change the page without building anything.
1. Open site/index.html on the lab machine.
2. Change today's special from masala chai to ginger chai. Do not build an image.
3. Run curl on localhost:8014 to see the change.
To pass: site/index.html says ginger chai, chai-14-web still mounts the site folder, and localhost:8014 shows ginger chai.
- Task 3
Keep nginx's logs in the logs folder
Make nginx write its logs into this chapter's logs folder.
1. Delete chai-14-web. A container's mounts cannot change after it is made.
2. Start a new chai-14-web with the same site mount and port 8014 as in task 1.
3. Also bind-mount this chapter's logs folder at /var/log/nginx. Do not make it read-only.
4. Visit the page, then run cat on logs/access.log.
To pass: chai-14-web is running with the logs folder bind-mounted, writable, at /var/log/nginx, and logs/access.log has nginx's GET requests. Keep the read-only site mount and port 8014, or tasks 1 and 2 stop passing.
- Task 4 · interview question
Interview: a bind mount or a named volume
Interview question: how is a bind mount different from a named volume?
1. Write one or two sentences in your own words.
2. Say where the files of each one live.
3. Say who chooses that place and looks after it.
4. Save the sentences in answer.txt, in the chapter folder.
To pass: answer.txt says where a bind mount's files live, and who looks after a named volume.
What you will learn
- Bind mounts: -v /a/path:/in/container shows a folder of the lab machine inside the container, the real files, not a copy.
- :ro: a mount the container may read but not change.
- Writable mounts: a container writing files you keep, such as logs.
- Mounts are fixed: adding one means a new container.
More in Data
Build "Bind mounts and live code" 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.