|
|
@@ -20,6 +20,7 @@ import (
|
|
|
"context"
|
|
|
"fmt"
|
|
|
"sort"
|
|
|
+ "strconv"
|
|
|
|
|
|
"github.com/docker/compose/v2/pkg/api"
|
|
|
"github.com/docker/compose/v2/pkg/utils"
|
|
|
@@ -72,7 +73,9 @@ func getDefaultFilters(projectName string, oneOff oneOff, selectedServices ...st
|
|
|
|
|
|
func (s *composeService) getSpecifiedContainer(ctx context.Context, projectName string, oneOff oneOff, stopped bool, serviceName string, containerIndex int) (moby.Container, error) {
|
|
|
defaultFilters := getDefaultFilters(projectName, oneOff, serviceName)
|
|
|
- defaultFilters = append(defaultFilters, containerNumberFilter(containerIndex))
|
|
|
+ if containerIndex > 0 {
|
|
|
+ defaultFilters = append(defaultFilters, containerNumberFilter(containerIndex))
|
|
|
+ }
|
|
|
containers, err := s.apiClient().ContainerList(ctx, moby.ContainerListOptions{
|
|
|
Filters: filters.NewArgs(
|
|
|
defaultFilters...,
|
|
|
@@ -83,8 +86,16 @@ func (s *composeService) getSpecifiedContainer(ctx context.Context, projectName
|
|
|
return moby.Container{}, err
|
|
|
}
|
|
|
if len(containers) < 1 {
|
|
|
- return moby.Container{}, fmt.Errorf("service %q is not running container #%d", serviceName, containerIndex)
|
|
|
+ if containerIndex > 0 {
|
|
|
+ return moby.Container{}, fmt.Errorf("service %q is not running container #%d", serviceName, containerIndex)
|
|
|
+ }
|
|
|
+ return moby.Container{}, fmt.Errorf("service %q is not running", serviceName)
|
|
|
}
|
|
|
+ sort.Slice(containers, func(i, j int) bool {
|
|
|
+ x, _ := strconv.Atoi(containers[i].Labels[api.ContainerNumberLabel])
|
|
|
+ y, _ := strconv.Atoi(containers[j].Labels[api.ContainerNumberLabel])
|
|
|
+ return x < y
|
|
|
+ })
|
|
|
container := containers[0]
|
|
|
return container, nil
|
|
|
}
|