Skip to content

fix(config): resolve build.dockerfile against devcontainer.json, not the context - #156

Merged
bilby91 merged 1 commit into
mainfrom
fix/build-dockerfile-config-relative
Nov 19, 2026
Merged

bilby91 merged 1 commit into
mainfrom
fix/build-dockerfile-config-relative

Conversation

@bilby91

@bilby91 bilby91 commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Bug

Seen 2026-11-19 on k8-dev. The dcvm repo's .devcontainer/devcontainer.json:

"build": { "dockerfile": "Dockerfile", "context": ".." }

with the Dockerfile at .devcontainer/Dockerfile. VS Code and the reference CLI build .devcontainer/Dockerfile with the repo root as the context. v0.5.4 fails:

build base image from /workspace/repo: build failed: failed to read dockerfile: open Dockerfile: no such file or directory

Cause

config/resolve.go (determineSource, case hasBuild) joined Context with the config folder but passed Dockerfile through unchanged. runtime/docker/build.go sends the context as a tar and asks Docker for Dockerfile inside it, so Docker looked for <repo root>/Dockerfile. This only worked when the context was the config folder itself (the default ".").

Fix

The spec, and the reference CLI's getDockerfilePath, resolve build.dockerfile against the folder that holds devcontainer.json. The CLI then runs docker build -f <abs dockerfile> <context>.

  • config: BuildSource.Dockerfile is now an absolute host path (filepath.Join(configDir, …); an absolute path is kept as is). Both dockerfile and context are now resolved after variable substitution, as the reference CLI does. Before, "context": "${localWorkspaceFolder}" became <configDir>//home/u/proj. It's the same resolution code, so I fixed it here rather than leave it.
  • runtime/docker (locateDockerfile): BuildSpec.Dockerfile may be absolute.
    • Inside the context, it becomes the context-relative name in the tar.
    • Outside the context, it's appended to the context tar under a reserved name, .dockerfile.<sha256[:12]>. This is what docker build -f does: the docker CLI injects it as .dockerfile.<random>. I matched it rather than failing because the reference CLI accepts this layout (Docker handles it there), and appending a single tar entry is the smallest change that matches. The name uses a content hash instead of a random suffix so the tar stays reproducible (tarDirectory normalizes metadata for BuildKit cache hits).
    • prePullBaseImages now reads the resolved host path instead of context/dockerfile.

Other readers checked

  • useruid.go, build.go (ImageName retag) and the feature layer (up.go layerFeatures) write their own generated Dockerfile into a temp context and pass "Dockerfile". Relative names behave as before, so nothing changes there.
  • Compose services pass svc.Build.Dockerfile with their own context by Compose rules. Left alone: relative names behave as before.
  • No cache key or fingerprint hashes BuildSource.Dockerfile. The compose inputs hash and compose.ConfigHash cover compose services only, and the devcontainer ID doesn't include it.

Behaviour change

A config that relied on the bug (a Dockerfile at the context root, referenced as "Dockerfile" with "context": "..") must now use "../Dockerfile", as it already must for VS Code and the reference CLI. Five integration fixtures used that layout and are updated. Recorded in CHANGELOG under Unreleased/Fixed.

Tests

  • config: context: ".." + dockerfile: "Dockerfile" → .devcontainer/Dockerfile; default context; ../build/Dockerfile; absolute path; ${localWorkspaceFolder} in both fields.
  • runtime/docker: locateDockerfile (default, relative, absolute inside, absolute outside → injected with a stable name, missing outside → error); tarDirectory appends the extra entry with normalized metadata.
  • Integration (Docker):
    • TestBuildSource_WithLocalFeature now uses the dcvm layout and asserts the context is the repo root.
    • New TestBuildSource_DockerfileOutsideContext builds a Dockerfile that sits outside the context.
    • Both fail on main with the exact production error.

Local: make lint (0 issues) and make test pass. The affected integration tests pass. The full suite passes locally: 46 tests in 711s, run with -timeout=40m because the unsharded make test-integration exceeds its 10m limit here (CI shards it into 3 × 15m).

Follow-up

DAP pins v0.5.4. It will need a version bump to the next patch (v0.5.5) once this is released. No release tagged here.

🤖 Generated with Claude Code

…the context

The spec resolves build.dockerfile relative to the folder holding
devcontainer.json, like build.context. We passed it through unchanged
and the runtime asked Docker for it inside the context tar, so
{"dockerfile": "Dockerfile", "context": ".."} looked for <repo>/Dockerfile
and failed with "open Dockerfile: no such file or directory".

- config: BuildSource.Dockerfile is now an absolute host path. Both
  dockerfile and context are resolved after variable substitution, so
  ${localWorkspaceFolder}/... is no longer joined onto the config folder.
- runtime/docker: BuildSpec.Dockerfile may be absolute. Inside the
  context it becomes the context-relative name; outside it is appended
  to the context tar as .dockerfile.<content hash>, as `docker build -f`
  does for the reference CLI. prePullBaseImages reads the host path.
- tests: config resolution cases, locateDockerfile / tar injection unit
  tests, the dcvm layout and an outside-context build end to end.
  Integration fixtures that relied on the bug now use "../Dockerfile".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@getdapnextai getdapnextai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review — a330439 · CLEAN

Range: the whole pull request (6303809...a330439).

I found no concrete defect in the reviewed changes. The Dockerfile/context resolution and Docker build-tar adaptation are consistent with the reference CLI behavior; no surviving correctness, contract, or determinism issue was substantiated.

What I reviewed

Coverage: Reviewed the whole PR range, base 63038094d3ae17749b83f114aabd7e4a9fc9313a through head a3304397a1ba53178dc0befc62888c842218893a (one commit, 13 files). Read the governing R1–R8 directives at the base; .dap/code-review.yml is absent, so the repository-specific engineering directives and core dimensions governed. Checked source resolution and substitutions, build consumers, Docker tar generation and Docker SDK request handling, relevant reference CLI path resolution, and fixture/test changes. Verified R1/R2/R5/R6/R8 concerns; did not run tests or execute the system. Conditional state-integrity, redelivery, and version-compatibility do not apply.


CLEAN — this round found nothing. Reply in a finding's own thread to argue it. On the pull request I answer @getdapnextai review, pause and resume.

@bilby91
bilby91 merged commit 581dd29 into main Nov 19, 2026
10 checks passed
@bilby91
bilby91 deleted the fix/build-dockerfile-config-relative branch November 19, 2026 04:40
bilby91 added a commit that referenced this pull request Nov 19, 2026
* fix(build): bound outside-context Dockerfiles to the workspace

v0.5.5 (#156) reads a build.dockerfile outside the build context from
the host and injects it into the context tar. The path comes from an
untrusted devcontainer.json and nothing bounded it, so an absolute
path, ../ traversal, ${localEnv:...} or a symlink could name any file
the process can read; the daemon's parse error ("unknown instruction:
...") then echoes its first line into the build output.

- runtime/docker: locateDockerfile is the only host read of the
  Dockerfile. It resolves symlinks on both sides and reads a file
  outside the context only if it is a regular file of at most 1 MiB
  inside BuildSpec.WorkspaceRoot; an in-context file's host copy must
  resolve inside the context or the workspace. Otherwise it returns
  *runtime.DockerfileRefusedError, naming the configured path, before
  the context is tarred or the daemon is called. prePullBaseImages
  takes the contents it read instead of reading the path again.
- runtime: BuildSpec.WorkspaceRoot; empty allows nothing outside the
  context. The engine sets it to LocalWorkspaceFolder for the
  devcontainer build and compose service builds.
- config: Resolve rejects, lexically, a build.dockerfile outside both
  the workspace and the context with *ConfigInvalidError.
- In-context Dockerfiles and the #156 layout (.devcontainer/Dockerfile
  with "context": "..") build as in v0.5.5.
- Tests: refusal cases in both packages, a no-daemon BuildImage test,
  and an integration test with a hostile config that asserts through a
  recording socket proxy that no build or pull reaches the daemon and
  that the secret appears in no event or error.

Refs #156, crunchloop/dap#8967

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(build): read the bounded Dockerfile through os.Root

The bound was checked on a resolved pathname and the file then opened
by pathname again, so a directory on the way swapped for a symlink in
between could carry the read outside the workspace; SameFile only
compared the stat and the open, which both followed the swap.

readDockerfile now opens os.Root on the resolved root and stats,
opens and reads the file through it, so no symlink or ".." can leave
the root whatever the tree does after the check. Any failure to open
it there, after the check found it, is a refusal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(build): confine in-context Dockerfile reads to the workspace root

os.OpenRoot opens the root itself by path. For a Dockerfile inside the
context, the context was tried first as the root; a context inside the
workspace is a folder the repository controls, so it could be swapped
for a symlink between the check and the open. Try the workspace root
first: its own path lies outside what the repository controls. The
context remains the root only for a context outside the workspace.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: bilby91 <2201079+bilby91@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant