github.com/docker/libcompose@v0.4.1-0.20210616120443-2a046c0bdbf2/project/project_down.go (about)

     1  package project
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"golang.org/x/net/context"
     7  
     8  	"github.com/docker/libcompose/project/events"
     9  	"github.com/docker/libcompose/project/options"
    10  )
    11  
    12  // Down stops the specified services and clean related containers (like docker stop + docker rm).
    13  func (p *Project) Down(ctx context.Context, opts options.Down, services ...string) error {
    14  	if !opts.RemoveImages.Valid() {
    15  		return fmt.Errorf("--rmi flag must be local, all or empty")
    16  	}
    17  	if err := p.Stop(ctx, 10, services...); err != nil {
    18  		return err
    19  	}
    20  	if opts.RemoveOrphans {
    21  		if err := p.runtime.RemoveOrphans(ctx, p.Name, p.ServiceConfigs); err != nil {
    22  			return err
    23  		}
    24  	}
    25  	if err := p.Delete(ctx, options.Delete{
    26  		RemoveVolume: opts.RemoveVolume,
    27  	}, services...); err != nil {
    28  		return err
    29  	}
    30  
    31  	networks, err := p.context.NetworksFactory.Create(p.Name, p.NetworkConfigs, p.ServiceConfigs, p.isNetworkEnabled())
    32  	if err != nil {
    33  		return err
    34  	}
    35  	if err := networks.Remove(ctx); err != nil {
    36  		return err
    37  	}
    38  
    39  	if opts.RemoveVolume {
    40  		volumes, err := p.context.VolumesFactory.Create(p.Name, p.VolumeConfigs, p.ServiceConfigs, p.isVolumeEnabled())
    41  		if err != nil {
    42  			return err
    43  		}
    44  		if err := volumes.Remove(ctx); err != nil {
    45  			return err
    46  		}
    47  	}
    48  
    49  	return p.forEach([]string{}, wrapperAction(func(wrapper *serviceWrapper, wrappers map[string]*serviceWrapper) {
    50  		wrapper.Do(wrappers, events.NoEvent, events.NoEvent, func(service Service) error {
    51  			return service.RemoveImage(ctx, opts.RemoveImages)
    52  		})
    53  	}), func(service Service) error {
    54  		return service.Create(ctx, options.Create{})
    55  	})
    56  }