docker compose up always kills the containers on second Ctrl-C

Kill executed on the second Ctrl-C of docker compose up  was
filtering containers depending on its state. In some cases the
containers can reach an state for what these filters don't get
any container, and the command keeps reporting `no container to
kill`.

Remove this filtering, so it tries to kill any container it
finds, independently of its state.

Fixes https://github.com/docker/compose/issues/10661

Signed-off-by: Jaime Soriano Pastor <jaime.soriano@elastic.co>
This commit is contained in:
Jaime Soriano Pastor 2024-04-11 22:04:39 +02:00 committed by Nicolas De loof
parent b032999f06
commit 5c1e5f3fc7
4 changed files with 8 additions and 5 deletions

View File

@ -312,6 +312,8 @@ type KillOptions struct {
Services []string Services []string
// Signal to send to containers // Signal to send to containers
Signal string Signal string
// All can be set to true to try to kill all found containers, independently of their state
All bool
} }
// RemoveOptions group options of the Remove API // RemoveOptions group options of the Remove API

View File

@ -41,12 +41,12 @@ const (
oneOffOnly oneOffOnly
) )
func (s *composeService) getContainers(ctx context.Context, project string, oneOff oneOff, stopped bool, selectedServices ...string) (Containers, error) { func (s *composeService) getContainers(ctx context.Context, project string, oneOff oneOff, all bool, selectedServices ...string) (Containers, error) {
var containers Containers var containers Containers
f := getDefaultFilters(project, oneOff, selectedServices...) f := getDefaultFilters(project, oneOff, selectedServices...)
containers, err := s.apiClient().ContainerList(ctx, containerType.ListOptions{ containers, err := s.apiClient().ContainerList(ctx, containerType.ListOptions{
Filters: filters.NewArgs(f...), Filters: filters.NewArgs(f...),
All: stopped, All: all,
}) })
if err != nil { if err != nil {
return nil, err return nil, err
@ -73,7 +73,7 @@ func getDefaultFilters(projectName string, oneOff oneOff, selectedServices ...st
return f return f
} }
func (s *composeService) getSpecifiedContainer(ctx context.Context, projectName string, oneOff oneOff, stopped bool, serviceName string, containerIndex int) (moby.Container, error) { func (s *composeService) getSpecifiedContainer(ctx context.Context, projectName string, oneOff oneOff, all bool, serviceName string, containerIndex int) (moby.Container, error) {
defaultFilters := getDefaultFilters(projectName, oneOff, serviceName) defaultFilters := getDefaultFilters(projectName, oneOff, serviceName)
if containerIndex > 0 { if containerIndex > 0 {
defaultFilters = append(defaultFilters, containerNumberFilter(containerIndex)) defaultFilters = append(defaultFilters, containerNumberFilter(containerIndex))
@ -82,7 +82,7 @@ func (s *composeService) getSpecifiedContainer(ctx context.Context, projectName
Filters: filters.NewArgs( Filters: filters.NewArgs(
defaultFilters..., defaultFilters...,
), ),
All: stopped, All: all,
}) })
if err != nil { if err != nil {
return moby.Container{}, err return moby.Container{}, err

View File

@ -40,7 +40,7 @@ func (s *composeService) kill(ctx context.Context, projectName string, options a
services := options.Services services := options.Services
var containers Containers var containers Containers
containers, err := s.getContainers(ctx, projectName, oneOffInclude, false, services...) containers, err := s.getContainers(ctx, projectName, oneOffInclude, options.All, services...)
if err != nil { if err != nil {
return err return err
} }

View File

@ -122,6 +122,7 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
return s.Kill(context.Background(), project.Name, api.KillOptions{ return s.Kill(context.Background(), project.Name, api.KillOptions{
Services: options.Create.Services, Services: options.Create.Services,
Project: project, Project: project,
All: true,
}) })
}) })
return nil return nil