Skip to content

Commit

Permalink
one-off container are not indexed, and must be ignored by exec --inde…
Browse files Browse the repository at this point in the history
…x command

Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof committed Oct 22, 2024
1 parent 0aad959 commit fa24ab8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
5 changes: 4 additions & 1 deletion pkg/compose/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/compose/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
29 changes: 29 additions & 0 deletions pkg/e2e/compose_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
})
}
1 change: 0 additions & 1 deletion pkg/e2e/compose_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down

0 comments on commit fa24ab8

Please sign in to comment.