@@ -339,8 +339,7 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa
339339 cOpts = append (cOpts , lCOpts ... )
340340
341341 var containerNameStore namestore.NameStore
342- if options .Name == "" && ! options .NameChanged {
343- // Automatically set the container name, unless `--name=""` was explicitly specified.
342+ if options .Name == "" {
344343 var imageRef string
345344 if ensuredImage != nil {
346345 imageRef = ensuredImage .Ref
@@ -352,15 +351,15 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa
352351 }
353352 options .Name = parsedReference .SuggestContainerName (id )
354353 }
355- if options .Name != "" {
356- containerNameStore , err = namestore .New (dataStore , options .GOptions .Namespace )
357- if err != nil {
358- return nil , generateRemoveOrphanedDirsFunc (ctx , id , dataStore , internalLabels ), err
359- }
360- if err := containerNameStore .Acquire (options .Name , id ); err != nil {
361- return nil , generateRemoveOrphanedDirsFunc (ctx , id , dataStore , internalLabels ), err
362- }
354+
355+ containerNameStore , err = namestore .New (dataStore , options .GOptions .Namespace )
356+ if err != nil {
357+ return nil , generateRemoveOrphanedDirsFunc (ctx , id , dataStore , internalLabels ), err
363358 }
359+ if err := containerNameStore .Acquire (options .Name , id ); err != nil {
360+ return nil , generateRemoveOrphanedDirsFunc (ctx , id , dataStore , internalLabels ), err
361+ }
362+
364363 internalLabels .name = options .Name
365364 internalLabels .pidFile = options .PidFile
366365
@@ -714,9 +713,7 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO
714713 var hostConfigLabel dockercompat.HostConfigLabel
715714 var dnsSettings dockercompat.DNSSettings
716715 m [labels .Namespace ] = internalLabels .namespace
717- if internalLabels .name != "" {
718- m [labels .Name ] = internalLabels .name
719- }
716+ m [labels .Name ] = internalLabels .name
720717 m [labels .Hostname ] = internalLabels .hostname
721718 m [labels .Domainname ] = internalLabels .domainname
722719 extraHostsJSON , err := json .Marshal (internalLabels .extraHosts )
@@ -1024,15 +1021,13 @@ func generateGcFunc(ctx context.Context, container containerd.Container, ns, id,
10241021 log .G (ctx ).WithError (rmErr ).Warnf ("failed to remove container %q state dir %q" , id , internalLabels .stateDir )
10251022 }
10261023
1027- if name != "" {
1028- var errE error
1029- if containerNameStore , errE = namestore .New (dataStore , ns ); errE != nil {
1030- log .G (ctx ).WithError (errE ).Warnf ("failed to instantiate container name store during cleanup for container %q" , id )
1031- }
1032- // Double-releasing may happen with containers started with --rm, so, ignore NotFound errors
1033- if errE := containerNameStore .Release (name , id ); errE != nil && ! errors .Is (errE , store .ErrNotFound ) {
1034- log .G (ctx ).WithError (errE ).Warnf ("failed to release container name store for container %q (%s)" , name , id )
1035- }
1024+ var errE error
1025+ if containerNameStore , errE = namestore .New (dataStore , ns ); errE != nil {
1026+ log .G (ctx ).WithError (errE ).Warnf ("failed to instantiate container name store during cleanup for container %q" , id )
1027+ }
1028+ // Double-releasing may happen with containers started with --rm, so, ignore NotFound errors
1029+ if errE := containerNameStore .Release (name , id ); errE != nil && ! errors .Is (errE , store .ErrNotFound ) {
1030+ log .G (ctx ).WithError (errE ).Warnf ("failed to release container name store for container %q (%s)" , name , id )
10361031 }
10371032 }
10381033}
0 commit comments