@@ -22,6 +22,7 @@ import (
2222 "github.com/docker/cli/templates"
2323 "github.com/docker/docker/api/types/swarm"
2424 "github.com/docker/docker/api/types/system"
25+ "github.com/docker/docker/client"
2526 "github.com/docker/docker/registry"
2627 "github.com/docker/go-units"
2728 "github.com/spf13/cobra"
@@ -97,30 +98,46 @@ func runInfo(ctx context.Context, cmd *cobra.Command, dockerCli command.Cli, opt
9798 info .ClientErrors = append (info .ClientErrors , err .Error ())
9899 }
99100
101+ var serverConnErr error
100102 if needsServerInfo (opts .format , info ) {
101- if dinfo , err := dockerCli .Client ().Info (ctx ); err == nil {
102- info .Info = & dinfo
103- } else {
103+ serverConnErr = addServerInfo (ctx , dockerCli , opts .format , & info )
104+ }
105+
106+ if opts .format == "" {
107+ info .UserName = dockerCli .ConfigFile ().AuthConfigs [registry .IndexServer ].Username
108+ info .ClientInfo .APIVersion = dockerCli .CurrentVersion ()
109+ return errors .Join (prettyPrintInfo (dockerCli , info ), serverConnErr )
110+ }
111+
112+ return errors .Join (serverConnErr , formatInfo (dockerCli .Out (), info , opts .format ))
113+ }
114+
115+ // addServerInfo retrieves the server information and adds it to the dockerInfo struct.
116+ // if a connection error occurs, it will be returned as an error.
117+ // other errors are appended to the info.ServerErrors field.
118+ func addServerInfo (ctx context.Context , dockerCli command.Cli , format string , info * dockerInfo ) error {
119+ dinfo , err := dockerCli .Client ().Info (ctx )
120+ if err != nil {
121+ // if no format is provided and we have an error, don't print the server info
122+ if format == "" {
123+ info .Info = nil
124+ }
125+ if ! client .IsErrConnectionFailed (err ) {
104126 info .ServerErrors = append (info .ServerErrors , err .Error ())
105- if opts .format == "" {
106- // reset the server info to prevent printing "empty" Server info
107- // and warnings, but don't reset it if a custom format was specified
108- // to prevent errors from Go's template parsing during format.
109- info .Info = nil
110- } else {
127+ if format != "" {
111128 // if a format is provided, print the error, as it may be hidden
112- // otherwise if the template doesn't include the ServerErrors field.
129+ // if the format template doesn't include the ServerErrors field.
113130 fprintln (dockerCli .Err (), err )
114131 }
132+ return nil
115133 }
134+ // on a server connection error, we want to return an error
135+ return err
116136 }
117137
118- if opts .format == "" {
119- info .UserName = dockerCli .ConfigFile ().AuthConfigs [registry .IndexServer ].Username
120- info .ClientInfo .APIVersion = dockerCli .CurrentVersion ()
121- return prettyPrintInfo (dockerCli , info )
122- }
123- return formatInfo (dockerCli .Out (), info , opts .format )
138+ // only assign the server info if we have no error
139+ info .Info = & dinfo
140+ return nil
124141}
125142
126143// placeHolders does a rudimentary match for possible placeholders in a
0 commit comments