Skip to content

Commit 352b03e

Browse files
committed
fix: return an error when docker host cannot be retrieved
1 parent b7f3ae3 commit 352b03e

4 files changed

Lines changed: 28 additions & 16 deletions

File tree

docker_client.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,17 @@ func (c *DockerClient) Info(ctx context.Context) (system.Info, error) {
7777
infoLabels += infoLabelsSb72.String()
7878
}
7979

80+
host, err := core.ExtractDockerHost(ctx)
81+
if err != nil {
82+
return dockerInfo, err
83+
}
8084
log.Printf(infoMessage, packagePath,
8185
dockerInfo.ServerVersion,
8286
c.ClientVersion(),
8387
dockerInfo.OperatingSystem, dockerInfo.MemTotal/1024/1024,
8488
infoLabels,
8589
internal.Version,
86-
core.MustExtractDockerHost(ctx),
90+
host,
8791
core.MustExtractDockerSocket(ctx),
8892
core.SessionID(),
8993
core.ProcessID(),

internal/core/client.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ import (
1212

1313
// NewClient returns a new docker client extracting the docker host from the different alternatives
1414
func NewClient(ctx context.Context, ops ...client.Opt) (*client.Client, error) {
15-
tcConfig := config.Read()
16-
17-
dockerHost := MustExtractDockerHost(ctx)
15+
dockerHost, err := ExtractDockerHost(ctx)
16+
if err != nil {
17+
return nil, err
18+
}
1819

20+
tcConfig := config.Read()
1921
opts := []client.Opt{client.FromEnv, client.WithAPIVersionNegotiation()}
2022
if dockerHost != "" {
2123
opts = append(opts, client.WithHost(dockerHost))

internal/core/docker_host.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ var (
3131
)
3232

3333
var (
34-
dockerHostCache string
35-
dockerHostOnce sync.Once
34+
dockerHostCache string
35+
dockerHostErrCache error
36+
dockerHostOnce sync.Once
3637
)
3738

3839
var (
@@ -85,16 +86,18 @@ var dockerHostCheck = func(ctx context.Context, host string) error {
8586
// 6. Rootless docker socket path.
8687
// 7. Else, because the Docker host is not set, it panics.
8788
func MustExtractDockerHost(ctx context.Context) string {
88-
dockerHostOnce.Do(func() {
89-
cache, err := extractDockerHost(ctx)
90-
if err != nil {
91-
panic(err)
92-
}
89+
host, err := ExtractDockerHost(ctx)
90+
if err != nil {
91+
panic(err)
92+
}
93+
return host
94+
}
9395

94-
dockerHostCache = cache
96+
func ExtractDockerHost(ctx context.Context) (string, error) {
97+
dockerHostOnce.Do(func() {
98+
dockerHostCache, dockerHostErrCache = extractDockerHost(ctx)
9599
})
96-
97-
return dockerHostCache
100+
return dockerHostCache, dockerHostErrCache
98101
}
99102

100103
// MustExtractDockerSocket Extracts the docker socket from the different alternatives, removing the socket schema and

provider.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,18 @@ func NewDockerProvider(provOpts ...DockerProviderOption) (*DockerProvider, error
141141
}
142142

143143
ctx := context.Background()
144+
host, err := core.ExtractDockerHost(ctx)
145+
if err != nil {
146+
return nil, err
147+
}
144148
c, err := NewDockerClientWithOpts(ctx)
145149
if err != nil {
146150
return nil, err
147151
}
148-
149152
return &DockerProvider{
150153
DockerProviderOptions: o,
151-
host: core.MustExtractDockerHost(ctx),
152154
client: c,
155+
host: host,
153156
config: config.Read(),
154157
}, nil
155158
}

0 commit comments

Comments
 (0)