mirror of
https://github.com/docker/compose.git
synced 2025-07-24 06:04:57 +02:00
Merge pull request #1787 from ndeloof/remove_anonymous
`compose down -v` also removes anonymous volumes
This commit is contained in:
commit
a29d3e6f80
@ -232,7 +232,7 @@ func (s *composeService) doBuild(ctx context.Context, project *types.Project, op
|
|||||||
for _, c := range observedState {
|
for _, c := range observedState {
|
||||||
for imageName := range opts {
|
for imageName := range opts {
|
||||||
if c.Image == imageName {
|
if c.Image == imageName {
|
||||||
err = s.removeContainers(ctx, cw, []moby.Container{c}, nil)
|
err = s.removeContainers(ctx, cw, []moby.Container{c}, nil, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -93,7 +93,7 @@ func (s *composeService) create(ctx context.Context, project *types.Project, opt
|
|||||||
if len(orphans) > 0 {
|
if len(orphans) > 0 {
|
||||||
if options.RemoveOrphans {
|
if options.RemoveOrphans {
|
||||||
w := progress.ContextWriter(ctx)
|
w := progress.ContextWriter(ctx)
|
||||||
err := s.removeContainers(ctx, w, orphans, nil)
|
err := s.removeContainers(ctx, w, orphans, nil, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -66,7 +66,7 @@ func (s *composeService) down(ctx context.Context, projectName string, options c
|
|||||||
|
|
||||||
err = InReverseDependencyOrder(ctx, options.Project, func(c context.Context, service types.ServiceConfig) error {
|
err = InReverseDependencyOrder(ctx, options.Project, func(c context.Context, service types.ServiceConfig) error {
|
||||||
serviceContainers := containers.filter(isService(service.Name))
|
serviceContainers := containers.filter(isService(service.Name))
|
||||||
err := s.removeContainers(ctx, w, serviceContainers, options.Timeout)
|
err := s.removeContainers(ctx, w, serviceContainers, options.Timeout, options.Volumes)
|
||||||
return err
|
return err
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -75,7 +75,7 @@ func (s *composeService) down(ctx context.Context, projectName string, options c
|
|||||||
|
|
||||||
orphans := containers.filter(isNotService(options.Project.ServiceNames()...))
|
orphans := containers.filter(isNotService(options.Project.ServiceNames()...))
|
||||||
if options.RemoveOrphans && len(orphans) > 0 {
|
if options.RemoveOrphans && len(orphans) > 0 {
|
||||||
err := s.removeContainers(ctx, w, orphans, options.Timeout)
|
err := s.removeContainers(ctx, w, orphans, options.Timeout, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -215,7 +215,7 @@ func (s *composeService) stopContainers(ctx context.Context, w progress.Writer,
|
|||||||
return eg.Wait()
|
return eg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *composeService) removeContainers(ctx context.Context, w progress.Writer, containers []moby.Container, timeout *time.Duration) error {
|
func (s *composeService) removeContainers(ctx context.Context, w progress.Writer, containers []moby.Container, timeout *time.Duration, volumes bool) error {
|
||||||
eg, _ := errgroup.WithContext(ctx)
|
eg, _ := errgroup.WithContext(ctx)
|
||||||
for _, container := range containers {
|
for _, container := range containers {
|
||||||
toDelete := container
|
toDelete := container
|
||||||
@ -228,7 +228,10 @@ func (s *composeService) removeContainers(ctx context.Context, w progress.Writer
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
w.Event(progress.RemovingEvent(eventName))
|
w.Event(progress.RemovingEvent(eventName))
|
||||||
err = s.apiClient.ContainerRemove(ctx, toDelete.ID, moby.ContainerRemoveOptions{Force: true})
|
err = s.apiClient.ContainerRemove(ctx, toDelete.ID, moby.ContainerRemoveOptions{
|
||||||
|
Force: true,
|
||||||
|
RemoveVolumes: volumes,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.Event(progress.ErrorMessageEvent(eventName, "Error while Removing"))
|
w.Event(progress.ErrorMessageEvent(eventName, "Error while Removing"))
|
||||||
return err
|
return err
|
||||||
|
@ -90,7 +90,7 @@ func TestDownRemoveVolumes(t *testing.T) {
|
|||||||
[]apitypes.Container{testContainer("service1", "123")}, nil)
|
[]apitypes.Container{testContainer("service1", "123")}, nil)
|
||||||
|
|
||||||
api.EXPECT().ContainerStop(gomock.Any(), "123", nil).Return(nil)
|
api.EXPECT().ContainerStop(gomock.Any(), "123", nil).Return(nil)
|
||||||
api.EXPECT().ContainerRemove(gomock.Any(), "123", apitypes.ContainerRemoveOptions{Force: true}).Return(nil)
|
api.EXPECT().ContainerRemove(gomock.Any(), "123", apitypes.ContainerRemoveOptions{Force: true, RemoveVolumes: true}).Return(nil)
|
||||||
|
|
||||||
api.EXPECT().NetworkList(gomock.Any(), apitypes.NetworkListOptions{Filters: filters.NewArgs(projectFilter(testProject))}).Return(nil, nil)
|
api.EXPECT().NetworkList(gomock.Any(), apitypes.NetworkListOptions{Filters: filters.NewArgs(projectFilter(testProject))}).Return(nil, nil)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user