Skip to content

Commit 4f9db4d

Browse files
idsulikndeloof
authored andcommitted
fix(containers): fix sorting logic by adding secondary sorting for one-off containers
Signed-off-by: Suleiman Dibirov <idsulik@gmail.com>
1 parent 06bf339 commit 4f9db4d

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

pkg/compose/containers.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,19 @@ func (s *composeService) getSpecifiedContainer(ctx context.Context, projectName
9393
}
9494
return moby.Container{}, fmt.Errorf("service %q is not running", serviceName)
9595
}
96+
97+
// Sort by container number first, then put one-off containers at the end
9698
sort.Slice(containers, func(i, j int) bool {
97-
x, _ := strconv.Atoi(containers[i].Labels[api.ContainerNumberLabel])
98-
y, _ := strconv.Atoi(containers[j].Labels[api.ContainerNumberLabel])
99-
return x < y
99+
numberLabelX, _ := strconv.Atoi(containers[i].Labels[api.ContainerNumberLabel])
100+
numberLabelY, _ := strconv.Atoi(containers[j].Labels[api.ContainerNumberLabel])
101+
IsOneOffLabelTrueX := containers[i].Labels[api.OneoffLabel] == "True"
102+
IsOneOffLabelTrueY := containers[j].Labels[api.OneoffLabel] == "True"
103+
104+
if numberLabelX == numberLabelY {
105+
return !IsOneOffLabelTrueX && IsOneOffLabelTrueY
106+
}
107+
108+
return numberLabelX < numberLabelY
100109
})
101110
container := containers[0]
102111
return container, nil

0 commit comments

Comments
 (0)