@@ -7,22 +7,22 @@ import (
7
7
"os"
8
8
"strings"
9
9
10
- "golang.org/x/net/context"
11
-
12
10
"github.com/Sirupsen/logrus"
13
11
"github.com/docker/engine-api/client"
14
12
"github.com/docker/engine-api/types"
15
13
"github.com/docker/engine-api/types/container"
16
14
"github.com/docker/go-connections/nat"
17
15
"github.com/hyperhq/hypercli/pkg/promise"
18
16
"github.com/hyperhq/hypercli/pkg/stdcopy"
17
+ "github.com/hyperhq/hypercli/pkg/stringid"
19
18
"github.com/hyperhq/hypercli/pkg/term"
20
19
"github.com/hyperhq/libcompose/config"
21
20
"github.com/hyperhq/libcompose/labels"
22
21
"github.com/hyperhq/libcompose/logger"
23
22
"github.com/hyperhq/libcompose/project"
24
23
"github.com/hyperhq/libcompose/project/events"
25
24
util "github.com/hyperhq/libcompose/utils"
25
+ "golang.org/x/net/context"
26
26
)
27
27
28
28
// 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
325
325
return - 1 , err
326
326
}
327
327
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
+ }
331
367
}
332
368
333
- return exitedContainer . State . ExitCode , nil
369
+ return status , nil
334
370
}
335
371
336
372
func holdHijackedConnection (tty bool , inputStream io.ReadCloser , outputStream , errorStream io.Writer , resp types.HijackedResponse ) error {
0 commit comments