Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 2f3b6a6

Browse files
authored
Merge pull request #198 from carmark/compose_exitcode
fix compose run exit code issue
2 parents 2994ba2 + 56c1918 commit 2f3b6a6

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

api/client/compose.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func (cli *DockerCli) CmdComposeRun(args ...string) error {
5757
Context: project.Context{
5858
ComposeFiles: []string{*composeFile},
5959
ProjectName: *projectName,
60+
Autoremove: *rm,
6061
},
6162
ClientFactory: cli,
6263
})
@@ -69,15 +70,15 @@ func (cli *DockerCli) CmdComposeRun(args ...string) error {
6970
if err != nil {
7071
return err
7172
}
72-
if status != 0 {
73-
return Cli.StatusError{StatusCode: status}
74-
}
7573
if *rm {
7674
opts := options.Delete{RemoveVolume: true}
7775
if err = project.Delete(opts, service); err != nil {
7876
return err
7977
}
8078
}
79+
if status != 0 {
80+
return Cli.StatusError{StatusCode: status}
81+
}
8182

8283
return nil
8384
}

vendor/src/github.com/hyperhq/libcompose/docker/container.go

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@ import (
77
"os"
88
"strings"
99

10-
"golang.org/x/net/context"
11-
1210
"github.com/Sirupsen/logrus"
1311
"github.com/docker/engine-api/client"
1412
"github.com/docker/engine-api/types"
1513
"github.com/docker/engine-api/types/container"
1614
"github.com/docker/go-connections/nat"
1715
"github.com/hyperhq/hypercli/pkg/promise"
1816
"github.com/hyperhq/hypercli/pkg/stdcopy"
17+
"github.com/hyperhq/hypercli/pkg/stringid"
1918
"github.com/hyperhq/hypercli/pkg/term"
2019
"github.com/hyperhq/libcompose/config"
2120
"github.com/hyperhq/libcompose/labels"
2221
"github.com/hyperhq/libcompose/logger"
2322
"github.com/hyperhq/libcompose/project"
2423
"github.com/hyperhq/libcompose/project/events"
2524
util "github.com/hyperhq/libcompose/utils"
25+
"golang.org/x/net/context"
2626
)
2727

2828
// Container holds information about a docker container and the service it is tied on.
@@ -325,12 +325,48 @@ func (c *Container) Run(ctx context.Context, imageName string, configOverride *c
325325
return -1, err
326326
}
327327

328-
exitedContainer, err := c.client.ContainerInspect(ctx, container.ID)
329-
if err != nil {
330-
return -1, err
328+
var status int
329+
// Attached mode
330+
if c.service.context.Autoremove {
331+
// Warn user if they detached us
332+
js, err := c.client.ContainerInspect(ctx, container.ID)
333+
if err != nil {
334+
return -1, err
335+
}
336+
if js.State.Running == true || js.State.Paused == true {
337+
logrus.Infof("Detached from %s, awaiting its termination in order to uphold \"--rm\".\n",
338+
stringid.TruncateID(container.ID))
339+
}
340+
341+
// Autoremove: wait for the container to finish, retrieve
342+
// the exit code and remove the container
343+
if status, err = c.client.ContainerWait(ctx, container.ID); err != nil {
344+
return -1, err
345+
}
346+
exitedContainer, err := c.client.ContainerInspect(ctx, container.ID)
347+
if err != nil {
348+
return -1, err
349+
}
350+
status = exitedContainer.State.ExitCode
351+
} else {
352+
// No Autoremove: Simply retrieve the exit code
353+
if !configOverride.Tty {
354+
// In non-TTY mode, we can't detach, so we must wait for container exit
355+
if status, err = c.client.ContainerWait(ctx, container.ID); err != nil {
356+
return -1, err
357+
}
358+
} else {
359+
// In TTY mode, there is a race: if the process dies too slowly, the state could
360+
// be updated after the getExitCode call and result in the wrong exit code being reported
361+
exitedContainer, err := c.client.ContainerInspect(ctx, container.ID)
362+
if err != nil {
363+
return -1, err
364+
}
365+
status = exitedContainer.State.ExitCode
366+
}
331367
}
332368

333-
return exitedContainer.State.ExitCode, nil
369+
return status, nil
334370
}
335371

336372
func holdHijackedConnection(tty bool, inputStream io.ReadCloser, outputStream, errorStream io.Writer, resp types.HijackedResponse) error {

vendor/src/github.com/hyperhq/libcompose/project/context.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ type Context struct {
2929
LoggerFactory logger.Factory
3030
IgnoreMissingConfig bool
3131
Project *Project
32+
33+
Autoremove bool
3234
}
3335

3436
func (c *Context) readComposeFiles() error {

0 commit comments

Comments
 (0)