#22491: Use multi-stage docker build to reduce image size from 4.72GB -> 2.51GB.
docker
size: S
Cluster:
Bun Runtime and Docker Improvements
## Summary
```
openclaw slim 65fbc609ff68 22 minutes ago 2.51GB
openclaw local 6786c4c88b44 14 hours ago 4.72GB
```
Describe the problem and fix in 2–5 bullets:
### Problem:
Existing Docker image is huge (4.72GB).
### Why it matters:
Wasteful for network pulls of the Docker image.
### What changed:
- Dockerfile now uses a multi-stage build to avoid including redundant build tools in the final Dockerfile.
- Redundant `chown` layer removed, saving 1.7GB.
### What did NOT change (scope boundary):
Anything outside of the Dockerfile.
Note that this can probably be slimmed down even further, but I've opted only for the least aggressive optimizations that should bring no impact to the agent.
Note that there may be some use-cases I haven't thought of where `openclaw` needs devtools like `typescript`.
## Change Type (select all)
- [ ] Bug fix
- [ ] Feature
- [ ] Refactor
- [ ] Docs
- [ ] Security hardening
- [x] Chore/infra
## Scope (select all touched areas)
- [ ] Gateway / orchestration
- [ ] Skills / tool execution
- [ ] Auth / tokens
- [ ] Memory / storage
- [ ] Integrations
- [ ] API / contracts
- [ ] UI / DX
- [ ] CI/CD / infra
## Linked Issue/PR
- Closes #
- Related #
## User-visible / Behavior Changes
None.
## Security Impact (required)
- New permissions/capabilities? **NO**
- Secrets/tokens handling changed? **NO**
- New/changed network calls? **NO**
- Command/tool execution surface changed? **NO**
- Data access scope changed? **NO**
## Repro + Verification
```
docker build -t openclaw:slim .
```
### Environment
OS: Ubuntu 25.10
Runtime/container: Docker.
Model/provider: AMD x86 CPU.
## Evidence
Attach at least one:
- [ ] Failing test/log before + passing after
- [ ] Trace/log snippets
- [ ] Screenshot/recording
- [x] Perf numbers (if relevant)
```
openclaw slim 65fbc609ff68 22 minutes ago 2.51GB
openclaw local 6786c4c88b44 14 hours ago 4.72GB
```
## Human Verification (required)
Paired and sent a few messages in web-ui to verify basic functionality.
Verified plugins loaded via `plugin list`.
## Compatibility / Migration
- Backward compatible? **YES**
- Config/env changes? **NO**
- Migration needed? **NO**
## Failure Recovery (if this breaks)
Just revert to the old `Dockerfile`.
## Risks and Mitigations
None.
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Optimized Docker image size from 4.72GB to 2.51GB (47% reduction) using multi-stage builds. The builder stage compiles the application and prunes dev dependencies, then the runtime stage copies only the necessary production artifacts. Key improvements:
- Eliminated Bun installation (~102MB) from final image by restricting it to build stage
- Removed `chown -R` layer that duplicated file data (~1.7GB) by using `COPY --chown` instead
- Pruned dev dependencies like `typescript`, `vitest`, `rolldown`, `oxlint` (~237MB)
- Removed build-time artifacts (`src/`, `test/`, `docs/`, `scripts/`, `patches/`, config files)
- Consolidated `OPENCLAW_DOCKER_APT_PACKAGES` handling between stages to avoid duplication
<h3>Confidence Score: 4/5</h3>
- This PR is safe to merge with low risk - it's a well-structured Docker optimization that maintains functionality while significantly reducing image size.
- The multi-stage build is implemented correctly and follows Docker best practices. The author has verified basic functionality and plugin loading. The only minor concern is that `corepack` (used by `pnpm`) is enabled only in the builder stage but not in the runtime stage, which could cause issues if any runtime operations require `pnpm`. However, since the application uses `node openclaw.mjs` at runtime and all dependencies are pre-installed, this should not be an issue in normal operation.
- No files require special attention - the changes are isolated to the Dockerfile and follow standard multi-stage build patterns.
<sub>Last reviewed commit: 8998d35</sub>
<!-- greptile_other_comments_section -->
<sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#22050: Speed up docker cold start time by 6.3x.
by mm-zacharydavison · 2026-02-20
85.4%
#23313: feat(docker): add optional build-arg OPENCLAW_INSTALL_DOCKER_CLI to...
by zhuxuwei88-bot · 2026-02-22
83.4%
#7986: feature/OPENCLAW_IMAGE
by ozbillwang · 2026-02-03
80.1%
#11818: fix(docker): resolve build hang by using in-place ownership and opt...
by dilly · 2026-02-08
79.8%
#9190: feat(docker): Add autonomous container self-restart and runtime pac...
by alexdredmon · 2026-02-05
79.7%
#7133: feat: Automated Docker setup with environment-based configuration
by synetalsolutions · 2026-02-02
79.7%
#2609: bugfix: Docker build fails on main: .dockerignore excludes required...
by yanquankun · 2026-01-27
79.7%
#17151: fix: add npm link to fix CLI permission denied (exit 127)
by sskyu · 2026-02-15
79.6%
#12629: Dockerfile: pre-install common CLI tools for agent runtime
by jhs129 · 2026-02-09
79.5%
#8376: Docker: inline chown to reduce image size and build time
by bugparty · 2026-02-03
79.5%