Sub topic 11
Example: Node.js and Python runtime images
This chapter assumes you know how layers and the build cache work, and that you can write a basic multi-stage Dockerfile with FROM ... AS and COPY --from. You should also be able to build a small Node.js or Python app.
Go can ship a single static binary. Node.js and Python cannot, because they need an interpreter at runtime. The goal is therefore different. You keep the interpreter and the production dependencies, and you drop everything used only to build the app: compilers, dev dependencies, caches and source files.
The shape of the build
Both examples use the same two-stage pattern.
Only the last stage becomes the final image. Earlier stages stay in the build cache but are not part of what you push.
Node.js
FROM node:20 AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:20-slim
WORKDIR /app
ENV NODE_ENV=production
COPY package*.json ./
RUN npm ci --omit=dev
COPY --from=build /app/dist ./dist
USER node
CMD ["node", "dist/index.js"]The build stage uses the full node:20 image and installs every dependency, including dev tools such as TypeScript or a bundler. It then runs npm run build to produce dist/.
The runtime stage starts again from node:20-slim. It copies only the package files and runs npm ci --omit=dev, so test runners and compilers never reach the final image. It then copies dist/ from the build stage.
Two details matter here:
package*.jsonis copied before the rest of the source in both stages. A source change then reuses the cached dependency layer.- The
nodeuser already exists in official Node images, soUSER nodeneeds no extra setup.
Python
# syntax=docker/dockerfile:1
FROM python:3.12-slim AS build
WORKDIR /app
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
COPY requirements.txt .
RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt
FROM python:3.12-slim
ENV PATH="/opt/venv/bin:$PATH"
WORKDIR /app
COPY --from=build /opt/venv /opt/venv
COPY . .
USER nobody
CMD ["python", "app.py"]Python has no dist/ folder, so the unit you move between stages is a virtual environment. The build stage installs packages into /opt/venv. The runtime stage copies that one directory and sets PATH so python resolves to the venv.
The --mount=type=cache flag keeps pip's download cache on the build host. It is not written into a layer, so builds get faster and the image does not grow. The # syntax=docker/dockerfile:1 line on the first line enables this BuildKit feature.
Common errors
- Packages needing a compiler. Some Python packages build C extensions during install. A slim image may lack
gcc. Install the compiler only in the build stage. If the package also needs a shared library at runtime, install that library in the runtime stage too. - Missing runtime files. If you copy only
dist/, files read at runtime, such as templates or migrations, are missing. Copy them explicitly. - Wrong ownership. Files copied with
COPYare owned by root. That is fine for read-only code. If the app writes to a directory, useCOPY --chown=node:nodeor create a writable directory for it. NODE_ENVset too early. If you setNODE_ENV=productionbeforenpm ciin the build stage, dev dependencies may be skipped and the build can fail. The Node example sets it only in the runtime stage.
Check the result
Compare the two stages with the commands from the measuring chapter.
docker build -t myapp:small .
docker build --target build -t myapp:build .
docker image ls myappThe small image should be noticeably smaller than the build image.
Exercise
In the Python example, why does the runtime stage run COPY --from=build /opt/venv /opt/venv instead of running pip install again?
Show answer
The build stage already installed the packages into the virtual environment, so copying that directory brings in exactly the installed dependencies. The runtime stage then needs no pip cache, no build tools and no second install step. This only works because both stages use the same base image and the same venv path.
Exercise
Your Node.js runtime image fails with "Cannot find module 'typescript'" at startup, although the build succeeded. What is the most likely cause, and what are two ways to fix it?
Show answer
The app imports a package that is listed under devDependencies, and npm ci --omit=dev did not install it in the runtime stage. Either move the package to dependencies, if the app needs it at runtime, or bundle or compile the app in the build stage so the output no longer imports it.