Skip to content

Commit 9ac0392

Browse files
committed
introduce --timeout on up
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 0612b34 commit 9ac0392

File tree

5 files changed

+68
-40
lines changed

5 files changed

+68
-40
lines changed

cmd/compose/up.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package compose
1919
import (
2020
"context"
2121
"fmt"
22+
"time"
2223

2324
"github.com/docker/compose/v2/cmd/formatter"
2425

@@ -48,6 +49,7 @@ type upOptions struct {
4849
noAttach []string
4950
timestamp bool
5051
wait bool
52+
waitTimeout int
5153
}
5254

5355
func (opts upOptions) apply(project *types.Project, services []string) error {
@@ -76,7 +78,7 @@ func upCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *cob
7678
Short: "Create and start containers",
7779
PreRunE: AdaptCmd(func(ctx context.Context, cmd *cobra.Command, args []string) error {
7880
create.pullChanged = cmd.Flags().Changed("pull")
79-
create.timeChanged = cmd.Flags().Changed("timeout")
81+
create.timeChanged = cmd.Flags().Changed("waitTimeout")
8082
return validateFlags(&up, &create)
8183
}),
8284
RunE: p.WithServices(func(ctx context.Context, project *types.Project, services []string) error {
@@ -102,7 +104,7 @@ func upCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *cob
102104
flags.BoolVar(&up.noStart, "no-start", false, "Don't start the services after creating them.")
103105
flags.BoolVar(&up.cascadeStop, "abort-on-container-exit", false, "Stops all containers if any container was stopped. Incompatible with -d")
104106
flags.StringVar(&up.exitCodeFrom, "exit-code-from", "", "Return the exit code of the selected service container. Implies --abort-on-container-exit")
105-
flags.IntVarP(&create.timeout, "timeout", "t", 10, "Use this timeout in seconds for container shutdown when attached or when containers are already running.")
107+
flags.IntVarP(&create.timeout, "waitTimeout", "t", 10, "Use this waitTimeout in seconds for container shutdown when attached or when containers are already running.")
106108
flags.BoolVar(&up.timestamp, "timestamps", false, "Show timestamps.")
107109
flags.BoolVar(&up.noDeps, "no-deps", false, "Don't start linked services.")
108110
flags.BoolVar(&create.recreateDeps, "always-recreate-deps", false, "Recreate dependent containers. Incompatible with --no-recreate.")
@@ -112,6 +114,7 @@ func upCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *cob
112114
flags.StringArrayVar(&up.attach, "attach", []string{}, "Attach to service output.")
113115
flags.StringArrayVar(&up.noAttach, "no-attach", []string{}, "Don't attach to specified service.")
114116
flags.BoolVar(&up.wait, "wait", false, "Wait for services to be running|healthy. Implies detached mode.")
117+
flags.IntVar(&up.waitTimeout, "wait-timeout", 0, "timeout waiting for application to be running|healthy.")
115118

116119
return upCmd
117120
}
@@ -188,6 +191,8 @@ func runUp(ctx context.Context, streams api.Streams, backend api.Service, create
188191
return backend.Create(ctx, project, create)
189192
}
190193

194+
timeout := time.Duration(upOptions.waitTimeout) * time.Second
195+
191196
return backend.Up(ctx, project, api.UpOptions{
192197
Create: create,
193198
Start: api.StartOptions{
@@ -197,6 +202,7 @@ func runUp(ctx context.Context, streams api.Streams, backend api.Service, create
197202
ExitCodeFrom: upOptions.exitCodeFrom,
198203
CascadeStop: upOptions.cascadeStop,
199204
Wait: upOptions.wait,
205+
WaitTimeout: timeout,
200206
Services: services,
201207
},
202208
})

docs/reference/compose_up.md

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,32 @@ Create and start containers
55

66
### Options
77

8-
| Name | Type | Default | Description |
9-
|:-----------------------------|:--------------|:----------|:---------------------------------------------------------------------------------------------------------|
10-
| `--abort-on-container-exit` | | | Stops all containers if any container was stopped. Incompatible with -d |
11-
| `--always-recreate-deps` | | | Recreate dependent containers. Incompatible with --no-recreate. |
12-
| `--attach` | `stringArray` | | Attach to service output. |
13-
| `--attach-dependencies` | | | Attach to dependent containers. |
14-
| `--build` | | | Build images before starting containers. |
15-
| `-d`, `--detach` | | | Detached mode: Run containers in the background |
16-
| `--exit-code-from` | `string` | | Return the exit code of the selected service container. Implies --abort-on-container-exit |
17-
| `--force-recreate` | | | Recreate containers even if their configuration and image haven't changed. |
18-
| `--no-attach` | `stringArray` | | Don't attach to specified service. |
19-
| `--no-build` | | | Don't build an image, even if it's missing. |
20-
| `--no-color` | | | Produce monochrome output. |
21-
| `--no-deps` | | | Don't start linked services. |
22-
| `--no-log-prefix` | | | Don't print prefix in logs. |
23-
| `--no-recreate` | | | If containers already exist, don't recreate them. Incompatible with --force-recreate. |
24-
| `--no-start` | | | Don't start the services after creating them. |
25-
| `--pull` | `string` | `missing` | Pull image before running ("always"\|"missing"\|"never") |
26-
| `--quiet-pull` | | | Pull without printing progress information. |
27-
| `--remove-orphans` | | | Remove containers for services not defined in the Compose file. |
28-
| `-V`, `--renew-anon-volumes` | | | Recreate anonymous volumes instead of retrieving data from the previous containers. |
29-
| `--scale` | `stringArray` | | Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present. |
30-
| `-t`, `--timeout` | `int` | `10` | Use this timeout in seconds for container shutdown when attached or when containers are already running. |
31-
| `--timestamps` | | | Show timestamps. |
32-
| `--wait` | | | Wait for services to be running\|healthy. Implies detached mode. |
8+
| Name | Type | Default | Description |
9+
|:-----------------------------|:--------------|:----------|:-------------------------------------------------------------------------------------------------------------|
10+
| `--abort-on-container-exit` | | | Stops all containers if any container was stopped. Incompatible with -d |
11+
| `--always-recreate-deps` | | | Recreate dependent containers. Incompatible with --no-recreate. |
12+
| `--attach` | `stringArray` | | Attach to service output. |
13+
| `--attach-dependencies` | | | Attach to dependent containers. |
14+
| `--build` | | | Build images before starting containers. |
15+
| `-d`, `--detach` | | | Detached mode: Run containers in the background |
16+
| `--exit-code-from` | `string` | | Return the exit code of the selected service container. Implies --abort-on-container-exit |
17+
| `--force-recreate` | | | Recreate containers even if their configuration and image haven't changed. |
18+
| `--no-attach` | `stringArray` | | Don't attach to specified service. |
19+
| `--no-build` | | | Don't build an image, even if it's missing. |
20+
| `--no-color` | | | Produce monochrome output. |
21+
| `--no-deps` | | | Don't start linked services. |
22+
| `--no-log-prefix` | | | Don't print prefix in logs. |
23+
| `--no-recreate` | | | If containers already exist, don't recreate them. Incompatible with --force-recreate. |
24+
| `--no-start` | | | Don't start the services after creating them. |
25+
| `--pull` | `string` | `missing` | Pull image before running ("always"\|"missing"\|"never") |
26+
| `--quiet-pull` | | | Pull without printing progress information. |
27+
| `--remove-orphans` | | | Remove containers for services not defined in the Compose file. |
28+
| `-V`, `--renew-anon-volumes` | | | Recreate anonymous volumes instead of retrieving data from the previous containers. |
29+
| `--scale` | `stringArray` | | Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present. |
30+
| `--timestamps` | | | Show timestamps. |
31+
| `--wait` | | | Wait for services to be running\|healthy. Implies detached mode. |
32+
| `--wait-timeout` | `int` | `0` | timeout waiting for application to be running\|healthy. |
33+
| `-t`, `--waitTimeout` | `int` | `10` | Use this waitTimeout in seconds for container shutdown when attached or when containers are already running. |
3334

3435

3536
<!---MARKER_GEN_END-->

docs/reference/docker_compose_up.yaml

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -231,18 +231,6 @@ options:
231231
experimentalcli: false
232232
kubernetes: false
233233
swarm: false
234-
- option: timeout
235-
shorthand: t
236-
value_type: int
237-
default_value: "10"
238-
description: |
239-
Use this timeout in seconds for container shutdown when attached or when containers are already running.
240-
deprecated: false
241-
hidden: false
242-
experimental: false
243-
experimentalcli: false
244-
kubernetes: false
245-
swarm: false
246234
- option: timestamps
247235
value_type: bool
248236
default_value: "false"
@@ -263,6 +251,28 @@ options:
263251
experimentalcli: false
264252
kubernetes: false
265253
swarm: false
254+
- option: wait-timeout
255+
value_type: int
256+
default_value: "0"
257+
description: timeout waiting for application to be running|healthy.
258+
deprecated: false
259+
hidden: false
260+
experimental: false
261+
experimentalcli: false
262+
kubernetes: false
263+
swarm: false
264+
- option: waitTimeout
265+
shorthand: t
266+
value_type: int
267+
default_value: "10"
268+
description: |
269+
Use this waitTimeout in seconds for container shutdown when attached or when containers are already running.
270+
deprecated: false
271+
hidden: false
272+
experimental: false
273+
experimentalcli: false
274+
kubernetes: false
275+
swarm: false
266276
deprecated: false
267277
experimental: false
268278
experimentalcli: false

pkg/api/api.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ type StartOptions struct {
140140
// ExitCodeFrom return exit code from specified service
141141
ExitCodeFrom string
142142
// Wait won't return until containers reached the running|healthy state
143-
Wait bool
143+
Wait bool
144+
WaitTimeout time.Duration
144145
// Services passed in the command line to be started
145146
Services []string
146147
}

pkg/compose/start.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package compose
1818

1919
import (
2020
"context"
21+
"fmt"
2122
"strings"
2223
"time"
2324

@@ -107,8 +108,17 @@ func (s *composeService) start(ctx context.Context, projectName string, options
107108
Condition: getDependencyCondition(s, project),
108109
}
109110
}
111+
if options.WaitTimeout > 0 {
112+
withTimeout, cancel := context.WithTimeout(ctx, options.WaitTimeout)
113+
ctx = withTimeout
114+
defer cancel()
115+
}
116+
110117
err = s.waitDependencies(ctx, project, depends, containers)
111118
if err != nil {
119+
if ctx.Err() == context.DeadlineExceeded {
120+
return fmt.Errorf("application not healthy after %s", options.WaitTimeout)
121+
}
112122
return err
113123
}
114124
}

0 commit comments

Comments
 (0)