Docker, start to ship · Chapter 8 of 26

Cache is money

By the end of this chapter you can order a Dockerfile so rebuilds take seconds, keep files out of a build with .dockerignore, and ship a rebuilt image. 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
  • 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

Bunty has put Tapri's orders API in app/ with a Dockerfile, and every small fix to server.js costs him the slow install again. Here is his second build, after adding one comment to server.js (he tagged it 0.1):

$ docker build -t chai-08-api:0.1 app
STEP 1/5: FROM node:24-alpine
STEP 2/5: WORKDIR /app
--> Using cache 9248bf9c372179f305e63b84c37efe873a7cf213286d1b96c74e1660443d4291
--> 9248bf9c372
STEP 3/5: COPY . .
--> 6e5d8e96daa
STEP 4/5: RUN node install.js
installing chai-utils (this takes a while)...
added 1 package in 3s
--> 9c345f1721b
STEP 5/5: CMD ["node", "server.js"]
COMMIT chai-08-api:0.1
--> dffec62f86f
Successfully tagged localhost/chai-08-api:0.1
dffec62f86fb12733af18c0e8ca579f9fc52b6119f6d1eb70b2b17f43a5768cb

(install.js is Tapri's stand-in for npm install: the lab has no internet, so it takes three seconds to write node_modules. A real install takes minutes.) Worse, Ravi found the payment key inside the image:

$ docker run --rm chai-08-api:0.1 cat .env
PAYMENT_KEY=sk_live_tapri_4f9a2c7e1b

Building more often will not help: what decides the waiting, and what goes in, is the Dockerfile and the folder it builds from.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Put the install step before the code

    Change the order of the steps in app/Dockerfile, so a code change does not run the slow install again.

    1. Open app/Dockerfile. Find the install step: the RUN line that runs node install.js.

    2. Before the install step, copy only package.json and install.js.

    3. After the install step, copy the rest of the app.

    4. Keep the same base image, working folder and command.

    To pass: app/Dockerfile builds with the app folder as the context. When only server.js changes, the build takes the install step from the cache.

  2. Task 2

    Keep node_modules and .env out with app/.dockerignore

    Keep the folder from Bunty's Mac and the payment key out of the image.

    1. Make a file called .dockerignore in the app folder, next to app/Dockerfile.

    2. Write node_modules on one line and .env on the next line.

    3. Build the image chai-08-api:1.0 with the app folder as the context.

    To pass: app/.dockerignore lists node_modules and .env. The image chai-08-api:1.0 has no .env in /app, and its node_modules is the one the install made during the build.

  3. Task 3

    Run chai-08-api on port 8008

    Run the orders API from your new image.

    1. Build chai-08-api:1.0 with the app folder as the context.

    2. Start a container named chai-08-api from it, in the background.

    3. Publish the container's port 3000 on the lab's port 8008.

    4. Run curl on localhost:8008 to see the answer.

    To pass: chai-08-api is running from chai-08-api:1.0, and localhost:8008 answers with the tapri-orders JSON.

  4. Task 4

    Ship Meera's new message from the cache

    Meera wants a new message. Ship it, and let the build reuse the install step.

    1. Build chai-08-api:1.0 first (task 3). Change server.js only after that.

    2. In app/server.js, change the message Orders are open to Orders are open, chai is hot.

    3. Build chai-08-api:1.0 again, with the same name and tag. The install step should say Using cache.

    4. Delete the container chai-08-api. Start it again the same way as in task 3, on port 8008.

    To pass: chai-08-api runs the newest chai-08-api:1.0, and localhost:8008 shows the new message. The install step in the image must be older than your change to server.js, so build first and edit after.

  5. Task 5 · interview question

    Interview: why COPY . . early breaks the cache

    Interview question: why does copying the whole folder before the install step break the cache?

    1. Write one or two sentences in your own words.

    2. Say what a change to any file does to that copy.

    3. Say what then happens to the steps after it.

    4. Save the sentences in answer.txt, in the chapter folder.

    To pass: answer.txt says what a change to any file does, and what it means for the cache or the steps after it.

What you will learn

  • The build cache: which steps a rebuild reuses, and why one change makes every later step run again.
  • Step order: slow steps that rarely change go first.
  • .dockerignore: files the build never sees.
  • Shipping a rebuild: a running container keeps its old image.

More in Images and Dockerfiles

Build "Cache is money" 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.