Skip to content

Commit a0a1908

Browse files
committed
compose: start services with a TTY in the background on up -d
compose up -d only passed -d to nerdctl run for services without stdin_open and tty, so a service with tty: true ran in the foreground and up -d didn't return until the container exited. docker compose starts these services detached, and compose attach needs them running in the background. Pass -d whenever up (or run) is detached, and don't hand stdin to a detached run. The attach test no longer needs a pseudo-TTY for up -d. Signed-off-by: Srinivas Chippagiri <cvas22@gmail.com>
1 parent 4cac9ae commit a0a1908

3 files changed

Lines changed: 7 additions & 9 deletions

File tree

‎cmd/nerdctl/compose/compose_attach_linux_test.go‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@ services:
5151
projectName := filepath.Base(filepath.Dir(composePath))
5252
data.Labels().Set("composeYAML", composePath)
5353

54-
// A container with a TTY needs a console to be created, like with nerdctl run -it.
55-
cmd := helpers.Command("compose", "-f", composePath, "up", "-d")
56-
cmd.WithPseudoTTY()
57-
cmd.Run(&test.Expected{ExitCode: expect.ExitCodeSuccess})
54+
helpers.Ensure("compose", "-f", composePath, "up", "-d")
5855
nerdtest.EnsureContainerStarted(helpers, serviceparser.DefaultContainerName(projectName, "svc0", "1"))
5956
}
6057

‎pkg/composer/run.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ func (c *Composer) runServices(ctx context.Context, parsedServices []*servicepar
242242
container := ps.Containers[0]
243243

244244
runEG.Go(func() error {
245-
id, err := c.upServiceContainer(ctx, ps, container, RecreateForce)
245+
id, err := c.upServiceContainer(ctx, ps, container, RecreateForce, ro.Detach)
246246
if err != nil {
247247
return err
248248
}

‎pkg/composer/up_service.go‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (c *Composer) upServices(ctx context.Context, parsedServices []*servicepars
6161
for _, container := range ps.Containers {
6262
container := container
6363
runEG.Go(func() error {
64-
id, err := c.upServiceContainer(ctx, ps, container, recreate)
64+
id, err := c.upServiceContainer(ctx, ps, container, recreate, uo.Detach)
6565
if err != nil {
6666
return err
6767
}
@@ -153,7 +153,8 @@ func (c *Composer) ensureImageMountSources(ctx context.Context, ps *serviceparse
153153

154154
// upServiceContainer must be called after ensureServiceImage
155155
// upServiceContainer returns container ID
156-
func (c *Composer) upServiceContainer(ctx context.Context, service *serviceparser.Service, container serviceparser.Container, recreate string) (string, error) {
156+
// When detach is set, the container is started in the background even if it has a TTY, like docker compose up -d.
157+
func (c *Composer) upServiceContainer(ctx context.Context, service *serviceparser.Service, container serviceparser.Container, recreate string, detach bool) (string, error) {
157158
// check if container already exists
158159
existingCid, err := c.containerID(ctx, container.Name, service.Unparsed.Name)
159160
if err != nil {
@@ -166,7 +167,7 @@ func (c *Composer) upServiceContainer(ctx context.Context, service *serviceparse
166167
}
167168

168169
var runFlagD bool
169-
if !service.Unparsed.StdinOpen && !service.Unparsed.Tty {
170+
if detach || (!service.Unparsed.StdinOpen && !service.Unparsed.Tty) {
170171
container.RunArgs = append([]string{"-d"}, container.RunArgs...)
171172
runFlagD = true
172173
}
@@ -266,7 +267,7 @@ func (c *Composer) executeUpCmd(ctx context.Context, cmd *exec.Cmd, containerNam
266267
log.G(ctx).Debugf("Running %v", cmd.Args)
267268
}
268269

269-
if stdinOpen {
270+
if stdinOpen && !runFlagD {
270271
cmd.Stdin = os.Stdin
271272
}
272273
if !runFlagD {

0 commit comments

Comments
 (0)