one-off container are not indexed, and must be ignored by exec --index command

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2024-10-21 15:37:42 +02:00 committed by Nicolas De loof
parent 0aad9595a5
commit fa24ab8e25
4 changed files with 34 additions and 3 deletions

View File

@ -505,7 +505,10 @@ func (s *composeService) prepareLabels(labels types.Labels, service types.Servic
}
labels[api.ConfigHashLabel] = hash
if number > 0 {
// One-off containers are not indexed
labels[api.ContainerNumberLabel] = strconv.Itoa(number)
}
var dependencies []string
for s, d := range service.DependsOn {

View File

@ -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
}

View File

@ -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"})
})
}

View File

@ -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})