Skip to content

fix(docker): use multi-stage build to remove build-essential from runtime image#1846

Open
yangzheli wants to merge 3 commits intobytedance:mainfrom
yangzheli:fix/dockerfile-multistage-build
Open

fix(docker): use multi-stage build to remove build-essential from runtime image#1846
yangzheli wants to merge 3 commits intobytedance:mainfrom
yangzheli:fix/dockerfile-multistage-build

Conversation

@yangzheli
Copy link
Copy Markdown
Contributor

@yangzheli yangzheli commented Apr 4, 2026

part of #1086

Summary

The build-essential toolchain was only needed for compiling native Python extensions during uv sync but remained in the final image, increasing size and attack surface. Split the Dockerfile into a builder stage (with build-essential) and a clean runtime stage that copies only the compiled artifacts, Node.js, Docker CLI, and uv.

Screenshots

Before After
image-before image-after

…time image

The build-essential toolchain (~200 MB) was only needed for compiling
native Python extensions during `uv sync` but remained in the final
image, increasing size and attack surface. Split the Dockerfile into
a builder stage (with build-essential) and a clean runtime stage that
copies only the compiled artifacts, Node.js, Docker CLI, and uv.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the backend Docker image into a multi-stage build so native Python extensions can be compiled in a builder stage while keeping the final runtime image smaller and with fewer build-time tools installed.

Changes:

  • Split backend/Dockerfile into builder and runtime stages, compiling Python deps in the builder stage.
  • Copy only the built backend artifacts/venv plus required tooling (uv, Node.js runtime, Docker CLI) into the runtime stage.

Address Copilot review comments:
- Add a `dev` build stage (FROM builder) that retains build-essential
  so startup-time `uv sync` in dev containers can compile from source
- Update docker-compose-dev.yaml to use `target: dev` for gateway and
  langgraph services
- Keep the clean runtime stage (no build-essential) as the default
  final stage for production builds

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@yangzheli
Copy link
Copy Markdown
Contributor Author

Copilot's suggestion has been adopted in 32e5bde. Please take another look when you have time.@WillemJiang

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants