Safeguard
Security Guides•

What does the final Docker stage need for Node.js, Python, Java and Go?

A side by side guide to what belongs in the runtime stage for each language and what should stay in the build stage, with a table you can use in reviews.

Safeguard Security Team
4 min read

The final stage needs the runtime, your built artifact, its runtime libraries and a non-root user, and nothing else. Compilers, package managers, test tools and source code stay in the build stage. What counts as the artifact differs: a jar for Java, a binary for Go, a virtual environment for Python, node_modules plus built output for Node.js.

The side by side table

Node.jsPythonJavaGo
Build stage hasFull Node image, npm, compiler for native add-onsCompilers and headers if wheels are missingJDK and build toolGo toolchain
Artifact to copynode_modules and built outputVirtual environment and app codeJar, or extracted layersOne static binary
Runtime in final stageNode binaryPython interpreterJRE or custom runtimeNone if static
Package manager in final stageAvoidAvoidNot neededNot needed
Common base choicesslim, alpine, distrolessslim, distrolessJRE image, distrolessscratch, distroless static
Non-root usernode user, uid 1000 in official imagesCreate one, numeric USERCreate one, numeric USERnonroot tag or numeric USER
Entry pointnode, exec formpython, exec formjava, exec formthe binary

Each column is covered in detail elsewhere in this series. See the posts on Node.js images without npm, pip install failures on slim images, JRE or JDK final images and static Go images.

Rules that hold in every language

  1. Use multi-stage builds. Docker's documentation describes multi-stage builds as a way to keep build tools out of the final image.
  2. Pin base images. Tags are mutable, and Docker's guidance describes pinning to a digest for a guaranteed image, then updating tags and digests on a schedule.
  3. Rebuild often. The same guidance recommends rebuilding images to pick up fresh base layers.
  4. Use a .dockerignore file so local files, caches and credentials do not enter the build context.
  5. Run as a non-root user and verify it with the id command.
  6. Use exec form for CMD and ENTRYPOINT so signals reach the app.

Review prompts for a pull request

Use these in code review when a Dockerfile changes:

  • Does the final stage start from a different image than the builder?
  • Is the base pinned, and is there a job that refreshes it?
  • Does the final image contain a compiler or package manager? Why?
  • Which user does it run as?
  • Does anything copy the whole build context into the final image?
  • Are secrets passed through build arguments or environment variables?

What a table cannot tell you

The right base image depends on your dependencies. A Python service with a compiled data library may need glibc, which moves you off alpine. A Java service that loads classes at run time may need more than a jlink runtime includes. Test the final image with your real integration tests, not only a hello world.

Try it yourself

Open the container image scan demo and look at which findings come from the base image and which come from application packages. The table above helps you decide which of the two you can shrink by changing the final stage.

Click through it at your own pace, or open it full screen.

Where Safeguard fits

Safeguard scans dependencies in all four ecosystems, builds SBOMs, uses reachability analysis to rank findings and lets you set policy gates for pull requests. The free scan is a first step.

Quick check

Is a multi-stage build enough to make an image secure?

No. It keeps build tools out of the final image, but dependencies, user settings and the base image still need attention.

Should every language use distroless?

Not necessarily. It suits many services, but if your team needs a shell for operations or your dependencies need libraries outside it, a slim image with a clear patch routine is reasonable.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.