Docker, start to ship · Chapter 21 of 26
The inner loop
By the end of this chapter you can see a change to a page at once, rebuild and replace one service of a stack, read one service's logs, and run a one-off command in a throwaway service container. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Compose
- 5 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
The chai-21 stack is running: web (nginx, with the menu page copied into its image) on port 8021, and api (the price list, node) on port
- Meera wants Masala chai on the menu, and Kabir has already fixed a price in
api/prices.js, but the running api does not know:
$ curl -s localhost:8071
{"cutting":10,"ginger":15,"masala":200}
With what you know, every change means build, wait, docker compose up -d for the whole stack, then find the next typo and do it again. Bunty spent twenty minutes on one line of HTML, and restarted the web server each time he fixed the api.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Mount web/site and add Masala chai with no rebuild
Make your edits to the menu page show at once, with no rebuild.
1. Open compose.yaml. Under the web service, mount the folder web/site at /usr/share/nginx/html. Read-only is best.
2. Run docker compose up -d to apply the change.
3. Add a line with Masala chai to the menu in web/site/index.html.
4. Run curl localhost:8021 to see the menu.
To pass: the web service is running, with web/site from the chapter folder mounted at /usr/share/nginx/html. web/site/index.html has Masala chai in it. localhost:8021 shows that file exactly as it is saved now.
- Task 2
Rebuild and replace only the api
Rebuild only the api, so it serves the fixed price.
1. Look at api/prices.js. Kabir fixed the price there, but the running api still uses the old image.
2. Build the api image again and replace its container. Name only the api service in the command.
3. In this lab, also add --force-recreate. Then Compose really makes a new container.
4. Run curl localhost:8071 to see the prices.
To pass: the api service is running, and localhost:8071 says masala is 20. You ran docker compose up -d --build api (or docker compose build api) in the terminal, in the chapter folder. Only a command that worked counts. A command with a typing mistake or an error does not count, so type it again.
- Task 3
Read the logs of the api only
Read the logs of the api service, and of no other service.
1. Use docker compose logs.
2. Give it the service name api, not the container name.
3. Find the line the api printed when it started. It says the masala chai price.
To pass: you ran docker compose logs with the service name api, in the terminal, in the chapter folder, and it worked.
- Task 4
Run the price tests in a one-off api container
Run the price tests once, in a new api container that is deleted at the end.
1. Finish task 2 first. Before the rebuild, the tests see the old price and fail.
2. Use docker compose run on the api service, with the command node test.js.
3. Ask Compose to delete the container when the tests end.
4. Run docker ps -a. If you see one-off api containers (their names end in _tmp and a number), delete them.
To pass: docker compose run on the api service with node test.js worked, so the tests passed. You typed it in the terminal, in the chapter folder. No one-off api containers are left.
- Task 5 · interview question
Interview: stop web, start it, explain down
Interview question: what does docker compose down do that docker compose stop does not?
1. Run docker compose stop web.
2. Then run docker compose start web, so web runs again.
3. Run docker ps -a after each one, and look at the list.
4. In answer.txt in the chapter folder, answer the question in one or two sentences, in your own words.
To pass: you ran docker compose stop web, and after it docker compose start web, in the terminal, in the chapter folder. Both worked. The web service is running at the end. answer.txt says what down does to the containers.
What you will learn
- A bind mount in compose.yaml: the service reads your folder, so an edit shows at once.
- One service at a time: up -d --build --force-recreate <service> and logs <service>.
- docker compose run --rm: a one-off command, such as the tests, in a fresh container of a service.
- stop and start: pausing one service.
More in Compose
Build The inner loop 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.