From 02ffe2ac6c834de81a4f3c9daab7652c6c65a01f Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Mon, 25 Aug 2025 16:30:00 +0200 Subject: [PATCH] prefer application container vs one-off running `exec` without index Signed-off-by: Nicolas De Loof --- pkg/compose/containers.go | 2 +- pkg/e2e/exec_test.go | 47 ++++++++++++++++++++++++++++++ pkg/e2e/fixtures/exec/compose.yaml | 4 +++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 pkg/e2e/exec_test.go create mode 100644 pkg/e2e/fixtures/exec/compose.yaml diff --git a/pkg/compose/containers.go b/pkg/compose/containers.go index 598cc2a23..a2c4c99b7 100644 --- a/pkg/compose/containers.go +++ b/pkg/compose/containers.go @@ -100,7 +100,7 @@ func (s *composeService) getSpecifiedContainer(ctx context.Context, projectName IsOneOffLabelTrueX := containers[i].Labels[api.OneoffLabel] == "True" IsOneOffLabelTrueY := containers[j].Labels[api.OneoffLabel] == "True" - if numberLabelX == numberLabelY { + if IsOneOffLabelTrueX || IsOneOffLabelTrueY { return !IsOneOffLabelTrueX && IsOneOffLabelTrueY } diff --git a/pkg/e2e/exec_test.go b/pkg/e2e/exec_test.go new file mode 100644 index 000000000..16643a0f8 --- /dev/null +++ b/pkg/e2e/exec_test.go @@ -0,0 +1,47 @@ +/* + Copyright 2023 Docker Compose CLI authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package e2e + +import ( + "testing" + + "gotest.tools/v3/icmd" +) + +func TestExec(t *testing.T) { + const projectName = "e2e-exec" + c := NewParallelCLI(t) + + cleanup := func() { + c.RunDockerComposeCmd(t, "--project-name", projectName, "down", "--timeout=0", "--remove-orphans") + } + t.Cleanup(cleanup) + cleanup() + + c.RunDockerComposeCmd(t, "-f", "./fixtures/exec/compose.yaml", "--project-name", projectName, "run", "-d", "test", "cat") + + res := c.RunDockerComposeCmdNoCheck(t, "--project-name", projectName, "exec", "--index=1", "test", "ps") + res.Assert(t, icmd.Expected{Err: "service \"test\" is not running container #1", ExitCode: 1}) + + res = c.RunDockerComposeCmd(t, "--project-name", projectName, "exec", "test", "ps") + res.Assert(t, icmd.Expected{Out: "cat"}) // one-off container was selected + + c.RunDockerComposeCmd(t, "-f", "./fixtures/exec/compose.yaml", "--project-name", projectName, "up", "-d") + + res = c.RunDockerComposeCmd(t, "--project-name", projectName, "exec", "test", "ps") + res.Assert(t, icmd.Expected{Out: "tail"}) // service container was selected +} diff --git a/pkg/e2e/fixtures/exec/compose.yaml b/pkg/e2e/fixtures/exec/compose.yaml new file mode 100644 index 000000000..8920173bf --- /dev/null +++ b/pkg/e2e/fixtures/exec/compose.yaml @@ -0,0 +1,4 @@ +services: + test: + image: alpine + command: tail -f /dev/null \ No newline at end of file