diff --git a/commands/bake.go b/commands/bake.go index 53d2f6715e59..d9145a660567 100644 --- a/commands/bake.go +++ b/commands/bake.go @@ -145,7 +145,7 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions) (err error return nil } - resp, err := build.Build(ctx, dis, bo, dockerAPI(dockerCli), confutil.ConfigDir(dockerCli), printer) + resp, err := build.Build(ctx, dis, bo, dockerAPI(ctx, dockerCli), confutil.ConfigDir(dockerCli), printer) if err != nil { return wrapBuildError(err, true) } diff --git a/commands/build.go b/commands/build.go index 70a745b4090c..19025c0fa1f7 100644 --- a/commands/build.go +++ b/commands/build.go @@ -248,7 +248,7 @@ func buildTargets(ctx context.Context, dockerCli command.Cli, opts map[string]bu printer := progress.NewPrinter(ctx2, os.Stderr, os.Stderr, progressMode) - resp, err := build.Build(ctx, dis, opts, dockerAPI(dockerCli), confutil.ConfigDir(dockerCli), printer) + resp, err := build.Build(ctx, dis, opts, dockerAPI(ctx, dockerCli), confutil.ConfigDir(dockerCli), printer) err1 := printer.Wait() if err == nil { err = err1 diff --git a/commands/util.go b/commands/util.go index 9d90098e293b..539e42de1a14 100644 --- a/commands/util.go +++ b/commands/util.go @@ -69,7 +69,7 @@ func driversForNodeGroup(ctx context.Context, dockerCli command.Cli, ng *store.N } } else { ep := ng.Nodes[0].Endpoint - dockerapi, err := clientForEndpoint(dockerCli, ep) + dockerapi, err := clientForEndpoint(ctx, dockerCli, ep) if err != nil { return nil, err } @@ -96,7 +96,7 @@ func driversForNodeGroup(ctx context.Context, dockerCli command.Cli, ng *store.N dis[i] = di }() - dockerapi, err := clientForEndpoint(dockerCli, n.Endpoint) + dockerapi, err := clientForEndpoint(ctx, dockerCli, n.Endpoint) if err != nil { di.Err = err return nil @@ -170,7 +170,7 @@ func configFromContext(endpointName string, s ctxstore.Reader) (clientcmd.Client } // clientForEndpoint returns a docker client for an endpoint -func clientForEndpoint(dockerCli command.Cli, name string) (dockerclient.APIClient, error) { +func clientForEndpoint(ctx context.Context, dockerCli command.Cli, name string) (dockerclient.APIClient, error) { list, err := dockerCli.ContextStore().List() if err != nil { return nil, err @@ -193,7 +193,14 @@ func clientForEndpoint(dockerCli command.Cli, name string) (dockerclient.APIClie if err != nil { return nil, err } - return dockerclient.NewClientWithOpts(clientOpts...) + client, err := dockerclient.NewClientWithOpts(clientOpts...) + if err != nil { + return nil, err + } + if _, err = client.Ping(ctx); err != nil { + return nil, err + } + return client, nil } } @@ -399,11 +406,15 @@ func hasNodeGroup(list []*nginfo, ngi *nginfo) bool { return false } -func dockerAPI(dockerCli command.Cli) *api { - return &api{dockerCli: dockerCli} +func dockerAPI(ctx context.Context, dockerCli command.Cli) *api { + return &api{ + ctx: ctx, + dockerCli: dockerCli, + } } type api struct { + ctx context.Context dockerCli command.Cli } @@ -411,7 +422,7 @@ func (a *api) DockerAPI(name string) (dockerclient.APIClient, error) { if name == "" { name = a.dockerCli.CurrentContext() } - return clientForEndpoint(a.dockerCli, name) + return clientForEndpoint(a.ctx, a.dockerCli, name) } type dinfo struct {