Appearance
Build Cache and Small Images
61. What happens when a Dockerfile instruction changes, and how does that affect subsequent build-cache layers?
The short answer
That step and every step after it are rebuilt. The steps before it are reused from cache. This happens because each layer is built on top of the exact filesystem left by the layer below. Once one layer is different, everything above it was built on a different starting point, so none of it can be trusted from cache.
The build cache is a chain, not a pile. Each step's cached result is only valid for one exact parent.
Why steps ⑤ and ⑥ must rebuild even though their text is the same. Step ⑥ compiles code using the libraries installed in step ④. If step ④ now installs different libraries, the old result of step ⑥ was built against the wrong libraries. The builder cannot know whether that matters, so it plays safe and rebuilds.
The rule that follows: order your Dockerfile from "changes least" to "changes most". Base image first, system packages next, dependencies after that, your own source code last. Question 63 applies this rule to the most important case.
62. How does Docker decide whether a build step can reuse cache?
The short answer
For each step, the builder works out a cache key from the parent layer plus the instruction itself. For most instructions, the instruction's text is enough. For COPY and ADD, it also uses a checksum of the files being copied. For RUN, it uses only the command text, never the command's result. If an existing cache entry has the same key, the step is skipped and that result is reused.
Think of the cache key as a fingerprint of "everything this step depends on". Here is what goes into it for each kind of step:
| Step | Cache key is built from |
|---|---|
FROM | The base image's digest |
RUN | Parent layer + command text + build args it uses |
COPY, ADD | Parent layer + destination + checksum of the files |
ENV, WORKDIR, USER | Parent layer + instruction text |
The surprise in the RUN row. Docker does not run the command to see whether its result would be different. It only compares the text. So this step is cached forever:
dockerfile
RUN apt-get update && apt-get install -y curlA month later, the package servers have security fixes, but your build still says CACHED for this line and reuses the month-old result. You get fresh packages only when the line's text changes, the base image changes, or you build with --no-cache or --pull.
The surprise in the COPY row. It is the content that counts, not the timestamp. Touching a file without changing it does not break the cache. Changing one character in one copied file does.
BuildKit adds two refinements:
- It works out the whole graph of steps first, so independent stages are built at the same time, and a stage nothing depends on is skipped entirely.
--no-cache-filterlets you rebuild just one stage:docker build --no-cache-filter=deps .
You can see each step's decision in the build output:
bash
docker build --progress=plain -t api .#5 [2/6] WORKDIR /app
#5 CACHED
#6 [3/6] COPY package.json package-lock.json ./
#6 CACHED
#7 [4/6] RUN npm ci --omit=dev
#7 CACHED
#8 [5/6] COPY dist/ ./dist/
#8 DONE 0.2sCACHED means the step was reused. The first step without it is where the chain broke.
63. Why should stable dependency manifests generally be copied before frequently changing application source files?
The short answer
Because installing dependencies is slow and changes rarely, while source code changes on every commit. If you copy only the dependency files first and install, that slow step stays cached until the dependencies really change. If you copy all the source first, every one-line code change breaks the cache before the install, and you reinstall everything on every build.
Compare two Dockerfiles for the same Node.js app.
dockerfile
FROM node:20-slim
WORKDIR /app
COPY . .
RUN npm ci
RUN npm run build
CMD ["node", "dist/server.js"]dockerfile
FROM node:20-slim
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
CMD ["node", "dist/server.js"]In the slow version, COPY . . copies everything, including your source files. Change one line of code, and that COPY step's checksum changes. By the chain rule from question 61, npm ci now runs again, downloading and installing every package. That might take three minutes for a ten-second code change.
In the fast version, the first COPY takes only package.json and package-lock.json. Those files change only when someone adds or upgrades a library. A code change leaves them identical, so npm ci stays cached. The build only redoes the cheap steps at the end.
The same rule works for every language:
| Language | Copy first | Then install |
|---|---|---|
| Node.js | package.json, package-lock.json | npm ci |
| Python | requirements.txt or pyproject.toml and lock | pip install |
| Java (Maven) | pom.xml | mvn dependency:go-offline |
| Go | go.mod, go.sum | go mod download |
Why the lock file matters. A lock file records the exact version of every library. Copying it first means the install step's cache is tied to exact versions, and npm ci installs exactly those versions, so two builds of the same commit get the same libraries.
64. How would you diagnose a Docker build that unexpectedly invalidates most of its cache?
The short answer
Find the first step that was not cached, because everything after it rebuilds as a consequence. Build with --progress=plain and look for the first step without CACHED. Then ask what that step depends on that changed: copied files, a build argument, the base image, or the machine itself. The usual causes are an early COPY . ., a missing .dockerignore, a build argument that changes every time, a moved base image tag, and a CI machine that starts with an empty cache.
Step 1: find the first miss.
bash
docker build --progress=plain -t api . 2>&1 | grep -E '^#[0-9]+ \[|CACHED'This prints each step and whether it was cached. The first step without CACHED is the one to study. Everything after it is a consequence, not a cause.
Step 2: ask what that step depends on. Work through the usual suspects:
- A
COPYthat copies too much.COPY . .near the top means any file change anywhere breaks the cache. Fix the order (question 63). - A missing or weak
.dockerignore. Files that change on every run, such as.git, log files, test reports or editor folders, are being copied. Question 60 covers what to exclude. - A build argument that is always different.
ARG BUILD_DATEorARG GIT_COMMITused early in the file changes every build. EveryRUNafter theARGis declared and uses it gets a new cache key. Declare such arguments as late as possible, right before the step that needs them, usually a finalLABEL. - The base image moved. If the build uses
--pulland someone pushed a new image under the same tag, the very first step changes and nothing is cached. Pinning the base image by digest makes this visible and deliberate (question 86). - A generated file. Some tools write a file with the current time or a random value into the source folder, such as a version file. If it is copied early, it breaks the cache every time.
- The cache is simply not there. In CI, every job may start on a fresh machine with an empty cache. Then nothing is cached, and nothing is wrong with your Dockerfile. Question 77 covers sharing cache between CI machines.
- The cache was cleaned up. BuildKit deletes old cache entries when it runs short of space, and someone may have run
docker builder prune.
Step 3: prove it. Build twice in a row with no changes. If the second build is fully cached, the Dockerfile is fine, and the problem is something that changes between builds: points 2 to 6, or the machine, point 7. If even the second build misses, look for a step whose input is different every time, such as point 3 or 5.
65. What is a multi-stage build, and why is it useful?
The short answer
A multi-stage build is a Dockerfile with several FROM lines. Each FROM starts a separate stage. You compile or build in an early stage that has all the heavy tools, then copy only the finished result into a small final stage. The compilers, source code, caches and secrets of the early stages never reach the final image, which ends up smaller, faster to ship and safer.
Building software needs much more than running it. To build a Go service you need the Go compiler, about 800 MB. To run it you need one file, the compiled program, around 15 MB. Before multi-stage builds, teams either shipped the compiler to production or kept two Dockerfiles and a script to move files between them.
COPY --from crosses into the final image.Why it is useful:
- Small images. Often ten to fifty times smaller. Smaller images pull faster, start faster on new machines, and cost less to store.
- Fewer vulnerabilities. Every package in an image is something a scanner can flag and an attacker can use. A compiler, a package manager and a shell are all useful to an attacker. Leaving them out removes them as options.
- No leftover secrets. Anything an early stage needed, such as a private package token, stays in that stage. It does not reach the final layers. Build secrets from question 79 are still the right way to pass the token, so it never sits in any layer at all.
- One file. Build and runtime live in one Dockerfile that everyone can read.
- Parallel and selective builds. BuildKit builds independent stages at the same time, and
docker build --target build .stops at a named stage, which is handy for debugging.
Questions 66, 67 and 68 show real multi-stage files for compiled, Node.js, Python and Java applications.
66. How would you write a multi-stage build for a compiled application?
The short answer
In the first stage, start from the full compiler image, copy the dependency files and download dependencies, then copy the source and compile a binary. In the final stage, start from a tiny base image such as distroless or scratch, copy in only the binary, set a non-root user and set the binary as the entrypoint.
Here is a Go service. Go is the clearest example, because a Go program can be compiled into one self-contained file.
dockerfile
# syntax=docker/dockerfile:1
# (1)
FROM golang:1.23 AS build
WORKDIR /src
# (2)
COPY go.mod go.sum ./
RUN go mod download
# (3)
COPY . .
# (4)
RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/checkout ./cmd/checkout
# (5)
FROM gcr.io/distroless/static-debian12:nonroot
# (6)
COPY --from=build /out/checkout /checkout
# (7)
USER nonroot:nonroot
EXPOSE 8080
# (8)
ENTRYPOINT ["/checkout"]- The build stage uses the official Go image, which includes the compiler. It is named
build. - Dependency files first, then download, so this slow step is cached until the dependencies change (question 63).
- Now the source code.
- The compile.
CGO_ENABLED=0tells Go not to link against the system's C library, so the program has no outside dependencies at all and can run on an empty base.-trimpathremoves your local folder paths from the program, so two builds on different machines produce the same file.-ldflags="-s -w"strips debugging information, making the file smaller. - The final stage. This distroless "static" image contains almost nothing: certificate files for HTTPS, time zone data, and a user list. No shell, no package manager. The
nonrootvariant is set up for an unprivileged user. - The only thing copied from the build stage: the binary.
- Runs as that unprivileged user.
- Exec form, so the program is process 1 and receives stop signals (question 50).
Result: the build stage is around 900 MB, and the final image is about 17 MB.
Why not scratch? You can use FROM scratch for a program like this, but then you must copy in the CA certificates yourself. Without them, every HTTPS call fails with a certificate error. You must also add time zone data if the program converts time zones. The distroless static image already contains both. Question 70 compares the options.
67. How would you build a Node.js or Python application so development tools and test dependencies do not ship in the runtime image?
The short answer
Use separate stages. One stage installs everything, including development tools, and builds the app. Another stage installs only the production dependencies. The final stage starts from a slim runtime image and copies in just the built code and the production dependencies. For Python, build into a virtual environment in the first stage and copy that environment into the final one.
Node.js with TypeScript:
dockerfile
# syntax=docker/dockerfile:1
# (1)
FROM node:20-slim AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY tsconfig.json ./
COPY src/ ./src/
RUN npm run build
# (2)
FROM node:20-slim AS prod-deps
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
# (3)
FROM node:20-slim AS runtime
ENV NODE_ENV=production
WORKDIR /app
COPY --from=prod-deps --chown=node:node /app/node_modules ./node_modules
COPY --from=build --chown=node:node /app/dist ./dist
COPY package.json ./
USER node
CMD ["node", "dist/server.js"]- The
buildstage installs all dependencies, including TypeScript, linters and testing libraries, because compiling needs them. Then it compilessrc/intodist/. - The
prod-depsstage installs only production dependencies.--omit=devskips everything listed underdevDependencies. It runs separately, so the development packages from stage 1 are never mixed in. - The final stage takes the compiled code from
buildand the cleannode_modulesfromprod-deps. TypeScript, the source files and the development packages are left behind. Often this cuts the image from over a gigabyte to under 200 MB.
Python:
dockerfile
# syntax=docker/dockerfile:1
# (1)
FROM python:3.12-slim AS build
RUN apt-get update && apt-get install -y --no-install-recommends build-essential \
&& rm -rf /var/lib/apt/lists/*
# (2)
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
COPY requirements.txt .
# (3)
RUN pip install --no-cache-dir -r requirements.txt
# (4)
FROM python:3.12-slim AS runtime
COPY --from=build /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH" PYTHONUNBUFFERED=1
WORKDIR /app
COPY app/ ./app/
RUN useradd --system --uid 10001 app
USER 10001
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "app.main:app"]- The build stage installs a C compiler, because some Python packages compile native code while installing.
- A virtual environment is a self-contained folder holding a Python setup and its packages. Putting everything in
/opt/venvmakes it easy to copy as one unit. - Installs the packages.
--no-cache-dirstops pip keeping downloaded files, which would only waste space. - The runtime stage has no compiler. It receives the finished virtual environment and the app code.
PYTHONUNBUFFERED=1makes Python print logs immediately instead of holding them in a buffer, sodocker logsshows them straight away.
requirements.txt here should list only what production needs. Keep development and testing tools in a separate file, such as requirements-dev.txt, that only CI installs.
68. How would you build a Java application with a separate compile stage and runtime stage?
The short answer
Compile in a stage that has the full JDK and Maven or Gradle, downloading dependencies in their own cached step first. Then copy only the built JAR into a runtime stage based on a JRE image, which can run Java but not compile it. For Spring Boot, extract the JAR into layers, so dependencies and your own code become separate image layers and small code changes upload only a small layer.
The JDK, the Java Development Kit, includes the compiler and tools, about 450 MB. The JRE, the Java Runtime Environment, only runs compiled programs, about 200 MB.
dockerfile
# syntax=docker/dockerfile:1
# (1)
FROM maven:3.9-eclipse-temurin-21 AS build
WORKDIR /src
# (2)
COPY pom.xml .
RUN mvn -B -q dependency:go-offline
# (3)
COPY src/ ./src/
RUN mvn -B -q package -DskipTests
# (4)
RUN java -Djarmode=tools -jar target/app.jar extract --layers --launcher --destination /out
# (5)
FROM eclipse-temurin:21-jre AS runtime
WORKDIR /app
COPY --from=build /out/dependencies/ ./
COPY --from=build /out/spring-boot-loader/ ./
COPY --from=build /out/snapshot-dependencies/ ./
COPY --from=build /out/application/ ./
RUN useradd --system --uid 10001 app
USER 10001
# (6)
ENTRYPOINT ["java", "-XX:MaxRAMPercentage=75", "org.springframework.boot.loader.launch.JarLauncher"]- Maven with a full JDK 21.
- Copy only the project file and download every dependency. This is the slow step, and it stays cached until
pom.xmlchanges. - Copy the source and build the JAR.
-DskipTestsis used because the CI pipeline has already run the checks in an earlier stage of the pipeline, and running them again inside the image build only makes it slower. - A Spring Boot feature that splits the JAR into folders: external libraries, which rarely change, and your own classes, which change every commit.
- The runtime stage uses only the JRE.
- Each folder from step 4 is copied as its own layer, from least to most often changed. A code change now rebuilds and uploads only the small
applicationlayer, a few hundred kilobytes, instead of a 60 MB JAR.-XX:MaxRAMPercentage=75tells the Java virtual machine to size its memory from the container's memory limit, leaving a quarter for everything else. Modern Java versions read the container's limit automatically, but without this flag they use only a quarter of it for the heap by default.
69. What is the difference between a build image and a runtime image?
The short answer
A build image holds everything needed to turn source code into a runnable program: compilers, SDKs, header files, package managers and build tools. A runtime image holds only what is needed to run that program: the runtime, the system libraries it links to, certificates and the program itself. Build images are big and full of tools. Runtime images should be small and contain as little as possible.
| Build image | Runtime image | |
|---|---|---|
| Purpose | Compile and package | Run in production |
| Contains | Compiler, SDK, tools | Runtime, libraries, the app |
| Example | golang:1.23, maven:3.9-... | distroless/static, eclipse-temurin:21-jre |
| Typical size | 500 MB to 1.5 GB | 2 MB to 250 MB |
| Shell and package manager | Yes | Often no |
| Where it runs | CI, during the build | Production |
Why keep them separate. Every extra tool in a runtime image has a cost:
- Size. More to store, more to pull on every new machine, slower scale-ups.
- Security. More packages means more vulnerability reports to read and more tools an attacker can use after breaking in. A compiler lets an attacker build their own tools on your server. A package manager lets them install anything.
- Clarity. When a runtime image contains only the app, it is obvious what is running.
Multi-stage builds (question 65) are how you get both from one Dockerfile.
70. How would you choose between Alpine, Debian slim, distroless, and scratch as a base image?
The short answer
Debian slim is the safe default: small, familiar, and based on the common glibc C library. Alpine is smaller still, but uses a different C library, musl, which breaks some software. Distroless has no shell or package manager at all, which is great for security but harder to debug. scratch is completely empty and only suits programs that carry everything they need inside themselves, such as static Go binaries.
| Base | Approx. size | Shell | Package manager | C library |
|---|---|---|---|---|
debian:12-slim | 75 MB | Yes | apt | glibc |
alpine:3.20 | 8 MB | Yes, BusyBox | apk | musl |
distroless/base | 20 MB | No | No | glibc |
distroless/static | 2 MB | No | No | None |
scratch | 0 MB | No | No | None |
A C library is the basic system library almost every program uses to talk to the kernel, open files, allocate memory and look up names. Most Linux systems use glibc. Alpine uses musl, a smaller one. Programs compiled against one may not run on the other, which question 71 explains.
When to use each:
- Debian slim (or Ubuntu): when you need to install system packages, run shell scripts, or use software that expects a normal Linux. Most compatible, and easy to debug.
- Alpine: when size really matters and your software works with musl. Fine for many Go programs and scripts. Risky for Python, Node.js native modules and some Java setups.
- Distroless: for production images of Java, Node.js, Python or Go apps whose start command does not need a shell. Small, and very few vulnerabilities to report, because so little is inside. Google publishes
:debugversions with a BusyBox shell for troubleshooting. scratch: for fully static binaries, when you are happy to add certificate files and a user list yourself.
The trade-off in one sentence: the less an image contains, the safer and smaller it is, and the harder it is to look inside when something goes wrong. Ephemeral debug containers (question 414) solve the debugging part, so many teams now choose distroless for production.
71. What compatibility problems can arise when moving from glibc-based images to Alpine's musl libc?
The short answer
Programs compiled for glibc may not start at all on Alpine, often with a confusing "not found" error. Python packages and Node.js native modules may have no ready-made build for musl, so they compile from source, which is slow and needs compilers. Name resolution behaves differently in some cases. Memory allocation can be slower, threads get smaller stacks by default, and locale support is limited.
These are the problems that come up in real projects:
- Binaries that "do not exist". You copy a program built on Debian into Alpine and run it. The error is
exec /app/tool: no such file or directory, even though the file is right there. The missing file is not the program. It is glibc's dynamic loader, the helper that starts programs, at/lib64/ld-linux-x86-64.so.2. Alpine does not have it. - No ready-made packages. Python's ready-built packages, called wheels, are published for glibc systems (tagged
manylinux) and sometimes for musl systems (musllinux). When there is no musl wheel, pip compiles from source. Builds become much slower and need compilers in the image. The same goes for Node.js modules with native code. - Name lookup differences. musl's DNS resolver has historically behaved differently from glibc's, for example in how it handles very large replies and search domains. Some of these differences have been reduced in newer musl versions, but they still surprise people in Kubernetes, where DNS settings are complex (question 375).
- Performance. musl's default memory allocator is slower than glibc's for programs that allocate memory heavily from many threads. Some Python and Java workloads run noticeably slower on Alpine.
- Smaller thread stacks. musl gives each new thread a much smaller stack by default than glibc does. Code with deep recursion can crash on Alpine with a stack overflow that never happened on Debian.
- Locales. Support for languages and character sets is limited, so some text formatting and sorting behaves differently.
How to avoid it: use Debian slim or distroless when the software is not known to work on musl. Build the software inside an Alpine stage if you do use Alpine, never copy glibc binaries in. Alpine offers a compatibility package, gcompat, as a last resort, but it covers only part of glibc.
72. How do you reduce image size without making the application fragile or difficult to maintain?
The short answer
Remove what the app does not need, and keep what it quietly depends on. Use multi-stage builds, a slim or distroless base, production-only dependencies, --no-install-recommends, clean-up in the same layer, and a good .dockerignore. Do not strip out certificates, time zone data, the user list, or a shell your start script needs. Measure what is big before cutting, and keep a debugging path.
Safe ways to shrink, in order of impact:
- Multi-stage builds (question 65). Usually the largest single saving.
- A smaller base (question 70). Debian slim instead of full Debian saves hundreds of megabytes.
- Production-only dependencies, such as
npm ci --omit=dev(question 67). - Fewer system packages.
apt-get install --no-install-recommendsskips "recommended" extras, which are often large. - Clean up in the same layer (question 74), so caches never get saved.
- A good
.dockerignore(question 60), so test data and documents never enter. - No duplicate copies from
RUN chown(question 468).
Things that look like waste but are not. Removing these makes an image smaller and broken:
- CA certificates. Without them, every HTTPS call fails with a certificate error.
- Time zone data. Without it, converting times to local time zones fails or silently uses UTC.
- The user list,
/etc/passwd. Some programs fail when the current user has no name. - A shell, if your entrypoint is a shell script.
- Locale files, if your app formats dates or numbers for different languages.
Keep it maintainable:
- Measure first. Use
docker historyor a tool calleddiveto find the large layers (question 88). Do not guess. - Keep the Dockerfile readable. A 20 MB saving is not worth a clever Dockerfile nobody can change safely.
- Keep a debugging route. For distroless images, rely on debug containers (question 414), or publish a
:debugvariant. - Use one shared base image across teams (question 165), so the knowledge lives in one place.
73. Why does deleting a large file in a later image layer not necessarily reduce the final image size?
The short answer
Because layers only add, they never shrink. The earlier layer still contains the file's bytes. Deleting it in a later layer only adds a small whiteout marker that hides the file. The image you push and pull still carries the full file in the earlier layer, so the size does not go down.
dockerfile
FROM debian:12-slim
# (1)
RUN curl -fsSLo /tmp/sdk.tar.gz https://example.com/sdk.tar.gz && tar -xzf /tmp/sdk.tar.gz -C /opt
# (2)
RUN rm /tmp/sdk.tar.gz- This layer contains the 500 MB archive and the unpacked files. The layer is now finished and can never change.
- This new layer contains only a whiteout entry saying "hide
/tmp/sdk.tar.gz". It is a few bytes.
Inside a container, the archive is gone. But docker history shows the truth:
RUN rm /tmp/sdk.tar.gz 0B
RUN curl -fsSLo /tmp/sdk.tar.gz ... 1.1GBEvery push and every pull still moves that 1.1 GB layer, including 500 MB of an archive nobody can see.
The fixes:
- Delete in the same
RUN, so the file never exists at the end of any layer:
dockerfile
RUN curl -fsSLo /tmp/sdk.tar.gz https://example.com/sdk.tar.gz \
&& tar -xzf /tmp/sdk.tar.gz -C /opt \
&& rm /tmp/sdk.tar.gz- Use a multi-stage build, so the download happens in a stage whose layers are thrown away (question 65).
- Use a mount instead of a file. A
RUN --mount=type=bindor cache mount makes files available during one step without ever writing them into a layer (question 76).
The same rule explains why a secret file deleted in a later step is still in the image. Anyone can extract the earlier layer and read it.
74. How would you combine package installation and temporary-file cleanup to avoid retaining unnecessary layer contents?
The short answer
Do the install and the clean-up in a single RUN step, joined with &&, so the temporary files are gone before the layer is saved. For Debian, that means apt-get update, then apt-get install --no-install-recommends, then deleting /var/lib/apt/lists/*, all in one step. Build-only tools can be installed, used and removed in the same step too. Or use BuildKit cache mounts, which keep caches outside the layers entirely.
Debian and Ubuntu:
dockerfile
RUN apt-get update \
&& apt-get install -y --no-install-recommends ca-certificates curl \
&& rm -rf /var/lib/apt/lists/*apt-get updatedownloads the package index, the list of available packages, which is tens of megabytes.- The install.
--no-install-recommendsskips optional extras. - Deleting the index in the same step means it never reaches the saved layer.
Keeping update and install in the same step also matters for correctness. If update were its own step, it could stay cached for months (question 62), and a later install would use an old index that points at package versions which no longer exist on the server.
Alpine:
dockerfile
RUN apk add --no-cache ca-certificates curl--no-cache makes apk fetch the index into memory and not save it, so there is nothing to clean up.
Tools needed only to build something:
dockerfile
RUN apt-get update \
&& apt-get install -y --no-install-recommends build-essential \
&& pip install --no-cache-dir -r requirements.txt \
&& apt-get purge -y --auto-remove build-essential \
&& rm -rf /var/lib/apt/lists/*The compiler is installed, used to build Python packages, and removed, all within one layer. The saved layer contains only the result. A multi-stage build (question 67) does the same job more cleanly, and is usually the better choice.
With BuildKit, there is a neater option: cache mounts. The package manager's cache lives in a separate store outside the image, reused between builds, and never saved into a layer. Question 76 shows how.
bash
docker history --format '{{.Size}}\t{{.CreatedBy}}' api # check the result: the RUN layer should hold only what you installed