Skip to content

Commit 09270dc

Browse files
committed
feat: automatically add socket's group when using --use-api-socket
1 parent 2518b52 commit 09270dc

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

cli/command/container/create.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,16 @@ func createContainer(ctx context.Context, dockerCLI command.Cli, containerCfg *c
257257
// hard-code engine socket path until https://github.com/moby/moby/pull/43459 gives us a discovery mechanism
258258
containerCfg.HostConfig.Mounts = append(containerCfg.HostConfig.Mounts, mount.Mount{
259259
Type: mount.TypeBind,
260-
Source: "/var/run/docker.sock",
261-
Target: "/var/run/docker.sock",
260+
Source: dockerSocketPath,
261+
Target: dockerSocketPath,
262262
BindOptions: &mount.BindOptions{},
263263
})
264264

265+
// Automatically add the socket's group so that non-root users (e.g.,
266+
// when using --user) can access the socket without needing an explicit
267+
// --group-add flag.
268+
addSocketGroup(&hostConfig.GroupAdd, dockerSocketPath)
269+
265270
/*
266271
267272
Ideally, we'd like to copy the config into a tmpfs but unfortunately,
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//go:build !windows
2+
3+
package container
4+
5+
import (
6+
"os"
7+
"strconv"
8+
"syscall"
9+
)
10+
11+
// addSocketGroup appends the GID of the socket file at path to groupAdd, so
12+
// non-root users can access the socket without an explicit --group-add flag.
13+
// Errors are silently ignored; this is best-effort.
14+
func addSocketGroup(groupAdd *[]string, path string) {
15+
fi, err := os.Stat(path)
16+
if err != nil {
17+
return
18+
}
19+
stat, ok := fi.Sys().(*syscall.Stat_t)
20+
if !ok {
21+
return
22+
}
23+
*groupAdd = append(*groupAdd, strconv.FormatUint(uint64(stat.Gid), 10))
24+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package container
2+
3+
// addSocketGroup is a no-op on Windows; the Windows engine is already rejected
4+
// earlier in createContainer via the OSType check.
5+
func addSocketGroup(_ *[]string, _ string) {}

0 commit comments

Comments
 (0)