Skip to content

cli/command: Avoid panics after client initialization errors - #7280

Merged
thaJeztah merged 1 commit into
docker:masterfrom
vvoland:work-panic
Sep 3, 2026
Merged

cli/command: Avoid panics after client initialization errors#7280
thaJeztah merged 1 commit into
docker:masterfrom
vvoland:work-panic

Conversation

@vvoland

@vvoland vvoland commented Sep 3, 2026

Copy link
Copy Markdown
Collaborator

- What I did

When client.New rejects an invalid Docker host or other client option, it returns a nil concrete client with an error.

Returning that pointer directly as client.APIClient stores a typed-nil interface, so lazy version checks dereference it.

Return an error instead.

- How I did it

- How to verify it

- Human readable description for the release notes

Fix CLI panic when `DOCKER_HOST` or `-H` specifies an invalid host.

- A picture of a cute animal (not mandatory but encouraged)

When client.New rejects an invalid Docker host or other client option,
it returns a nil concrete client with an error.

Returning that pointer directly as client.APIClient stores a typed-nil
interface, so lazy version checks dereference it.

Return an error instead.

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
@vvoland vvoland added this to the 29.8.0 milestone Sep 3, 2026
@vvoland vvoland self-assigned this Sep 3, 2026
@vvoland
vvoland requested a review from thaJeztah September 3, 2026 09:01
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@thaJeztah

Copy link
Copy Markdown
Member

Hm.. nice one indeed. We should probably do some more fixes in this area after this.

Initially wasn't able to get it to panic, but it depends on what command you're running; this one reproduces indeed;

DOCKER_API_VERSION=1 DOCKER_HOST=:2375 docker version
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x58 pc=0x100a31280]

goroutine 1 [running]:
github.com/docker/cli/vendor/github.com/moby/moby/client.(*Client).ClientVersion(0x17cc1292c000?)
	/go/src/github.com/docker/cli/vendor/github.com/moby/moby/client/client.go:328
github.com/docker/cli/cli/command.(*DockerCli).CurrentVersion(0x17cc1292c000)
	/go/src/github.com/docker/cli/cli/command/cli.go:93 +0x34
github.com/docker/cli/cli/command/system.newClientVersion({0x101101511, 0x7}, {0x101f6c3f0, 0x17cc1292c000})
	/go/src/github.com/docker/cli/cli/command/system/version.go:126 +0x168
github.com/docker/cli/cli/command/system.runVersion({0x101f5da80, 0x17cc12918000}, {0x101f6c3f0, 0x17cc1292c000}, 0x101db8f40?)
	/go/src/github.com/docker/cli/cli/command/system/version.go:220 +0x5c
github.com/docker/cli/cli/command/system.newVersionCommand.func1(0x0?, {0x0?, 0x0?, 0xf059149abeac7925?})
	/go/src/github.com/docker/cli/cli/command/system/version.go:183 +0x2c
main.runDocker.(*DockerCli).InstrumentCobraCommands.func5.2(0x17cc12d65508, {0x10205d6e0, 0x0, 0x0})
	/go/src/github.com/docker/cli/cli/command/telemetry_utils.go:58 +0x60
github.com/docker/cli/vendor/github.com/spf13/cobra.(*Command).execute(0x17cc12d65508, {0x17cc12af70b0, 0x0, 0x0})
	/go/src/github.com/docker/cli/vendor/github.com/spf13/cobra/command.go:1015 +0x814
github.com/docker/cli/vendor/github.com/spf13/cobra.(*Command).ExecuteC(0x17cc12936008)
	/go/src/github.com/docker/cli/vendor/github.com/spf13/cobra/command.go:1148 +0x350
github.com/docker/cli/vendor/github.com/spf13/cobra.(*Command).Execute(...)
	/go/src/github.com/docker/cli/vendor/github.com/spf13/cobra/command.go:1071
github.com/docker/cli/vendor/github.com/spf13/cobra.(*Command).ExecuteContext(...)
	/go/src/github.com/docker/cli/vendor/github.com/spf13/cobra/command.go:1064
main.runDocker({0x101f5da80, 0x17cc12918000}, 0x17cc1292c000)
	/go/src/github.com/docker/cli/cmd/docker/docker.go:544 +0x76c
main.dockerMain({0x101f5d730?, 0x10205d6e0?})
	/go/src/github.com/docker/cli/cmd/docker/docker.go:93 +0x13c
main.main()
	/go/src/github.com/docker/cli/cmd/docker/docker.go:43 +0x2c

@thaJeztah thaJeztah left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread cli/command/cli.go
return newAPIClientFromEndpoint(endpoint, configFile, client.WithUserAgent(UserAgent()))
}

func newAPIClientFromEndpoint(ep docker.Endpoint, configFile *configfile.ConfigFile, extraOpts ...client.Opt) (client.APIClient, error) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one probably also could've keep a concrete type as return.

@thaJeztah
thaJeztah merged commit 7c88381 into docker:master Sep 3, 2026
108 of 109 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants