Skip to content

core: mount a set of files read-only - #14872

Merged
sipsma merged 2 commits into
mainfrom
sipsma/engine-mounted-files
Oct 27, 2026
Merged

sipsma merged 2 commits into
mainfrom
sipsma/engine-mounted-files

Conversation

@sipsma

@sipsma sipsma commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

This adds Container.withMountedFiles(path, sources). It mounts a list of files read-only in a directory at path, each under the base name of its own path, without first copying them into a new directory.

Why

Mounting several files together took a directory made for them. Directory.withFiles creates a snapshot, links the files into it, commits it, and gives it an owner lease and a cache result, all for a directory that's mounted once. If the client hasn't asked for that directory yet, it also costs the client one more request for its ID before the exec's own request.

A Go build that compiles each package in its own exec mounts each package's imports this way, so it pays this for every package. In a cold build of dagger's CLI, that's about 9.6 ms of engine time before each compile on the critical path, and 9–10 s of engine time over the build.

What it does

The container holds the files as dependencies, the way it holds a volume. When an exec starts, the engine:

  1. makes a directory under its root, on the snapshots' filesystem;
  2. links each file into it, with the same copier Directory.withFiles uses, so the files are hard links where the filesystem allows;
  3. binds the directory read-only into the container;
  4. removes the directory when the exec's mounts are released.

There's no snapshot, owner lease or cache result for the set.

When the engine starts, it removes directories left by execs it didn't finish, in the same start-up cleanup that removes the executor's leftover hosts and resolv.conf. These directories have to live on the snapshots' filesystem for hard links, rather than in the engine's temporary directory like other per-exec scratch space. Leftovers would otherwise keep deleted snapshots' files on disk.

Semantics

  • Always read-only. The files are hard links to the sources' own files, so the exec can't create, change or delete anything under the path.
  • Names: each file appears under the base name of its path, as with Directory.withFiles.
  • Two files with the same name are an error. withFiles lets the later replace the earlier; for a mount, that would hide a wrong input.
  • A file that doesn't exist fails the call with that file's error.
  • An empty list mounts an empty read-only directory.
  • The container's root is an error, however it's spelled: a few read-only files can't be a root filesystem.
  • Paths under the mount can't be read back with Container.directory or Container.file, as with cache and tmpfs mounts. The files are the ones passed in.
  • An exec's cache key includes each file and its name.

What it changes for users

  • A new API, in every SDK.
  • Faster builds that use it. A cold build of dagger's CLI with a Go module that mounts each package's imports with it, against the same engine with withFiles:
withFiles + mounted directory withMountedFiles
First build (s) 34.29, 34.08, 34.45 32.09, 33.48, 34.36
Engine CPU (s) 64.2, 63.3, 64.7 53.6, 54.9, 57.8
Rebuild after an edit (s) 11.37, 11.11, 10.97 9.94, 10.01, 10.59
Second build in the same engine (s) 55.37, 55.94, 55.09 53.99, 53.86, 55.03

AWS c8gd.4xlarge (arm64, 16 cores), one fresh dev engine per build, 3 interleaved builds per arm. The module's two versions differed only in that one call. Top-level requests per first build fell from 5,170 to 3,837, the directories' ID requests.

  • No change for code that doesn't use it.

Tests

TestWithMountedFiles (core/integration, TestContainer) checks:

  • names and contents;
  • that writes, new files and removals all fail;
  • an empty list;
  • the root and another spelling of it;
  • duplicate names;
  • a missing file;
  • that reading a path back is refused;
  • that the same mount gives a cached exec.

It runs in CI's test-split/test-container check.

Rebased onto main after #14871, with one conflict in core/integration/container_test.go: both sides added a test at the same place, and both are kept unchanged. Rebased again after #14875, with no conflicts.

@sipsma
sipsma requested review from a team as code owners October 27, 2026 06:14
@sipsma
sipsma force-pushed the sipsma/engine-mounted-files branch from 302cfd9 to da3c914 Compare October 27, 2026 07:30
Container.withMountedFiles mounts a list of files read-only in a directory
at a path, each under the base name of its own path. Two files with the
same name are an error, and so is the container's root: a few read-only
files can't be a root filesystem. Paths under the mount can't be read back
from the container, as for cache and tmpfs mounts.

Mounting files together took a directory made for them: Directory.withFiles
creates a snapshot, links the files into it, commits it, and gives it an
owner lease and a cache result, all to be mounted once. A Go build that
mounts each package's imports this way pays that for every package.

A files mount makes none of it. The container holds the files as
dependencies, as it holds a volume, and the exec links them into a
directory of its own, on the snapshots' filesystem so the links are hard
links, and binds that directory read-only. The engine removes directories
left by execs it didn't finish when it starts, with the executor's other
leftovers.

Signed-off-by: Erik Sipsma <[email protected]>
Regenerated with go test ./core/schema -run TestBaseSchemaAllowlist -update.

Signed-off-by: Erik Sipsma <[email protected]>
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