Make writeable Volumes work for non root users
- Dominant language
- C#
- Stars
- 6.3k
- Forks
- 991
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 196
Description
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Is your feature request related to a problem? Please describe the problem.
Currently `.WithVolume()` only really works if your container runs as a root user. If your container runs as anythign other than root, you won't be able to write to the volume
```cs
var container = builder.AddContainer("sdk", "mcr.microsoft.com/dotnet/sdk:9.0")
//https://learn.microsoft.com/en-us/dotnet/core/compatibility/containers/8.0/app-user
.WithEntrypoint("sh")
.WithArgs("-c", """
echo "Creating file in /home/app/mount"
mkdir -p /home/app/mount/dir
echo "hello" >> /home/app/mount/file
echo "Creating file in /mnt/something"
mkdir -p /mnt/something/dir
echo "hello" >> /mnt/something/file
echo "Creating file in /tmp/something"
mkdir -p /tmp/something/dir
echo "hello" >> /tmp/something/file
echo "Done"
sleep 5000000
""".ReplaceLineEndings("\n"));
// Run as non root (user == app)
container.WithContainerRuntimeArgs("--user", "1654:1654");
// Create volumes
container
.WithVolume("/mnt/something")
.WithVolume("/tmp/something")
.WithVolume("/home/app/mount")
;
```
### Describe the solution you'd like
Ideally I'd like to see `WithVolume("/path")`, just work for non root users, being writeable by the container user (assuming readonly wasn't set to true). If not, some kind of overload to `WithVolume` so I can specify what permissions the volume should be created would be nice.
### Additional context
I have also experimented with the ContainerFiles api to see if that helps. This grants all the appropriate permissions if I don't use a volume mount, but volume mounts seem to ignore these permissions.
```cs
var mode = UnixFileMode.UserRead | UnixFileMode.GroupRead | UnixFileMode.OtherRead
| UnixFileMode.UserWrite | UnixFileMode.GroupWrite | UnixFileMode.OtherWrite;
container.WithContainerFiles("/", [
new ContainerDirectory{
Name = "mnt",
Entries = [
new ContainerDirectory{
Name = "something",
Owner = 1654,
Group = 1654,
Mode = mode,
Entries = [
new ContainerDirectory{
Name = "mount",
Owner = 1654,
Group = 1654,
Mode = mode }
]
}
]
}
])
;
```
Contributor guide
Assessment
This issue has not been assessed yet.