From a6335c42269662413cd2038a274b7776f5bdb730 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 11 Dec 2025 15:59:33 +0100 Subject: [PATCH] docker run, create: don't swallow connection errors during validate Some validation steps done by `docker create` (and `docker run`) are platform- specific, and need to know the daemon's OS. To get this information, the CLI.ServerInfo() method was used, which discards connection errors, resulting in an empty OS, which causes validation to fail with an "unknown server OS" error message. This patch changes it to use the Client.Ping so that we can error when failing to connect. We should look if we can reduce the platform-specific validation and parsing on the client-side, but at least this change should produce a more useful error. Before this patch: DOCKER_HOST=tcp://example.invalid docker run -it --rm --device=/dev/dri alpine docker: unknown server OS: Run 'docker run --help' for more information With this patch: DOCKER_HOST=tcp://example.invalid docker run -it --rm --device=/dev/dri alpine failed to connect to the docker API at tcp://example.invalid:2375: lookup example.invalid on 192.168.65.7:53: no such host Signed-off-by: Sebastiaan van Stijn --- cli/command/container/client_test.go | 4 ++++ cli/command/container/create.go | 7 ++++++- cli/command/container/run.go | 7 ++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/cli/command/container/client_test.go b/cli/command/container/client_test.go index fe46be39329f..9b66f713cb59 100644 --- a/cli/command/container/client_test.go +++ b/cli/command/container/client_test.go @@ -232,3 +232,7 @@ func (f *fakeClient) ContainerPause(ctx context.Context, containerID string, opt return client.ContainerPauseResult{}, nil } + +func (*fakeClient) Ping(_ context.Context, _ client.PingOptions) (client.PingResult, error) { + return client.PingResult{}, nil +} diff --git a/cli/command/container/create.go b/cli/command/container/create.go index a58eeaa16d2e..36c6cfe6f8d3 100644 --- a/cli/command/container/create.go +++ b/cli/command/container/create.go @@ -112,7 +112,12 @@ func runCreate(ctx context.Context, dockerCli command.Cli, flags *pflag.FlagSet, } } copts.env = *opts.NewListOptsRef(&newEnv, nil) - containerCfg, err := parse(flags, copts, dockerCli.ServerInfo().OSType) + serverInfo, err := dockerCli.Client().Ping(ctx, client.PingOptions{}) + if err != nil { + return err + } + + containerCfg, err := parse(flags, copts, serverInfo.OSType) if err != nil { return cli.StatusError{ Status: withHelp(err, "create").Error(), diff --git a/cli/command/container/run.go b/cli/command/container/run.go index 2d75ec765b06..8eef49b1771d 100644 --- a/cli/command/container/run.go +++ b/cli/command/container/run.go @@ -101,7 +101,12 @@ func runRun(ctx context.Context, dockerCli command.Cli, flags *pflag.FlagSet, ro } } copts.env = *opts.NewListOptsRef(&newEnv, nil) - containerCfg, err := parse(flags, copts, dockerCli.ServerInfo().OSType) + serverInfo, err := dockerCli.Client().Ping(ctx, client.PingOptions{}) + if err != nil { + return err + } + + containerCfg, err := parse(flags, copts, serverInfo.OSType) // just in case the parse does not exit if err != nil { return cli.StatusError{