@@ -24,7 +24,6 @@ import (
24
24
25
25
"github.com/compose-spec/compose-go/types"
26
26
moby "github.com/docker/docker/api/types"
27
- "github.com/docker/docker/api/types/filters"
28
27
"github.com/docker/docker/errdefs"
29
28
"golang.org/x/sync/errgroup"
30
29
@@ -41,7 +40,6 @@ func (s *composeService) Down(ctx context.Context, projectName string, options a
41
40
}
42
41
43
42
func (s * composeService ) down (ctx context.Context , projectName string , options api.DownOptions ) error {
44
- builtFromResources := options .Project == nil
45
43
w := progress .ContextWriter (ctx )
46
44
resourceToRemove := false
47
45
@@ -51,8 +49,9 @@ func (s *composeService) down(ctx context.Context, projectName string, options a
51
49
return err
52
50
}
53
51
54
- if builtFromResources {
55
- options .Project , err = s .getProjectWithVolumes (ctx , containers , projectName )
52
+ project := options .Project
53
+ if project == nil {
54
+ project , err = s .getProjectWithResources (ctx , containers , projectName )
56
55
if err != nil {
57
56
return err
58
57
}
@@ -62,7 +61,7 @@ func (s *composeService) down(ctx context.Context, projectName string, options a
62
61
resourceToRemove = true
63
62
}
64
63
65
- err = InReverseDependencyOrder (ctx , options . Project , func (c context.Context , service string ) error {
64
+ err = InReverseDependencyOrder (ctx , project , func (c context.Context , service string ) error {
66
65
serviceContainers := containers .filter (isService (service ))
67
66
err := s .removeContainers (ctx , w , serviceContainers , options .Timeout , options .Volumes )
68
67
return err
@@ -71,25 +70,22 @@ func (s *composeService) down(ctx context.Context, projectName string, options a
71
70
return err
72
71
}
73
72
74
- orphans := containers .filter (isNotService (options . Project .ServiceNames ()... ))
73
+ orphans := containers .filter (isNotService (project .ServiceNames ()... ))
75
74
if options .RemoveOrphans && len (orphans ) > 0 {
76
75
err := s .removeContainers (ctx , w , orphans , options .Timeout , false )
77
76
if err != nil {
78
77
return err
79
78
}
80
79
}
81
80
82
- ops , err := s .ensureNetworksDown (ctx , projectName )
83
- if err != nil {
84
- return err
85
- }
81
+ ops := s .ensureNetworksDown (ctx , project , w )
86
82
87
83
if options .Images != "" {
88
84
ops = append (ops , s .ensureImagesDown (ctx , projectName , options , w )... )
89
85
}
90
86
91
87
if options .Volumes {
92
- ops = append (ops , s .ensureVolumesDown (ctx , options . Project , w )... )
88
+ ops = append (ops , s .ensureVolumesDown (ctx , project , w )... )
93
89
}
94
90
95
91
if ! resourceToRemove && len (ops ) == 0 {
@@ -106,6 +102,9 @@ func (s *composeService) down(ctx context.Context, projectName string, options a
106
102
func (s * composeService ) ensureVolumesDown (ctx context.Context , project * types.Project , w progress.Writer ) []downOp {
107
103
var ops []downOp
108
104
for _ , vol := range project .Volumes {
105
+ if vol .External .External {
106
+ continue
107
+ }
109
108
volumeName := vol .Name
110
109
ops = append (ops , func () error {
111
110
return s .removeVolume (ctx , volumeName , w )
@@ -125,20 +124,18 @@ func (s *composeService) ensureImagesDown(ctx context.Context, projectName strin
125
124
return ops
126
125
}
127
126
128
- func (s * composeService ) ensureNetworksDown (ctx context.Context , projectName string ) ( []downOp , error ) {
127
+ func (s * composeService ) ensureNetworksDown (ctx context.Context , project * types. Project , w progress. Writer ) []downOp {
129
128
var ops []downOp
130
- networks , err := s .apiClient ().NetworkList (ctx , moby.NetworkListOptions {Filters : filters .NewArgs (projectFilter (projectName ))})
131
- if err != nil {
132
- return ops , err
133
- }
134
- for _ , n := range networks {
135
- networkID := n .ID
129
+ for _ , n := range project .Networks {
130
+ if n .External .External {
131
+ continue
132
+ }
136
133
networkName := n .Name
137
134
ops = append (ops , func () error {
138
- return s .removeNetwork (ctx , networkID , networkName )
135
+ return s .removeNetwork (ctx , networkName , w )
139
136
})
140
137
}
141
- return ops , nil
138
+ return ops
142
139
}
143
140
144
141
func (s * composeService ) getServiceImages (options api.DownOptions , projectName string ) map [string ]struct {} {
@@ -233,21 +230,20 @@ func (s *composeService) removeContainers(ctx context.Context, w progress.Writer
233
230
return eg .Wait ()
234
231
}
235
232
236
- func (s * composeService ) getProjectWithVolumes (ctx context.Context , containers Containers , projectName string ) (* types.Project , error ) {
233
+ func (s * composeService ) getProjectWithResources (ctx context.Context , containers Containers , projectName string ) (* types.Project , error ) {
237
234
containers = containers .filter (isNotOneOff )
238
235
project , _ := s .projectFromName (containers , projectName )
239
- volumes , err := s .apiClient ().VolumeList (ctx , filters .NewArgs (projectFilter (projectName )))
236
+
237
+ volumes , err := s .actualVolumes (ctx , projectName )
240
238
if err != nil {
241
239
return nil , err
242
240
}
241
+ project .Volumes = volumes
243
242
244
- project .Volumes = types.Volumes {}
245
- for _ , vol := range volumes .Volumes {
246
- project.Volumes [vol.Labels [api.VolumeLabel ]] = types.VolumeConfig {
247
- Name : vol .Name ,
248
- Driver : vol .Driver ,
249
- Labels : vol .Labels ,
250
- }
243
+ networks , err := s .actualNetworks (ctx , projectName )
244
+ if err != nil {
245
+ return nil , err
251
246
}
247
+ project .Networks = networks
252
248
return project , nil
253
249
}
0 commit comments