diff --git a/pkg/compose/create.go b/pkg/compose/create.go index 2dbb41f8958..6994823ab57 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 1bd769dbb61..7598b68bac0 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 1e3d6695e22..cc8222802fc 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 8a19f3d3181..40b2f48d5aa 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})