From fa24ab8e252f2fdab70dea7c2a7bed024704c739 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Mon, 21 Oct 2024 15:37:42 +0200 Subject: [PATCH] one-off container are not indexed, and must be ignored by exec --index command Signed-off-by: Nicolas De Loof --- pkg/compose/create.go | 5 ++++- pkg/compose/run.go | 2 +- pkg/e2e/compose_exec_test.go | 29 +++++++++++++++++++++++++++++ pkg/e2e/compose_run_test.go | 1 - 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/pkg/compose/create.go b/pkg/compose/create.go index 2dbb41f89..6994823ab 100644 --- a/pkg/compose/create.go +++ b/pkg/compose/create.go @@ -505,7 +505,10 @@ func (s *composeService) prepareLabels(labels types.Labels, service types.Servic } labels[api.ConfigHashLabel] = hash - labels[api.ContainerNumberLabel] = strconv.Itoa(number) + if number > 0 { + // One-off containers are not indexed + labels[api.ContainerNumberLabel] = strconv.Itoa(number) + } var dependencies []string for s, d := range service.DependsOn { diff --git a/pkg/compose/run.go b/pkg/compose/run.go index 1bd769dbb..7598b68ba 100644 --- a/pkg/compose/run.go +++ b/pkg/compose/run.go @@ -109,7 +109,7 @@ func (s *composeService) prepareRun(ctx context.Context, project *types.Project, return "", err } - created, err := s.createContainer(ctx, project, service, service.ContainerName, 1, createOpts) + created, err := s.createContainer(ctx, project, service, service.ContainerName, -1, createOpts) if err != nil { return "", err } diff --git a/pkg/e2e/compose_exec_test.go b/pkg/e2e/compose_exec_test.go index 1e3d6695e..cc8222802 100644 --- a/pkg/e2e/compose_exec_test.go +++ b/pkg/e2e/compose_exec_test.go @@ -65,3 +65,32 @@ func TestLocalComposeExec(t *testing.T) { assert.Check(t, !strings.Contains(res.Stdout(), "FOO="), res.Combined()) }) } + +func TestLocalComposeExecOneOff(t *testing.T) { + c := NewParallelCLI(t) + + const projectName = "compose-e2e-exec-one-off" + cmdArgs := func(cmd string, args ...string) []string { + ret := []string{"--project-directory", "fixtures/simple-composefile", "--project-name", projectName, cmd} + ret = append(ret, args...) + return ret + } + + cleanup := func() { + c.RunDockerComposeCmd(t, cmdArgs("down", "--timeout=0")...) + } + cleanup() + t.Cleanup(cleanup) + + c.RunDockerComposeCmd(t, cmdArgs("run", "-d", "simple")...) + + t.Run("exec in one-off container", func(t *testing.T) { + res := c.RunDockerComposeCmd(t, cmdArgs("exec", "-e", "FOO", "simple", "/usr/bin/env")...) + assert.Check(t, !strings.Contains(res.Stdout(), "FOO="), res.Combined()) + }) + + t.Run("exec with index", func(t *testing.T) { + res := c.RunDockerComposeCmdNoCheck(t, cmdArgs("exec", "--index", "1", "-e", "FOO", "simple", "/usr/bin/env")...) + res.Assert(t, icmd.Expected{ExitCode: 1, Err: "service \"simple\" is not running container #1"}) + }) +} diff --git a/pkg/e2e/compose_run_test.go b/pkg/e2e/compose_run_test.go index 8a19f3d31..40b2f48d5 100644 --- a/pkg/e2e/compose_run_test.go +++ b/pkg/e2e/compose_run_test.go @@ -62,7 +62,6 @@ func TestLocalComposeRun(t *testing.T) { assert.Assert(t, runContainerID != "") res = c.RunDockerCmd(t, "inspect", runContainerID) res.Assert(t, icmd.Expected{Out: ` "Status": "exited"`}) - res.Assert(t, icmd.Expected{Out: `"com.docker.compose.container-number": "1"`}) res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project": "run-test"`}) res.Assert(t, icmd.Expected{Out: `"com.docker.compose.oneoff": "True",`}) res.Assert(t, icmd.Expected{Out: `"com.docker.compose.slug": "` + truncatedSlug})