Skip to content

Commit a6335c4

Browse files
committed
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 <[email protected]>
1 parent 91d44d6 commit a6335c4

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

cli/command/container/client_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,7 @@ func (f *fakeClient) ContainerPause(ctx context.Context, containerID string, opt
232232

233233
return client.ContainerPauseResult{}, nil
234234
}
235+
236+
func (*fakeClient) Ping(_ context.Context, _ client.PingOptions) (client.PingResult, error) {
237+
return client.PingResult{}, nil
238+
}

cli/command/container/create.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,12 @@ func runCreate(ctx context.Context, dockerCli command.Cli, flags *pflag.FlagSet,
112112
}
113113
}
114114
copts.env = *opts.NewListOptsRef(&newEnv, nil)
115-
containerCfg, err := parse(flags, copts, dockerCli.ServerInfo().OSType)
115+
serverInfo, err := dockerCli.Client().Ping(ctx, client.PingOptions{})
116+
if err != nil {
117+
return err
118+
}
119+
120+
containerCfg, err := parse(flags, copts, serverInfo.OSType)
116121
if err != nil {
117122
return cli.StatusError{
118123
Status: withHelp(err, "create").Error(),

cli/command/container/run.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,12 @@ func runRun(ctx context.Context, dockerCli command.Cli, flags *pflag.FlagSet, ro
101101
}
102102
}
103103
copts.env = *opts.NewListOptsRef(&newEnv, nil)
104-
containerCfg, err := parse(flags, copts, dockerCli.ServerInfo().OSType)
104+
serverInfo, err := dockerCli.Client().Ping(ctx, client.PingOptions{})
105+
if err != nil {
106+
return err
107+
}
108+
109+
containerCfg, err := parse(flags, copts, serverInfo.OSType)
105110
// just in case the parse does not exit
106111
if err != nil {
107112
return cli.StatusError{

0 commit comments

Comments
 (0)