@@ -77,7 +77,7 @@ type ContainerMgr interface {
7777 GetExecConfig (ctx context.Context , execid string ) (* ContainerExecConfig , error )
7878
7979 // Remove removes a container, it may be running or stopped and so on.
80- Remove (ctx context.Context , name string , option * ContainerRemoveOption ) error
80+ Remove (ctx context.Context , name string , option * types. ContainerRemoveOptions ) error
8181
8282 // Rename renames a container.
8383 Rename (ctx context.Context , oldName string , newName string ) error
@@ -209,20 +209,20 @@ func (mgr *ContainerManager) Restore(ctx context.Context) error {
209209}
210210
211211// Remove removes a container, it may be running or stopped and so on.
212- func (mgr * ContainerManager ) Remove (ctx context.Context , name string , option * ContainerRemoveOption ) error {
212+ func (mgr * ContainerManager ) Remove (ctx context.Context , name string , options * types. ContainerRemoveOptions ) error {
213213 c , err := mgr .container (name )
214214 if err != nil {
215215 return err
216216 }
217217 c .Lock ()
218218 defer c .Unlock ()
219219
220- if ! c .IsStopped () && ! c .IsExited () && ! c .IsCreated () && ! option .Force {
220+ if ! c .IsStopped () && ! c .IsExited () && ! c .IsCreated () && ! options .Force {
221221 return fmt .Errorf ("container: %s is not stopped, can't remove it without flag force" , c .ID ())
222222 }
223223
224224 // if the container is running, force to stop it.
225- if c .IsRunning () && option .Force {
225+ if c .IsRunning () && options .Force {
226226 msg , err := mgr .Client .DestroyContainer (ctx , c .ID (), c .StopTimeout ())
227227 if err != nil && ! errtypes .IsNotfound (err ) {
228228 return errors .Wrapf (err , "failed to destroy container: %s" , c .ID ())
@@ -232,7 +232,7 @@ func (mgr *ContainerManager) Remove(ctx context.Context, name string, option *Co
232232 }
233233 }
234234
235- if err := mgr .detachVolumes (ctx , c .meta ); err != nil {
235+ if err := mgr .detachVolumes (ctx , c .meta , options . Volumes ); err != nil {
236236 logrus .Errorf ("failed to detach volume: %v" , err )
237237 }
238238
@@ -1854,7 +1854,7 @@ func (mgr *ContainerManager) generateMountPoints(ctx context.Context, meta *Cont
18541854
18551855 defer func () {
18561856 if err != nil {
1857- if err := mgr .detachVolumes (ctx , meta ); err != nil {
1857+ if err := mgr .detachVolumes (ctx , meta , false ); err != nil {
18581858 logrus .Errorf ("failed to detach volume, err: %v" , err )
18591859 }
18601860 }
@@ -2235,7 +2235,7 @@ func (mgr *ContainerManager) setMountPointDiskQuota(ctx context.Context, c *Cont
22352235 return nil
22362236}
22372237
2238- func (mgr * ContainerManager ) detachVolumes (ctx context.Context , c * ContainerMeta ) error {
2238+ func (mgr * ContainerManager ) detachVolumes (ctx context.Context , c * ContainerMeta , remove bool ) error {
22392239 for _ , mount := range c .Mounts {
22402240 name := mount .Name
22412241 if name == "" {
@@ -2271,6 +2271,12 @@ func (mgr *ContainerManager) detachVolumes(ctx context.Context, c *ContainerMeta
22712271 }
22722272
22732273 mgr .VolumeMgr .Detach (ctx , name , option )
2274+
2275+ if remove {
2276+ if err := mgr .VolumeMgr .Remove (ctx , name ); err != nil && ! errtypes .IsUsing (err ) {
2277+ logrus .Warnf ("failed to remove volume: %s when remove container" , name )
2278+ }
2279+ }
22742280 }
22752281
22762282 return nil
0 commit comments