MCPcopy
hub / github.com/docker/compose / Remove

Method Remove

pkg/compose/remove.go:30–96  ·  pkg/compose/remove.go::composeService.Remove
(ctx context.Context, projectName string, options api.RemoveOptions)

Source from the content-addressed store, hash-verified

28)
29
30func (s *composeService) Remove(ctx context.Context, projectName string, options api.RemoveOptions) error { //nolint:gocyclo
31 projectName = strings.ToLower(projectName)
32
33 if options.Stop {
34 err := s.Stop(ctx, projectName, api.StopOptions{
35 Services: options.Services,
36 Project: options.Project,
37 })
38 if err != nil {
39 return err
40 }
41 }
42
43 containers, err := s.getContainers(ctx, projectName, oneOffExclude, true, options.Services...)
44 if err != nil {
45 if api.IsNotFoundError(err) {
46 _, _ = fmt.Fprintln(s.stderr(), "No stopped containers")
47 return nil
48 }
49 return err
50 }
51
52 if options.Project != nil {
53 containers = containers.filter(isService(options.Project.ServiceNames()...))
54 }
55
56 var stoppedContainers Containers
57 for _, ctr := range containers {
58 // We have to inspect containers, as State reported by getContainers suffers a race condition
59 inspected, err := s.apiClient().ContainerInspect(ctx, ctr.ID, client.ContainerInspectOptions{})
60 if api.IsNotFoundError(err) {
61 // Already removed. Maybe configured with auto-remove
62 continue
63 }
64 if err != nil {
65 return err
66 }
67 if !inspected.Container.State.Running || (options.Stop && s.dryRun) {
68 stoppedContainers = append(stoppedContainers, ctr)
69 }
70 }
71
72 var names []string
73 for _, c := range stoppedContainers {
74 names = append(names, getCanonicalContainerName(c))
75 }
76
77 if len(names) == 0 {
78 return api.ErrNoResources
79 }
80
81 msg := fmt.Sprintf("Going to remove %s", strings.Join(names, ", "))
82 if options.Force {
83 _, _ = fmt.Fprintln(s.stdout(), msg)
84 } else {
85 confirm, err := s.prompt(msg, false)
86 if err != nil {
87 return err

Callers

nothing calls this directly

Calls 12

StopMethod · 0.95
getContainersMethod · 0.95
stderrMethod · 0.95
apiClientMethod · 0.95
stdoutMethod · 0.95
removeMethod · 0.95
IsNotFoundErrorFunction · 0.92
isServiceFunction · 0.85
RunFunction · 0.85
filterMethod · 0.80
ContainerInspectMethod · 0.45

Tested by

no test coverage detected