@@ -77,6 +77,7 @@ type DockerCli struct {
7777 dockerEndpoint docker.Endpoint
7878 contextStoreConfig * store.Config
7979 initTimeout time.Duration
80+ userAgent string
8081 res telemetryResource
8182
8283 // baseCtx is the base context used for internal operations. In the future
@@ -312,10 +313,10 @@ func NewAPIClientFromFlags(opts *cliflags.ClientOptions, configFile *configfile.
312313 if err != nil {
313314 return nil , fmt .Errorf ("unable to resolve docker endpoint: %w" , err )
314315 }
315- return newAPIClientFromEndpoint (endpoint , configFile )
316+ return newAPIClientFromEndpoint (endpoint , configFile , client . WithUserAgent ( UserAgent ()) )
316317}
317318
318- func newAPIClientFromEndpoint (ep docker.Endpoint , configFile * configfile.ConfigFile ) (client.APIClient , error ) {
319+ func newAPIClientFromEndpoint (ep docker.Endpoint , configFile * configfile.ConfigFile , extraOpts ... client. Opt ) (client.APIClient , error ) {
319320 opts , err := ep .ClientOpts ()
320321 if err != nil {
321322 return nil , err
@@ -330,7 +331,7 @@ func newAPIClientFromEndpoint(ep docker.Endpoint, configFile *configfile.ConfigF
330331 if withCustomHeaders != nil {
331332 opts = append (opts , withCustomHeaders )
332333 }
333- opts = append (opts , client . WithUserAgent ( UserAgent ()) )
334+ opts = append (opts , extraOpts ... )
334335 return client .NewClientWithOpts (opts ... )
335336}
336337
@@ -551,7 +552,8 @@ func (cli *DockerCli) initialize() error {
551552 return
552553 }
553554 if cli .client == nil {
554- if cli .client , cli .initErr = newAPIClientFromEndpoint (cli .dockerEndpoint , cli .configFile ); cli .initErr != nil {
555+ ops := []client.Opt {client .WithUserAgent (cli .userAgent )}
556+ if cli .client , cli .initErr = newAPIClientFromEndpoint (cli .dockerEndpoint , cli .configFile , ops ... ); cli .initErr != nil {
555557 return
556558 }
557559 }
@@ -598,6 +600,7 @@ func NewDockerCli(ops ...CLIOption) (*DockerCli, error) {
598600 WithContentTrustFromEnv (),
599601 WithDefaultContextStoreConfig (),
600602 WithStandardStreams (),
603+ WithUserAgent (UserAgent ()),
601604 }
602605 ops = append (defaultOps , ops ... )
603606
@@ -621,7 +624,7 @@ func getServerHost(hosts []string, defaultToTLS bool) (string, error) {
621624 }
622625}
623626
624- // UserAgent returns the user agent string used for making API requests
627+ // UserAgent returns the default user agent string used for making API requests.
625628func UserAgent () string {
626629 return "Docker-Client/" + version .Version + " (" + runtime .GOOS + ")"
627630}
0 commit comments