Skip to content

Fix duplicate venv copy in server Dockerfile template - #1034

Open
ritvik412 wants to merge 10 commits into
huggingface:mainfrom
ritvik412:fix/1021-dockerfile-venv-double-copy
Open

ritvik412 wants to merge 10 commits into
huggingface:mainfrom
ritvik412:fix/1021-dockerfile-venv-double-copy

Conversation

@ritvik412

@ritvik412 ritvik412 commented Aug 2, 2026

Copy link
Copy Markdown

Fixes #1021

Problem

openenv init generates a Dockerfile whose final stage copies the built
virtualenv twice: once explicitly, and once again implicitly because uv sync
places .venv inside /app/env, and the next layer copies /app/env
wholesale — nesting a second full copy of the venv inside it. This roughly
doubles the image size (confirmed via dive in the issue: two ~523-524MB
layers for what should be one).

Fix

  • Move .venv out of /app/env in the builder stage, before the final
    stage's copies happen, so the wholesale COPY --from=builder /app/env /app/env no longer contains a nested venv.
  • Recreate a .venv symlink inside /app/env in the runtime stage (pointing
    at the single real copy at /app/.venv) so tooling that expects the venv
    alongside the project (e.g. uv run) still works, with no second physical
    copy on disk.
  • Applied identically to src/openenv/cli/templates/openenv_env/server/Dockerfile
    and .claude/skills/generate-openenv-env/assets/openenv_env_template/server/Dockerfile,
    since these two files were already kept byte-for-byte identical (aside from
    license header) and would otherwise drift.

Testing

Built a fresh env from the fixed template (openenv init + docker build) and
confirmed via docker history that the venv is now copied once, not twice:


Note

Low Risk
Build-only Dockerfile template changes; runtime behavior and paths (PATH, PYTHONPATH, uvicorn CMD) stay the same aside from smaller images.

Overview
Fixes duplicate virtualenv layers in the generated OpenEnv server Docker image by restructuring the builder/runtime copy steps.

In the builder stage, .venv is moved from /app/env/.venv to /app/.venv before the final stage copies /app/env, so the wholesale env copy no longer embeds a second full venv (~half the image size per #1021).

The runtime stage copies the venv once from /app/.venv, then adds a symlink at /app/env/.venv/app/.venv so uv run and similar tools still see a project-local venv without a second on-disk copy.

The same change is applied to both kept-in-sync templates: src/openenv/cli/templates/openenv_env/server/Dockerfile and the Claude skill asset template.

Reviewed by Cursor Bugbot for commit 084df12. Bugbot is set up for automated code reviews on this repo. Configure here.

openenv init generated Dockerfiles copy the built virtualenv twice:
once explicitly (COPY --from=builder /app/env/.venv /app/.venv) and
once implicitly, since uv sync places .venv inside /app/env and the
next layer copies /app/env wholesale (COPY --from=builder /app/env
/app/env). This roughly doubles the final image size (~523MB -> two
~524MB layers per dive output in huggingface#1021).

Fix: move .venv out of /app/env in the builder stage before the final
stage copies happen, then symlink /app/env/.venv -> /app/.venv in the
runtime stage so tooling that expects the venv alongside the project
(e.g. uv run) keeps working, without shipping a second physical copy.

Applied identically to both copies of the template (CLI init template
and the Claude Code skill's mirrored asset) since they were already
kept byte-for-byte identical apart from the license header.

Fixes huggingface#1021
@ritvik412 ritvik412 changed the title Fix duplicate venv copy in server Dockerfile template (#1021) Fix duplicate venv copy in server Dockerfile template Aug 2, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Stale comment

Reviewed at c2910b7562daf6738c51a3c3af40ef35e9c9eaf2: moving the builder venv before copying /app/env removes the duplicate layer, the runtime symlink preserves uv run, and both scaffold copies stay synchronized. No static defect or alignment issue found. Before merge, refresh onto current main, approve and pass exact-head CI, and retain functional evidence that a generated scaffold image builds and resolves /app/.venv through both PATH and /app/env/.venv; no required CI has run on this branch.

Open in Web View Automation 

Sent by Cursor Automation: Release

@cursor
cursor Bot requested a review from burtenshaw September 10, 2026 06:22
@burtenshaw

Copy link
Copy Markdown
Collaborator

cursor review

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

APPROVE at a930ded1

Correct fix for the latent double-copy: builder moves .venv to /app/.venv before the wholesale /app/env copy, then recreates /app/env/.venv → /app/.venv so uv run-style tooling still finds it. Both the CLI scaffold template and the generate-openenv-env skill asset are updated consistently.

Wheel cargo: yes — src/openenv/cli/templates/.../Dockerfile ships in the package. Worth a line in release notes when scaffolded images matter.

No Tier 1 issues. LGTM.

View PR

Open in Web View Automation 

Sent by Cursor Automation: Release

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

APPROVE at 6524358d (merge-main only)

Rebased onto main after #1178 (1e3f16d6). Feature tip unchanged from prior APPROVE at a930ded1 (Dockerfile template double-venv copy → single /app/.venv + symlink). No new conflicts in the template path.

Still needs exact-head repository CI / Approve-and-run on the fork. 0.6.0 package cargo if merged before Thursday — refresh draft #1211 after green CI.

View PR

Open in Web View Automation 

Sent by Cursor Automation: Release

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

APPROVE at 5c1004be (merge-main only)

Rebased onto main after #1219 docs merge (66eb946d). Feature fix unchanged: Dockerfile template still avoids the latent double-copy of .venv.

Package cargo if merged (would require #1211 refresh/TestPyPI). Fork may still need Approve-and-run for exact-head CI.

View PR

Open in Web View Automation 

Sent by Cursor Automation: Release

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Merge-main re-APPROVE at 1e5d951b

Ben merged main into this fork tip. Net feature diff unchanged: both scaffold Dockerfiles still mv /app/env/.venv /app/.venv then symlink back — removes the duplicated physical venv.

Fork PR: repository Tests/Package CI/lint will not run until Approve-and-run. Content review still holds from prior tip.

Package cargo if merged before Thursday (CLI template under src/) — would require refreshing draft #1211.

View PR

Open in Web View Automation 

Sent by Cursor Automation: Release

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Re-APPROVE at 465583f4 (merge-main only)

Ben merged main (26c9465e / #1179) into this fork PR. Stable feature patch-id is unchanged vs prior approved tip 1e5d951b (7fb20446…); both Dockerfile template blobs are identical.

Prior technical approval stands. Still needs maintainer Approve and run for repository CI. Package cargo if merged before Thursday — would require refreshing draft #1211 / TestPyPI again.

View PR

Open in Web View Automation 

Sent by Cursor Automation: Release

This branch has not been deployed

No deployments
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.

Default Dockerfile inefficient multi-stage build results in doubling size of python venv.

2 participants