@@ -18,14 +18,13 @@ package compose
1818
1919import (
2020 "context"
21+ "fmt"
2122 "sort"
22- "strconv"
23-
24- moby "github.com/docker/docker/api/types"
25- "github.com/docker/docker/api/types/filters"
2623
2724 "github.com/docker/compose/v2/pkg/api"
2825 "github.com/docker/compose/v2/pkg/utils"
26+ moby "github.com/docker/docker/api/types"
27+ "github.com/docker/docker/api/types/filters"
2928)
3029
3130// Containers is a set of moby Container
@@ -41,7 +40,22 @@ const (
4140
4241func (s * composeService ) getContainers (ctx context.Context , project string , oneOff oneOff , stopped bool , selectedServices ... string ) (Containers , error ) {
4342 var containers Containers
44- f := []filters.KeyValuePair {projectFilter (project )}
43+ f := getDefaultFilters (project , oneOff , selectedServices ... )
44+ containers , err := s .apiClient ().ContainerList (ctx , moby.ContainerListOptions {
45+ Filters : filters .NewArgs (f ... ),
46+ All : stopped ,
47+ })
48+ if err != nil {
49+ return nil , err
50+ }
51+ if len (selectedServices ) > 1 {
52+ containers = containers .filter (isService (selectedServices ... ))
53+ }
54+ return containers , nil
55+ }
56+
57+ func getDefaultFilters (projectName string , oneOff oneOff , selectedServices ... string ) []filters.KeyValuePair {
58+ f := []filters.KeyValuePair {projectFilter (projectName )}
4559 if len (selectedServices ) == 1 {
4660 f = append (f , serviceFilter (selectedServices [0 ]))
4761 }
@@ -52,17 +66,26 @@ func (s *composeService) getContainers(ctx context.Context, project string, oneO
5266 f = append (f , oneOffFilter (false ))
5367 case oneOffInclude :
5468 }
69+ return f
70+ }
71+
72+ func (s * composeService ) getSpecifiedContainer (ctx context.Context , projectName string , oneOff oneOff , stopped bool , serviceName string , containerIndex int ) (moby.Container , error ) {
73+ defaultFilters := getDefaultFilters (projectName , oneOff , serviceName )
74+ defaultFilters = append (defaultFilters , containerNumberFilter (containerIndex ))
5575 containers , err := s .apiClient ().ContainerList (ctx , moby.ContainerListOptions {
56- Filters : filters .NewArgs (f ... ),
57- All : stopped ,
76+ Filters : filters .NewArgs (
77+ defaultFilters ... ,
78+ ),
79+ All : stopped ,
5880 })
5981 if err != nil {
60- return nil , err
82+ return moby. Container {} , err
6183 }
62- if len (selectedServices ) > 1 {
63- containers = containers . filter ( isService ( selectedServices ... ) )
84+ if len (containers ) < 1 {
85+ return moby. Container {}, fmt . Errorf ( "service %q is not running container #%d" , serviceName , containerIndex )
6486 }
65- return containers , nil
87+ container := containers [0 ]
88+ return container , nil
6689}
6790
6891// containerPredicate define a predicate we want container to satisfy for filtering operations
@@ -87,14 +110,6 @@ func isNotOneOff(c moby.Container) bool {
87110 return ! ok || v == "False"
88111}
89112
90- func indexed (index int ) containerPredicate {
91- return func (c moby.Container ) bool {
92- number := c .Labels [api .ContainerNumberLabel ]
93- idx , err := strconv .Atoi (number )
94- return err == nil && index == idx
95- }
96- }
97-
98113// filter return Containers with elements to match predicate
99114func (containers Containers ) filter (predicate containerPredicate ) Containers {
100115 var filtered Containers
0 commit comments