Skip to content

Match docker build-context UX for sandbox Image builds#775

Merged
cooleel merged 8 commits into
mainfrom
sw/fix_image_build_sdk
Jun 27, 2026
Merged

Match docker build-context UX for sandbox Image builds#775
cooleel merged 8 commits into
mainfrom
sw/fix_image_build_sdk

Conversation

@cooleel

@cooleel cooleel commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Summary

Align Image.build() / createSandboxImage() with docker build <context>:

  • When context_dir is given, upload that directory as-is.
  • When omitted and the image reads host files (COPY/ADD host sources, or aRUN --mount=type=bind reading the context), fail fast asking forcontext_dir instead of silently archiving the cwd.
  • When omitted and the image reads no host files, upload an empty context (just the generated Dockerfile).

Bind-mount detection covers the BuildKit default (type=bind, no from=); from= mounts and cache/tmpfs/secret/ssh mounts don't require a context.

Why

The old path always uploaded the cwd, leaking unrelated files into the build context and surprising users. The new behavior is explicit and matches Docker.

Changes

  • Python + TypeScript SDK kept in sync (sandbox_builder.py, sandbox-image.ts).
  • Tests added for COPY/ADD, remote ADD, --from stage, and RUN bind/cache mounts.

Versions

Python SDK → 0.5.51, TypeScript SDK → 0.5.50.

cooleel and others added 4 commits June 26, 2026 17:50
Image objects render their Dockerfile from text and don't copy host
files by default, so there's no build context to upload. Previously an
Image build with no explicit context_dir defaulted to cwd, archiving and
uploading the entire working directory (which has no Dockerfile).

Add _image_context_dir() to resolve the build context: use the explicit
context_dir when given, otherwise a throwaway empty temp dir cleaned up
via an ExitStack. Apply this in build_sandbox_image and
build_sandbox_application_image, and update docstrings to note that
context_dir should be passed explicitly only when the image copies local
files.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
When an Image build omits context_dir, assemble a minimal build context
from only the files the COPY/ADD ops reference (resolved relative to cwd)
instead of uploading the whole working directory.

Fix two regressions in the staging path:
- Honor .dockerignore: carry cwd's root .dockerignore into the staged
  context so the Rust archiver applies the same exclusions it would for a
  full-cwd context (staged files keep their cwd-relative paths). Python and
  TypeScript SDKs.
- Preserve symlink source paths (Python): compute the staged path from the
  lexically-normalized source rather than the symlink-resolved target, so a
  COPY whose source is a symlink stages at the path the Dockerfile names.
  TypeScript now dereferences staged sources for parity.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The comment claimed contextDir is required when an Image has COPY/ADD ops
and that omitting it throws. The implementation instead assembles a minimal
build context from the referenced sources (matching the Python SDK), so the
doc now describes that behavior.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
cooleel and others added 2 commits June 26, 2026 20:53
Image builds now mirror `docker build <context>`: when context_dir is
given it is uploaded as-is and COPY/ADD sources resolve relative to it.
When omitted, an image with no host-file COPY/ADD ops gets an empty
context (just the generated Dockerfile) so cwd is not archived; an image
that does read host files via COPY/ADD fails fast with a clear message
telling the caller to pass context_dir.

Removes the auto-staging layer (glob/symlink/.dockerignore handling) that
diverged from Docker semantics. Scoped to the SDK Image build path only;
the application-image path always receives context_dir from deploy and is
reverted to its original cwd-based resolution. Applied to both the Python
and TypeScript SDKs.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
A BuildKit `RUN --mount=type=bind` (the default mount type) reads from
the build context, but `_image_requires_context` only inspected COPY/ADD
ops. An image whose only host-file access was such a RUN mount fell
through to the empty-context path and silently mounted nothing, breaking
builds that worked when the cwd was always uploaded.

Detect context-reading RUN bind mounts (type=bind, no `from=`) alongside
COPY/ADD so those builds fail fast asking for `context_dir`. Mirror the
fix in the TypeScript SDK and generalize the error message.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@cooleel cooleel changed the title Stage a minimal Image build context instead of uploading cwd Match docker build-context UX for sandbox Image builds Jun 26, 2026
@cooleel cooleel marked this pull request as ready for review June 26, 2026 13:33
@cooleel cooleel merged commit d08fb86 into main Jun 27, 2026
36 of 38 checks passed
@cooleel cooleel deleted the sw/fix_image_build_sdk branch June 27, 2026 08:23
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