Skip to content

Commit bfd7428

Browse files
author
Ulysses Souza
authored
Merge pull request docker#9074 from ndeloof/down_volumes
only remove volumes set by compose file
2 parents 37f763f + feba34e commit bfd7428

File tree

3 files changed

+40
-22
lines changed

3 files changed

+40
-22
lines changed

pkg/compose/down.go

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ func (s *composeService) down(ctx context.Context, projectName string, options a
5151
}
5252

5353
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+
}
5558
}
5659

5760
if len(containers) > 0 {
@@ -85,11 +88,7 @@ func (s *composeService) down(ctx context.Context, projectName string, options a
8588
}
8689

8790
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)...)
9392
}
9493

9594
if !resourceToRemove && len(ops) == 0 {
@@ -103,19 +102,15 @@ func (s *composeService) down(ctx context.Context, projectName string, options a
103102
return eg.Wait()
104103
}
105104

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 {
107106
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
114109
ops = append(ops, func() error {
115-
return s.removeVolume(ctx, id, w)
110+
return s.removeVolume(ctx, volumeName, w)
116111
})
117112
}
118-
return ops, nil
113+
return ops
119114
}
120115

121116
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
237232
return eg.Wait()
238233
}
239234

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) {
241237
project := &types.Project{
242238
Name: projectName,
243239
}
244240
if len(containers) == 0 {
245-
return project
241+
return project, nil
246242
}
247243
set := map[string]moby.Container{}
248244
for _, c := range containers {
@@ -263,5 +259,20 @@ func (s *composeService) projectFromContainerLabels(containers Containers, proje
263259
}
264260
project.Services = append(project.Services, service)
265261
}
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
267278
}

pkg/compose/down_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ func TestDown(t *testing.T) {
4444
testContainer("service2", "789", false),
4545
testContainer("service_orphan", "321", true),
4646
}, nil)
47+
api.EXPECT().VolumeList(gomock.Any(), filters.NewArgs(projectFilter(strings.ToLower(testProject)))).
48+
Return(volume.VolumeListOKBody{}, nil)
4749

4850
api.EXPECT().ContainerStop(gomock.Any(), "123", nil).Return(nil)
4951
api.EXPECT().ContainerStop(gomock.Any(), "456", nil).Return(nil)
@@ -74,6 +76,8 @@ func TestDownRemoveOrphans(t *testing.T) {
7476
testContainer("service2", "789", false),
7577
testContainer("service_orphan", "321", true),
7678
}, nil)
79+
api.EXPECT().VolumeList(gomock.Any(), filters.NewArgs(projectFilter(strings.ToLower(testProject)))).
80+
Return(volume.VolumeListOKBody{}, nil)
7781

7882
api.EXPECT().ContainerStop(gomock.Any(), "123", nil).Return(nil)
7983
api.EXPECT().ContainerStop(gomock.Any(), "789", nil).Return(nil)
@@ -100,13 +104,16 @@ func TestDownRemoveVolumes(t *testing.T) {
100104

101105
api.EXPECT().ContainerList(gomock.Any(), projectFilterListOpt()).Return(
102106
[]moby.Container{testContainer("service1", "123", false)}, nil)
107+
api.EXPECT().VolumeList(gomock.Any(), filters.NewArgs(projectFilter(strings.ToLower(testProject)))).
108+
Return(volume.VolumeListOKBody{
109+
Volumes: []*moby.Volume{{Name: "myProject_volume"}},
110+
}, nil)
103111

104112
api.EXPECT().ContainerStop(gomock.Any(), "123", nil).Return(nil)
105113
api.EXPECT().ContainerRemove(gomock.Any(), "123", moby.ContainerRemoveOptions{Force: true, RemoveVolumes: true}).Return(nil)
106114

107115
api.EXPECT().NetworkList(gomock.Any(), moby.NetworkListOptions{Filters: filters.NewArgs(projectFilter(strings.ToLower(testProject)))}).Return(nil, nil)
108116

109-
api.EXPECT().VolumeList(gomock.Any(), filters.NewArgs(projectFilter(strings.ToLower(testProject)))).Return(volume.VolumeListOKBody{Volumes: []*moby.Volume{{Name: "myProject_volume"}}}, nil)
110117
api.EXPECT().VolumeRemove(gomock.Any(), "myProject_volume", true).Return(nil)
111118

112119
err := tested.Down(context.Background(), strings.ToLower(testProject), compose.DownOptions{Volumes: true})

pkg/e2e/volumes_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ func TestLocalComposeVolume(t *testing.T) {
8383

8484
t.Run("cleanup volume project", func(t *testing.T) {
8585
c.RunDockerComposeCmd("--project-name", projectName, "down", "--volumes")
86-
res := c.RunDockerCmd("volume", "ls")
87-
assert.Assert(t, !strings.Contains(res.Stdout(), projectName+"_staticVol"))
88-
assert.Assert(t, !strings.Contains(res.Stdout(), "myvolume"))
86+
ls := c.RunDockerCmd("volume", "ls").Stdout()
87+
assert.Assert(t, !strings.Contains(ls, projectName+"_staticVol"))
88+
assert.Assert(t, !strings.Contains(ls, "myvolume"))
8989
})
9090
}

0 commit comments

Comments
 (0)