Skip to content

Commit 232d4bd

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 232d4bd

3 files changed

Lines changed: 38 additions & 4 deletions

File tree

cli/command/container/create.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ const (
3636
PullImageNever = "never"
3737
)
3838

39+
// dockerSocketPath is the path to the Docker Engine API socket.
40+
//
41+
// TODO(thaJeztah): hard-coded until https://github.com/moby/moby/pull/43459
42+
// provides a discovery mechanism.
43+
const dockerSocketPath = "/var/run/docker.sock"
44+
3945
type createOptions struct {
4046
name string
4147
platform string
@@ -254,13 +260,13 @@ func createContainer(ctx context.Context, dockerCLI command.Cli, containerCfg *c
254260
return "", errors.New("flag --use-api-socket can't be used with a Windows Docker Engine")
255261
}
256262

257-
// 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{
263+
hostConfig.Mounts = append(hostConfig.Mounts, mount.Mount{
259264
Type: mount.TypeBind,
260-
Source: "/var/run/docker.sock",
261-
Target: "/var/run/docker.sock",
265+
Source: dockerSocketPath,
266+
Target: dockerSocketPath,
262267
BindOptions: &mount.BindOptions{},
263268
})
269+
addSocketGroup(&hostConfig.GroupAdd, dockerSocketPath)
264270

265271
/*
266272
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)