From 4f9db4d3e69b48d58d7c4dc6ada07c5211a01169 Mon Sep 17 00:00:00 2001 From: Suleiman Dibirov Date: Sat, 20 Jul 2024 09:50:17 +0300 Subject: [PATCH] fix(containers): fix sorting logic by adding secondary sorting for one-off containers Signed-off-by: Suleiman Dibirov --- pkg/compose/containers.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/pkg/compose/containers.go b/pkg/compose/containers.go index e898ce848..9b5e6bac4 100644 --- a/pkg/compose/containers.go +++ b/pkg/compose/containers.go @@ -93,10 +93,19 @@ func (s *composeService) getSpecifiedContainer(ctx context.Context, projectName } return moby.Container{}, fmt.Errorf("service %q is not running", serviceName) } + + // Sort by container number first, then put one-off containers at the end 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 + numberLabelX, _ := strconv.Atoi(containers[i].Labels[api.ContainerNumberLabel]) + numberLabelY, _ := strconv.Atoi(containers[j].Labels[api.ContainerNumberLabel]) + IsOneOffLabelTrueX := containers[i].Labels[api.OneoffLabel] == "True" + IsOneOffLabelTrueY := containers[j].Labels[api.OneoffLabel] == "True" + + if numberLabelX == numberLabelY { + return !IsOneOffLabelTrueX && IsOneOffLabelTrueY + } + + return numberLabelX < numberLabelY }) container := containers[0] return container, nil