Docker, start to ship · Chapter 11 of 26
Small is fast
By the end of this chapter you can write a multi-stage Dockerfile that builds with one image and ships with a much smaller one. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Images and Dockerfiles
- 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
The menu site is one HTML page. A node script, site/build.js, makes it from menu.json into site/dist/index.html. Bunty's image, chai-11-menu:fat, starts from node, copies in everything, runs the build, and serves the page with his own small node server, serve.js.
Ravi has to pull that image on every server, over Tapri's cafe Wi-Fi. It is node's whole image plus Bunty's files, and here are the two base images it could have used (the sizes students see on the lab's servers):
$ docker images | grep -E "REPOSITORY|node |nginx "
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/library/node 24-alpine c10884952bb8 2 weeks ago 173 MB
docker.io/library/node alpine c10884952bb8 2 weeks ago 173 MB
docker.io/library/node latest c10884952bb8 2 weeks ago 173 MB
docker.io/library/nginx 1.27-alpine 6769dc3a703c 17 months ago 49.6 MB
docker.io/library/nginx alpine 6769dc3a703c 17 months ago 49.6 MB
docker.io/library/nginx latest 6769dc3a703c 17 months ago 49.6 MB
Node is needed to build the page, not to serve it. With one FROM, though, the image you build in is the image you ship, and every tool the build needed (node, npm, the sources) ships too.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Build the menu in node, ship it in nginx
Build the menu page with node, but ship only nginx and the page.
1. Write a new Dockerfile in the site folder, next to Bunty's. For example, site/Dockerfile.slim.
2. First stage: start from node:24-alpine and run node build.js on the site's files.
3. Last stage: start from nginx:alpine. Copy only the files of the built dist folder into /usr/share/nginx/html. Do not add a CMD.
4. Build it with the site folder as the context, as chai-11-menu:slim.
To pass: chai-11-menu:slim runs nginx's own command, serves the page that build.js made, and has no node in it.
- Task 2
Get slim under a third of fat's size
Make sure the new image is much smaller than Bunty's.
1. List the two images chai-11-menu:fat and chai-11-menu:slim, and compare their sizes.
2. If slim is not small enough, fix your new Dockerfile and build it again.
3. Do not delete chai-11-menu:fat. If it is gone, lab reset builds it again.
To pass: chai-11-menu:slim is less than a third of the size of chai-11-menu:fat, counted in bytes, and chai-11-menu:fat still exists.
- Task 3
Run chai-11-web from the slim image on port 8011
Run the small image and see the menu.
1. Start a container named chai-11-web from chai-11-menu:slim, in the background.
2. Publish the container's port 80 (nginx's port) on the lab's port 8011.
3. Run curl on localhost:8011 to see the page.
To pass: chai-11-web is running from the newest chai-11-menu:slim, and localhost:8011 shows Masala chai, Rs 25. If you build slim again, delete chai-11-web and start it again.
- Task 4 · interview question
Interview: what a multi-stage build leaves out
Interview question: what does a multi-stage build leave out of the final image?
1. Write one or two sentences in your own words.
2. Say what stays out of the final image, and what ends up in it.
3. Save the sentences in answer.txt, in the chapter folder.
To pass: answer.txt talks about the stages, and says how the files get into the final image or what is left out.
What you will learn
- Stages: a Dockerfile with several FROM lines, each starting again from its own base.
- COPY --from: taking a file out of an earlier stage.
- -f: building from a Dockerfile with another name.
- Measuring: comparing image sizes, and the leftovers a build leaves.
More in Images and Dockerfiles
Build "Small is fast" 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.