Skip to content

Commit 6b4bef9

Browse files
committed
feat: automatically add socket's group when using --use-api-socket
Signed-off-by: Felipe Santos <felipecassiors@gmail.com>
1 parent 2518b52 commit 6b4bef9

3 files changed

Lines changed: 33 additions & 3 deletions

File tree

cli/command/container/create.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,14 @@ func createContainer(ctx context.Context, dockerCLI command.Cli, containerCfg *c
255255
}
256256

257257
// hard-code engine socket path until https://github.com/moby/moby/pull/43459 gives us a discovery mechanism
258-
containerCfg.HostConfig.Mounts = append(containerCfg.HostConfig.Mounts, mount.Mount{
258+
const dockerSocketPath = "/var/run/docker.sock"
259+
hostConfig.Mounts = append(hostConfig.Mounts, mount.Mount{
259260
Type: mount.TypeBind,
260-
Source: "/var/run/docker.sock",
261-
Target: "/var/run/docker.sock",
261+
Source: dockerSocketPath,
262+
Target: dockerSocketPath,
262263
BindOptions: &mount.BindOptions{},
263264
})
265+
addSocketGroup(&hostConfig.GroupAdd, dockerSocketPath)
264266

265267
/*
266268
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package container
2+
3+
// addSocketGroup is a no-op on Windows.
4+
func addSocketGroup(_ *[]string, _ string) {}

0 commit comments

Comments
 (0)