Merge pull request #9256 from ndeloof/rm_empty

don't fail trying to remove container with no candidate
This commit is contained in:
Guillaume Lours 2022-03-09 16:36:13 +01:00 committed by GitHub
commit 4d163f35f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 2 deletions

View File

@ -165,7 +165,7 @@ SERVICES:
continue SERVICES
}
}
return project, errors.New("no such service: " + qs)
return project, errors.Wrapf(api.ErrNotFound, "no such service: %q", qs)
}
err := project.ForServices(services)
if err != nil {

View File

@ -32,6 +32,10 @@ import (
func (s *composeService) Remove(ctx context.Context, projectName string, options api.RemoveOptions) error {
containers, _, err := s.actualState(ctx, projectName, options.Services)
if err != nil {
if api.IsNotFoundError(err) {
fmt.Fprintln(s.stderr(), "No stopped containers")
return nil
}
return err
}
@ -45,7 +49,7 @@ func (s *composeService) Remove(ctx context.Context, projectName string, options
})
if len(names) == 0 {
fmt.Println("No stopped containers")
fmt.Fprintln(s.stderr(), "No stopped containers")
return nil
}
msg := fmt.Sprintf("Going to remove %s", strings.Join(names, ", "))

View File

@ -188,6 +188,11 @@ func TestRm(t *testing.T) {
assert.Assert(t, !strings.Contains(res.Combined(), projectName+"_simple"), res.Combined())
})
t.Run("rm -sf <none>", func(t *testing.T) {
res := c.RunDockerComposeCmd("-f", "./fixtures/simple-composefile/compose.yaml", "-p", projectName, "rm", "-sf", "simple")
res.Assert(t, icmd.Expected{ExitCode: 0})
})
t.Run("down", func(t *testing.T) {
c.RunDockerComposeCmd("-p", projectName, "down")
})