Skip to content

Comments

Remove restriction on anonymous read-only volumes#51682

Merged
vvoland merged 2 commits intomoby:masterfrom
srstsavage:45297-allow-anonymous-read-only-volumes
Dec 19, 2025
Merged

Remove restriction on anonymous read-only volumes#51682
vvoland merged 2 commits intomoby:masterfrom
srstsavage:45297-allow-anonymous-read-only-volumes

Conversation

@srstsavage
Copy link
Contributor

@srstsavage srstsavage commented Dec 11, 2025

Remove restriction on anonymous read-only volumes.

This restriction is currently preventing the use of pre-populated volumes that should be accessed in a read-only manner in a container (e.g. an NFS volume containing data to be processed or served).

According to @neersighted the restriction may have originally been put in place with the assumption that pre-populated volumes would be exposed as a named volume by the volume driver.

In practice, NFS volumes are mounted using the docker local driver by supplying driver opts. Example that fails when readonly is specified but works without:

docker run --rm -it \
 --mount 'readonly,type=volume,dst=/data/dest,volume-driver=local,volume-opt=type=nfs,volume-opt=device=:/export/some-share,"volume-opt=o=nfsvers=4,addr=some.server"' \
  debian

Fixes #45297

- What I did
Removed restriction on anonymous read-only volumes.

- How I did it
Deleted simple checks for anonymous read-only volumes from Linux and Windows volume parsers.

- How to verify it
After changes, read only mount an NFS share using something like

docker run --rm -it \
 --mount 'readonly,type=volume,dst=/data/dest,volume-driver=local,volume-opt=type=nfs,volume-opt=device=:/export/some-share,"volume-opt=o=nfsvers=4,addr=some.server"' \
  debian

- Human readable description for the release notes

Remove restriction on anonymous read-only volumes
image

@github-actions github-actions bot added area/volumes Volumes area/daemon Core Engine labels Dec 11, 2025
@srstsavage srstsavage force-pushed the 45297-allow-anonymous-read-only-volumes branch from 185e372 to 15f622a Compare December 11, 2025 04:30
@thaJeztah thaJeztah added kind/enhancement Enhancements are not bugs or new features but can improve usability or performance. impact/changelog labels Dec 11, 2025
@thaJeztah
Copy link
Member

Did a quick check if the read-only would potentially cause issues when propagating the content;

Created an image with content existing at the destination;

docker build -t foo -<<'EOF'
FROM alpine
RUN mkdir -p /data/dest && echo "hello" > /data/dest/file.txt
EOF
docker run --rm --mount type=volume,dst=/data/dest,readonly -w /data/dest foo sh -c 'ls -l && echo "foo" > new-file.txt'
total 4
-rw-r--r--    1 root     root             6 Dec 11 11:20 file.txt
sh: can't create new-file.txt: Read-only file system

Also checking --volumes-from;

docker run -dit --rm --name container1 --mount type=volume,dst=/data/dest,readonly -w /data/dest foo

docker run -it --volumes-from container1 -w /data/dest alpine sh -c 'ls -l && echo "foo" > new-file.txt'
total 4
-rw-r--r--    1 root     root             6 Dec 11 11:20 file.txt
sh: can't create new-file.txt: Read-only file system

There's one issue we should look at, and whether we want to support or not; the shorthand -v / --volume notation (<hostpath>:<containerpath>[:<option>...]) notation currently expects either 2 components (<hostpath>:<containerpath>), or three (<hostpath>:<containerpath>:<options>). Currently it fails if only 2 components are passed; likely it attempts to use ro as destination-path, and refusing it, because that's required to be an absolute path (orthogonal, but the error message could probably be improved to be more clear on that).

docker run -it --rm -v /data/dest:ro -w /data/dest foo
docker: Error response from daemon: invalid volume specification: '/data/dest:ro'

When specifying a source (myvolume), it does work;

docker run -it --rm -v myvolume:/data/dest:ro -w /data/dest foo

In general, it's OK for the advanced (--mount) flag to provide more features than the short-hand; we mostly froze the shorthand syntax, because the colon-separated format was problematic already (on Windows, the paths themselves would contain colons due to drive-letters (C:)), but in this case, I wonder if it's unexpected to not allow an option (ro / rw) that's otherwise supported by the shorthand.

That said; I guess that's already the case for other options, like SELinux labels (-v /data/dest:z), so maybe something we just need to document (use the advanced syntax for this)

docker run --rm -v myvolume:/data/dest:z -w /data/dest foo

docker run --rm -v data/dest:z -w /data/dest foo
docker: Error response from daemon: invalid volume specification: 'data/dest:nocopy'

@srstsavage srstsavage force-pushed the 45297-allow-anonymous-read-only-volumes branch from 15f622a to 9c1ac82 Compare December 11, 2025 15:04
@vvoland vvoland added this to the 29.2.0 milestone Dec 11, 2025
Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

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

LGTM

@thaJeztah
Copy link
Member

We should add some tests for this though before merging; let me know if you need help with that/

@srstsavage srstsavage force-pushed the 45297-allow-anonymous-read-only-volumes branch 2 times, most recently from 20d7b46 to 719ed69 Compare December 12, 2025 00:53
@srstsavage
Copy link
Contributor Author

Took a stab at some simple tests, adding Test*ValidateMounts to linux_parser_test.go and windows_parser_test.go. These tests just test mount.Mount validity. Let me know if I'm off track or if the tests should be expanded.

@vvoland
Copy link
Contributor

vvoland commented Dec 16, 2025

CI failure:

=== FAIL: daemon/volume/mounts TestLinuxValidateMounts (0.00s)
    linux_parser_test.go:336: assertion failed: error is not nil: invalid mount config for type "bind": bind source path does not exist: /tmp

@srstsavage
Copy link
Contributor Author

srstsavage commented Dec 17, 2025

@vvoland Really sorry if I'm just missing it (still getting used to the CI/test setup) but is there a chance that failure is from a previous version of the PR? I can't find that failure in the most recent results, and the test passes locally for me in the dev container:

root@9cf7f5178494:/go/src/github.com/docker/docker# TESTDIRS='./daemon/volume/mounts' TESTFLAGS='-test.run TestLinuxValidateMounts' ./hack/test/unit 2>&1 | tail -n 4
ok      github.com/moby/moby/v2/daemon/volume/mounts    (cached)        coverage: 9.4% of statements

DONE 1 tests in 0.000s
+ '[' -n '' ']'

At one point I had used a mockFiProvider in that test

        parser := NewLinuxParser()
+       if p, ok := parser.(*linuxParser); ok {
+               p.fi = mockFiProvider{}
+       }

and if I add that back it fails with the bind source path does not exist: /tmp message, but that mockFiProvider has been removed from TestLinuxValidateMounts and is not in the current PR.

@srstsavage srstsavage force-pushed the 45297-allow-anonymous-read-only-volumes branch from 719ed69 to 78d1637 Compare December 17, 2025 09:12
@thaJeztah
Copy link
Member

I gave CI a kick to run

@thaJeztah
Copy link
Member

One failure; looks like it may be related;

=== Failed
=== FAIL: daemon/volume/mounts TestLinuxValidateMounts (0.00s)
    linux_parser_test.go:336: assertion failed: error is not nil: invalid mount config for type "bind": bind source path does not exist: /tmp

Restriction on anonymouse read-only volumes is currently preventing
the use of pre-populated volumes that should be accessed in a read-only manner in a container
(e.g. an NFS volume containing data to be processed or served).

According to @neersighted the restriction may have originally been put
in place with the assumption that pre-populated volumes would be
exposed as a named volume by the volume driver.

In practice, NFS volumes are mounted using the docker `local` driver
by supplying driver opts. Example that fails when `readonly` is specified but works without:

```
docker run --rm -it \
 --mount 'readonly,type=volume,dst=/data/dest,volume-driver=local,volume-opt=type=nfs,volume-opt=device=:/export/some-share,"volume-opt=o=nfsvers=4,addr=some.server"' \
  debian
```

Fixes moby#45297

Signed-off-by: Shane St Savage <shane@axds.co>
@srstsavage srstsavage force-pushed the 45297-allow-anonymous-read-only-volumes branch from 78d1637 to beeacde Compare December 19, 2025 00:08
@srstsavage
Copy link
Contributor Author

Thanks very much for the review and suggestions. I applied all of them, confirmed that tests pass locally for me as well, rebased against master, and pushed the changes. 🤞 on CI passing this time...

@srstsavage
Copy link
Contributor Author

Seems likeTestLinuxValidateMounts needs to be configured to not run on Windows? Should I add something like below to the subtest logic, or is there a better way?

    if runtime.GOOS == "windows" {
      t.Skip("TODO: skip irrelevant Linux test on Windows")
    }

The test was failing on Windows because it used Unix-style paths and
relied on platform-specific filesystem behavior.

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
@vvoland vvoland force-pushed the 45297-allow-anonymous-read-only-volumes branch from 184c252 to 1175dd3 Compare December 19, 2025 11:43
@vvoland
Copy link
Contributor

vvoland commented Dec 19, 2025

I've adjusted the tests (one is skipped on Windows).

It's a bit of a workaround, because the parser itself shouldn't care if the source directory exists or not... but it's unrelated to this change so let's not try to fix it here.

Copy link
Contributor

@vvoland vvoland left a comment

Choose a reason for hiding this comment

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

LGTM, but since I pushed one commit, I would appreciate a second review 😅

Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

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

LGTM

@vvoland vvoland merged commit 3ccd05d into moby:master Dec 19, 2025
323 of 328 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/daemon Core Engine area/volumes Volumes impact/changelog kind/enhancement Enhancements are not bugs or new features but can improve usability or performance. status/4-merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Anonymous readonly volumes

3 participants