@@ -51,7 +51,10 @@ func (s *composeService) down(ctx context.Context, projectName string, options a
51
51
}
52
52
53
53
if options .Project == nil {
54
- options .Project = s .projectFromContainerLabels (containers .filter (isNotOneOff ), projectName )
54
+ options .Project , err = s .projectFromLabels (ctx , containers .filter (isNotOneOff ), projectName )
55
+ if err != nil {
56
+ return err
57
+ }
55
58
}
56
59
57
60
if len (containers ) > 0 {
@@ -85,11 +88,7 @@ func (s *composeService) down(ctx context.Context, projectName string, options a
85
88
}
86
89
87
90
if options .Volumes {
88
- rm , err := s .ensureVolumesDown (ctx , projectName , w )
89
- if err != nil {
90
- return err
91
- }
92
- ops = append (ops , rm ... )
91
+ ops = append (ops , s .ensureVolumesDown (ctx , options .Project , w )... )
93
92
}
94
93
95
94
if ! resourceToRemove && len (ops ) == 0 {
@@ -103,19 +102,15 @@ func (s *composeService) down(ctx context.Context, projectName string, options a
103
102
return eg .Wait ()
104
103
}
105
104
106
- func (s * composeService ) ensureVolumesDown (ctx context.Context , projectName string , w progress.Writer ) ( []downOp , error ) {
105
+ func (s * composeService ) ensureVolumesDown (ctx context.Context , project * types. Project , w progress.Writer ) []downOp {
107
106
var ops []downOp
108
- volumes , err := s .apiClient .VolumeList (ctx , filters .NewArgs (projectFilter (projectName )))
109
- if err != nil {
110
- return ops , err
111
- }
112
- for _ , vol := range volumes .Volumes {
113
- id := vol .Name
107
+ for _ , vol := range project .Volumes {
108
+ volumeName := vol .Name
114
109
ops = append (ops , func () error {
115
- return s .removeVolume (ctx , id , w )
110
+ return s .removeVolume (ctx , volumeName , w )
116
111
})
117
112
}
118
- return ops , nil
113
+ return ops
119
114
}
120
115
121
116
func (s * composeService ) ensureImagesDown (ctx context.Context , projectName string , options api.DownOptions , w progress.Writer ) []downOp {
@@ -237,12 +232,13 @@ func (s *composeService) removeContainers(ctx context.Context, w progress.Writer
237
232
return eg .Wait ()
238
233
}
239
234
240
- func (s * composeService ) projectFromContainerLabels (containers Containers , projectName string ) * types.Project {
235
+ // projectFromLabels builds a types.Project based on actual resources with compose labels set
236
+ func (s * composeService ) projectFromLabels (ctx context.Context , containers Containers , projectName string ) (* types.Project , error ) {
241
237
project := & types.Project {
242
238
Name : projectName ,
243
239
}
244
240
if len (containers ) == 0 {
245
- return project
241
+ return project , nil
246
242
}
247
243
set := map [string ]moby.Container {}
248
244
for _ , c := range containers {
@@ -263,5 +259,20 @@ func (s *composeService) projectFromContainerLabels(containers Containers, proje
263
259
}
264
260
project .Services = append (project .Services , service )
265
261
}
266
- return project
262
+
263
+ volumes , err := s .apiClient .VolumeList (ctx , filters .NewArgs (projectFilter (projectName )))
264
+ if err != nil {
265
+ return nil , err
266
+ }
267
+
268
+ project .Volumes = types.Volumes {}
269
+ for _ , vol := range volumes .Volumes {
270
+ project.Volumes [vol.Labels [api.VolumeLabel ]] = types.VolumeConfig {
271
+ Name : vol .Name ,
272
+ Driver : vol .Driver ,
273
+ Labels : vol .Labels ,
274
+ }
275
+ }
276
+
277
+ return project , nil
267
278
}
0 commit comments