Repository: docker/compose
File: pkg/compose/generate.go (Go)

Code before:
	"fmt"
	"strings"

	"github.com/compose-spec/compose-go/v2/types"
	"github.com/docker/compose/v2/pkg/api"
	"github.com/docker/compose/v2/pkg/utils"
	moby "github.com/docker/docker/api/types"
	containerType "github.com/docker/docker/api/types/container"
	"github.com/docker/docker/api/types/filters"
	"github.com/docker/docker/api/types/mount"
	"github.com/docker/docker/api/types/network"

	"golang.org/x/exp/maps"
)

func (s *composeService) Generate(ctx context.Context, options api.GenerateOptions) (*types.Project, error) {
	filtersListNames := filters.NewArgs()
	filtersListIDs := filters.NewArgs()
	for _, containerName := range options.Containers {
		filtersListNames.Add("name", containerName)
		filtersListIDs.Add("id", containerName)
	}
	containers, err := s.apiClient().ContainerList(ctx, containerType.ListOptions{
		Filters: filtersListNames,
		All:     true,
	})
	if err != nil {
		return nil, err
	}

	containersByIds, err := s.apiClient().ContainerList(ctx, containerType.ListOptions{
		Filters: filtersListIDs,
		All:     true,
	})
	if err != nil {
		return nil, err
	}
	for _, container := range containersByIds {
		if !utils.Contains(containers, container) {
			containers = append(containers, container)
		}
	}

	if len(containers) == 0 {
		return nil, fmt.Errorf("no container(s) found with the following name(s): %s", strings.Join(options.Containers, ","))
	}

	return s.createProjectFromContainers(containers)
}

func (s *composeService) createProjectFromContainers(containers []moby.Container) (*types.Project, error) {

Review comment (suggestion):
til about this filter list approach! nice :) 

one side effect of treating the user-provided container references as `name` filters is that they'll fuzzy-match. it could result in unexpected behavior (or an implicit feature? lol) for users. just something to note!

Code after:
	"fmt"
	"strings"

	"github.com/compose-spec/compose-go/v2/types"
	"github.com/docker/compose/v2/pkg/api"
	"github.com/docker/compose/v2/pkg/utils"
	moby "github.com/docker/docker/api/types"
	containerType "github.com/docker/docker/api/types/container"
	"github.com/docker/docker/api/types/filters"
	"github.com/docker/docker/api/types/mount"
	"github.com/docker/docker/api/types/network"

	"golang.org/x/exp/maps"
)

func (s *composeService) Generate(ctx context.Context, options api.GenerateOptions) (*types.Project, error) {
	filtersListNames := filters.NewArgs()
	filtersListIDs := filters.NewArgs()
	for _, containerName := range options.Containers {
		filtersListNames.Add("name", containerName)
		filtersListIDs.Add("id", containerName)
	}
	containers, err := s.apiClient().ContainerList(ctx, containerType.ListOptions{
		Filters: filtersListNames,
		All:     true,
	})
	if err != nil {
		return nil, err
	}

	containersByIds, err := s.apiClient().ContainerList(ctx, containerType.ListOptions{
		Filters: filtersListIDs,
		All:     true,
	})
	if err != nil {
		return nil, err
	}
	for _, container := range containersByIds {
		if !utils.Contains(containers, container) {
			containers = append(containers, container)
		}
	}

	if len(containers) == 0 {
		return nil, fmt.Errorf("no container(s) found with the following name(s): %s", strings.Join(options.Containers, ","))
	}

	return s.createProjectFromContainers(containers, options.ProjectName)
}

func (s *composeService) createProjectFromContainers(containers []moby.Container, projectName string) (*types.Project, error) {