@@ -446,18 +446,48 @@ func (mgr *ContainerManager) Start(ctx context.Context, id, detachKeys string) (
446446}
447447
448448func (mgr * ContainerManager ) start (ctx context.Context , c * Container , detachKeys string ) error {
449+ var err error
450+
449451 c .Lock ()
450452 if c .Config == nil || c .State == nil {
451453 c .Unlock ()
452454 return errors .Wrapf (errtypes .ErrNotfound , "container %s" , c .ID )
453455 }
454456 c .DetachKeys = detachKeys
455457
458+ // attach volume
459+ for _ , mp := range c .Mounts {
460+ if mp .Name == "" {
461+ continue
462+ }
463+
464+ attachedVolumes := map [string ]struct {}{}
465+ defer func () {
466+ if err != nil {
467+ for name := range attachedVolumes {
468+ _ , err = mgr .VolumeMgr .Detach (ctx , name , map [string ]string {volumetypes .OptionRef : c .ID })
469+ if err != nil {
470+ logrus .Errorf ("failed to detach volume(%s) when start container(%s) rollback" ,
471+ name , c .ID )
472+ }
473+ }
474+ }
475+ }()
476+
477+ _ , err = mgr .VolumeMgr .Attach (ctx , mp .Name , map [string ]string {volumetypes .OptionRef : c .ID })
478+ if err != nil {
479+ c .Unlock ()
480+ return errors .Wrapf (err , "failed to attach volume(%s)" , mp .Name )
481+ }
482+ attachedVolumes [mp .Name ] = struct {}{}
483+ }
484+
456485 // initialise container network mode
457486 networkMode := c .HostConfig .NetworkMode
458487
459488 if IsContainer (networkMode ) {
460- origContainer , err := mgr .Get (ctx , strings .SplitN (networkMode , ":" , 2 )[1 ])
489+ var origContainer * Container
490+ origContainer , err = mgr .Get (ctx , strings .SplitN (networkMode , ":" , 2 )[1 ])
461491 if err != nil {
462492 c .Unlock ()
463493 return err
@@ -471,7 +501,8 @@ func (mgr *ContainerManager) start(ctx context.Context, c *Container, detachKeys
471501 } else {
472502 // initialise host network mode
473503 if IsHost (networkMode ) {
474- hostname , err := os .Hostname ()
504+ var hostname string
505+ hostname , err = os .Hostname ()
475506 if err != nil {
476507 c .Unlock ()
477508 return err
@@ -480,7 +511,7 @@ func (mgr *ContainerManager) start(ctx context.Context, c *Container, detachKeys
480511 }
481512
482513 // build the network related path.
483- if err : = mgr .buildNetworkRelatedPath (c ); err != nil {
514+ if err = mgr .buildNetworkRelatedPath (c ); err != nil {
484515 c .Unlock ()
485516 return err
486517 }
@@ -491,7 +522,7 @@ func (mgr *ContainerManager) start(ctx context.Context, c *Container, detachKeys
491522 endpoint := mgr .buildContainerEndpoint (c )
492523 endpoint .Name = name
493524 endpoint .EndpointConfig = endpointSetting
494- if _ , err : = mgr .NetworkMgr .EndpointCreate (ctx , endpoint ); err != nil {
525+ if _ , err = mgr .NetworkMgr .EndpointCreate (ctx , endpoint ); err != nil {
495526 logrus .Errorf ("failed to create endpoint: %v" , err )
496527 c .Unlock ()
497528 return err
@@ -501,7 +532,11 @@ func (mgr *ContainerManager) start(ctx context.Context, c *Container, detachKeys
501532 }
502533 c .Unlock ()
503534
504- return mgr .createContainerdContainer (ctx , c )
535+ if err = mgr .createContainerdContainer (ctx , c ); err != nil {
536+ return errors .Wrapf (err , "failed to create container(%s) on containerd" , c .ID )
537+ }
538+
539+ return nil
505540}
506541
507542// buildNetworkRelatedPath builds the network related path.
0 commit comments