[Bug]: A file mount nested inside a directory mount replaces the parent share (directory mounts unaffected)
I have done the following
- [x] I have searched the existing issues
- [ ] If possible, I've reproduced the issue using the 'main' branch of this project
(Reproduced on the 1.2.2 release binary, not a main build.)
Steps to reproduce
Mounting a file at a path nested inside a directory mount causes the parent directory mount to be replaced rather than overlaid — every other file in the parent share disappears from the container.
mkdir -p /tmp/sess /tmp/group/subdir
echo PARENT-SHARE-CONTENT > /tmp/group/marker.txt
echo '{"nested":true}' > /tmp/group/config.json
echo sub > /tmp/group/subdir/inner.txt
# CONTROL — parent share only
container run --rm \
-v /tmp/sess:/workspace \
-v /tmp/group:/workspace/agent \
docker.io/library/alpine:latest ls -1 /workspace/agent
# TEST — identical, plus one nested FILE mount
container run --rm \
-v /tmp/sess:/workspace \
-v /tmp/group:/workspace/agent \
-v /tmp/group/config.json:/workspace/agent/config.json:ro \
docker.io/library/alpine:latest ls -1 /workspace/agent
Actual behavior
CONTROL: TEST:
config.json config.json
marker.txt
subdir
marker.txt and subdir/ are gone. The parent share has been replaced by the
single nested file.
Expected behavior
The nested file mount overlays that one path; the rest of the parent share stays visible. Docker's behavior on the identical three mounts (verified locally):
config.json
marker.txt
subdir
Boundary conditions
| Variant | Result |
|---|---|
Nested file mount, :ro |
Parent share replaced |
| Nested file mount, read-write | Parent share replaced (so :ro is not the trigger) |
| Nested directory mount | Correct — all entries present |
| Same three mounts under Docker | Correct — all entries present |
So this is specific to file mounts nested inside a directory mount, in either access mode.
Why this is easy to miss
We hit this in production and did not notice for weeks. The symptom is silent: the mount that does survive is the nested file, which is usually the file you were actively looking at — so spot-checks pass. What disappears is everything else in the parent directory. In our case a per-session config file mounted read-only inside an agent's workspace collapsed that entire workspace to the single config file, across every container on the host. No error, no warning, and the surviving file made it look healthy.
A warning at mount-resolution time when a file mount shadows an enclosing directory mount would have caught this immediately.
Environment
containerCLI 1.2.2 (release)- macOS 26 (Darwin 25.1.0), Apple Silicon (Mac16,9)
The reported collapse looks real. On current main, Parser.mount() rejects a bind source that is a file (path is not a directory), but shorthand Parser.volume() (-v host:container) only checks that the path exists and never uses the isDirectory result, so a nested file is passed through as a virtiofs share and replaces the parent directory share.
I can send a PR. Before coding: which behavior do you want?
- Reject file sources on
-vthe same way--mountalready does, with a clear error (and a test). Small, matches the existing parser. - Docker-style nested file overlay: the file mounts on top of the parent directory share and the rest of that share stays visible. Larger change.
I prefer (1) unless you want real file-mount overlay support. Happy to follow either.
Thanks for tracking this down — that matches what I saw.
One concern with option 1: --mount already rejects file sources, so if -v does too, there's no way left to bind a single file into a container. Top-level (non-nested) file binds via -v work fine today, and some consumers depend on them — we mount a CA certificate file into every container this way, so option 1 would break every spawn for us.
Option 2 would be ideal. If that's too large for now, a smaller fix that avoids the breakage: reject only file sources whose destination is nested inside another share (the case that actually collapses the parent), and keep top-level file binds as they are.
Some extra data points. The trigger seems to be the file's host parent folder, not where the file is mounted in the container. That matters for the fix discussed above: rejecting only file mounts whose destination is nested inside another mount would miss case C below and wrongly reject case D.
| Case | Mounts | Result |
|---|---|---|
| A | the same folder at two unrelated paths | both present |
| B | two files from the same folder, each at its own path | both present |
| C | a folder at /x, and one of its files at an unrelated /z/f |
/x is gone; only /z/f remains |
| D | a folder at /x, plus a file from a different folder at /x/g |
both present |
| E | a folder at /x, plus a file from its subfolder at /x/a/f |
both present |
So a directory share is dropped when a file whose direct parent is that same host folder is also mounted, wherever either one lands in the container. A grandparent (E) isn't affected. There is no error or warning in any case.
Reproduction:
d=$HOME/mount-test; rm -rf "$d"; mkdir -p "$d/a" "$d/other"
echo f > "$d/a/f"; echo s > "$d/a/s"; echo g > "$d/other/g"
img=docker.io/library/alpine:latest
echo "== A"; container run --rm -v "$d/a:/x" -v "$d/a:/y" $img sh -c 'ls /x /y'
echo "== B"; container run --rm -v "$d/a/f:/x/f" -v "$d/a/s:/y/s" $img sh -c 'cat /x/f /y/s'
echo "== C"; container run --rm -v "$d/a:/x" -v "$d/a/f:/z/f" $img sh -c 'ls /x; cat /z/f'
echo "== D"; container run --rm -v "$d/a:/x" -v "$d/other/g:/x/g" $img sh -c 'ls /x'
echo "== E"; container run --rm -v "$d:/x" -v "$d/a/f:/x/a/f" $img sh -c 'ls /x /x/a'
Output:
== A
/x:
f
s
/y:
f
s
== B
f
s
== C
ls: /x: No such file or directory
f
== D
f
g
s
== E
/x:
a
other
/x/a:
f
g
s
(The g under /x/a in E is an empty file left behind by D: the mount point for /x/g was created in the host folder.)
Environment: container CLI version 1.5.0, macOS 27.0.1, Apple M5.
Thanks @eenbroodje, that table pins it down. The trigger being the file's host parent folder matches how single-file mounts work in containerization.
virtiofs can only share directories, so FileMountContext.prepare shares the file's parent folder and later bind-mounts the file out of it. Every share is tagged with hashFilePath(source). In case C the holding share for a/f and the user's -v a:/x share both hash to hashFilePath(a), so they become one share. LinuxContainer and LinuxPod then drop every mount whose tag is in holdingDirectoryTags, and /x goes with it. D and E work because the file's parent is a different folder from the directory share.
So this is a bug, not a semantics question, and neither reject option is needed. The fix belongs in apple/containerization (Sources/Containerization/FileMount.swift and the filter in LinuxContainer.swift / LinuxPod.swift): only skip a holding tag when no user mount shares it, and bind the file out of the existing share. I'll open a PR there with a test for each of cases A–E and link it here.
Fix is up in https://github.com/apple/containerization/pull/990, with unit tests for cases A–E. Once that lands, picking up the new containerization release here fixes the mount.