Skip to content

Commit 5bae3d2

Browse files
authored
fix: return an error when docker host cannot be retrieved (#3613)
1 parent fc19484 commit 5bae3d2

4 files changed

Lines changed: 28 additions & 15 deletions

File tree

docker_client.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,17 @@ func (c *DockerClient) Info(ctx context.Context, options client.InfoOptions) (cl
7373
infoLabels += infoLabelsSb72.String()
7474
}
7575

76+
host, err := core.ExtractDockerHost(ctx)
77+
if err != nil {
78+
return dockerInfo, err
79+
}
7680
log.Printf(infoMessage, packagePath,
7781
dockerInfo.Info.ServerVersion,
7882
c.ClientVersion(),
7983
dockerInfo.Info.OperatingSystem, dockerInfo.Info.MemTotal/1024/1024,
8084
infoLabels,
8185
internal.Version,
82-
core.MustExtractDockerHost(ctx),
86+
host,
8387
core.MustExtractDockerSocket(ctx),
8488
core.SessionID(),
8589
core.ProcessID(),

internal/core/client.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +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()
15+
dockerHost, err := ExtractDockerHost(ctx)
16+
if err != nil {
17+
return nil, err
18+
}
1619

17-
dockerHost := MustExtractDockerHost(ctx)
20+
tcConfig := config.Read()
1821

1922
opts := []client.Opt{client.FromEnv}
2023
if 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)