@@ -78,6 +78,7 @@ type DockerCli struct {
7878 dockerEndpoint docker.Endpoint
7979 contextStoreConfig * store.Config
8080 initTimeout time.Duration
81+ userAgent string
8182 res telemetryResource
8283
8384 // baseCtx is the base context used for internal operations. In the future
@@ -313,10 +314,10 @@ func NewAPIClientFromFlags(opts *cliflags.ClientOptions, configFile *configfile.
313314 if err != nil {
314315 return nil , errors .Wrap (err , "unable to resolve docker endpoint" )
315316 }
316- return newAPIClientFromEndpoint (endpoint , configFile )
317+ return newAPIClientFromEndpoint (endpoint , configFile , client . WithUserAgent ( UserAgent ()) )
317318}
318319
319- func newAPIClientFromEndpoint (ep docker.Endpoint , configFile * configfile.ConfigFile ) (client.APIClient , error ) {
320+ func newAPIClientFromEndpoint (ep docker.Endpoint , configFile * configfile.ConfigFile , extraOpts ... client. Opt ) (client.APIClient , error ) {
320321 opts , err := ep .ClientOpts ()
321322 if err != nil {
322323 return nil , err
@@ -331,7 +332,7 @@ func newAPIClientFromEndpoint(ep docker.Endpoint, configFile *configfile.ConfigF
331332 if withCustomHeaders != nil {
332333 opts = append (opts , withCustomHeaders )
333334 }
334- opts = append (opts , client . WithUserAgent ( UserAgent ()) )
335+ opts = append (opts , extraOpts ... )
335336 return client .NewClientWithOpts (opts ... )
336337}
337338
@@ -552,7 +553,8 @@ func (cli *DockerCli) initialize() error {
552553 return
553554 }
554555 if cli .client == nil {
555- if cli .client , cli .initErr = newAPIClientFromEndpoint (cli .dockerEndpoint , cli .configFile ); cli .initErr != nil {
556+ ops := []client.Opt {client .WithUserAgent (cli .userAgent )}
557+ if cli .client , cli .initErr = newAPIClientFromEndpoint (cli .dockerEndpoint , cli .configFile , ops ... ); cli .initErr != nil {
556558 return
557559 }
558560 }
@@ -599,6 +601,7 @@ func NewDockerCli(ops ...CLIOption) (*DockerCli, error) {
599601 WithContentTrustFromEnv (),
600602 WithDefaultContextStoreConfig (),
601603 WithStandardStreams (),
604+ WithUserAgent (UserAgent ()),
602605 }
603606 ops = append (defaultOps , ops ... )
604607
@@ -622,7 +625,7 @@ func getServerHost(hosts []string, defaultToTLS bool) (string, error) {
622625 }
623626}
624627
625- // UserAgent returns the user agent string used for making API requests
628+ // UserAgent returns the default user agent string used for making API requests.
626629func UserAgent () string {
627630 return "Docker-Client/" + version .Version + " (" + runtime .GOOS + ")"
628631}
0 commit comments