Docker for developers · mid
Production images: Node, Spring Boot and Python AI services
A production image is built in stages, installs dependencies in their own cached layer, runs on a small runtime base and carries no build tools. The details differ for Node, Java and Python.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Node: stage one builds (npm ci, then the build, then npm prune --omit=dev). Stage two uses the same slim base, sets NODE_ENV=production, copies only node_modules, dist and package.json, runs as USER node and starts with CMD ["node", "dist/server.js"]. Pin a version tag, never latest.
- Cache order: things that change rarely first (base image, dependency manifests, install), code last. A code change then reuses the dependency layer. COPY . . before npm ci makes every code change reinstall everything.
- Spring Boot: a runtime image that is the Maven image carries a full JDK, Maven, source code and the dependency cache. Build in one stage, run on a JRE image (eclipse-temurin JRE) or a distroless Java image.
- Copy pom.xml first and resolve dependencies in their own layer, or use a BuildKit cache mount for the Maven repository. Run tests in the CI step before, and build the image from the tested jar, not with mvn package inside the image build.
- Hand-written Dockerfile: full control, works for any stack, but each team owns the best practices. Buildpacks (Paketo, spring-boot:build-image, pack build) detect the stack and make a consistent layered non-root image, and can patch the OS layer by rebasing. Jib builds Java images with no Dockerfile and no Docker daemon.
- Python AI services: a plain pip install torch on Linux x86 pulls the CUDA build and NVIDIA libraries, gigabytes that do nothing on CPU nodes. Install the CPU wheel from the PyTorch CPU index, or use ONNX Runtime.
- Python image size: python slim base, two stages (build a virtualenv, copy it to the slim image), pip install --no-cache-dir, no compilers in the final image. Measure with docker history and dive.
- A layer is immutable and identified by content. Delete in a later layer hides a file but does not remove it from the image.
The red pen
Common traps: mistake → fix
Mistake: Putting tests inside the image build is good practice
Fix: Tests belong in the CI step. The image should contain only what runs.
Mistake: A smaller base always means a better image
Fix: Smaller is good, but Alpine can break native libraries. Slim or distroless are common safe choices.
Mistake: Jib or buildpacks replace all Docker knowledge
Fix: They hide the Dockerfile but not layers, base images and security.
Easy to hard
Questions interviewers ask, easy to hard
- Walk me through a production Dockerfile for a Node API.
- Why does the order of COPY and RUN lines matter?
- A Spring Boot Dockerfile uses the Maven image as the final image. What is wrong?
- When would you use buildpacks or Jib and not a Dockerfile?
- A small Python AI service has a 7 GB image. How do you find the cause and shrink it?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.