ソースを参照

introduce --timeout on `up`

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 2 年 前
コミット
9ac0392baf

+ 8 - 2
cmd/compose/up.go

@@ -19,6 +19,7 @@ package compose
 import (
 	"context"
 	"fmt"
+	"time"
 
 	"github.com/docker/compose/v2/cmd/formatter"
 
@@ -48,6 +49,7 @@ type upOptions struct {
 	noAttach           []string
 	timestamp          bool
 	wait               bool
+	waitTimeout        int
 }
 
 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
 		Short: "Create and start containers",
 		PreRunE: AdaptCmd(func(ctx context.Context, cmd *cobra.Command, args []string) error {
 			create.pullChanged = cmd.Flags().Changed("pull")
-			create.timeChanged = cmd.Flags().Changed("timeout")
+			create.timeChanged = cmd.Flags().Changed("waitTimeout")
 			return validateFlags(&up, &create)
 		}),
 		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
 	flags.BoolVar(&up.noStart, "no-start", false, "Don't start the services after creating them.")
 	flags.BoolVar(&up.cascadeStop, "abort-on-container-exit", false, "Stops all containers if any container was stopped. Incompatible with -d")
 	flags.StringVar(&up.exitCodeFrom, "exit-code-from", "", "Return the exit code of the selected service container. Implies --abort-on-container-exit")
-	flags.IntVarP(&create.timeout, "timeout", "t", 10, "Use this timeout in seconds for container shutdown when attached or when containers are already running.")
+	flags.IntVarP(&create.timeout, "waitTimeout", "t", 10, "Use this waitTimeout in seconds for container shutdown when attached or when containers are already running.")
 	flags.BoolVar(&up.timestamp, "timestamps", false, "Show timestamps.")
 	flags.BoolVar(&up.noDeps, "no-deps", false, "Don't start linked services.")
 	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
 	flags.StringArrayVar(&up.attach, "attach", []string{}, "Attach to service output.")
 	flags.StringArrayVar(&up.noAttach, "no-attach", []string{}, "Don't attach to specified service.")
 	flags.BoolVar(&up.wait, "wait", false, "Wait for services to be running|healthy. Implies detached mode.")
+	flags.IntVar(&up.waitTimeout, "wait-timeout", 0, "timeout waiting for application to be running|healthy.")
 
 	return upCmd
 }
@@ -188,6 +191,8 @@ func runUp(ctx context.Context, streams api.Streams, backend api.Service, create
 		return backend.Create(ctx, project, create)
 	}
 
+	timeout := time.Duration(upOptions.waitTimeout) * time.Second
+
 	return backend.Up(ctx, project, api.UpOptions{
 		Create: create,
 		Start: api.StartOptions{
@@ -197,6 +202,7 @@ func runUp(ctx context.Context, streams api.Streams, backend api.Service, create
 			ExitCodeFrom: upOptions.exitCodeFrom,
 			CascadeStop:  upOptions.cascadeStop,
 			Wait:         upOptions.wait,
+			WaitTimeout:  timeout,
 			Services:     services,
 		},
 	})

+ 26 - 25
docs/reference/compose_up.md

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

+ 22 - 12
docs/reference/docker_compose_up.yaml

@@ -231,18 +231,6 @@ options:
       experimentalcli: false
       kubernetes: false
       swarm: false
-    - option: timeout
-      shorthand: t
-      value_type: int
-      default_value: "10"
-      description: |
-        Use this timeout in seconds for container shutdown when attached or when containers are already running.
-      deprecated: false
-      hidden: false
-      experimental: false
-      experimentalcli: false
-      kubernetes: false
-      swarm: false
     - option: timestamps
       value_type: bool
       default_value: "false"
@@ -263,6 +251,28 @@ options:
       experimentalcli: false
       kubernetes: false
       swarm: false
+    - option: wait-timeout
+      value_type: int
+      default_value: "0"
+      description: timeout waiting for application to be running|healthy.
+      deprecated: false
+      hidden: false
+      experimental: false
+      experimentalcli: false
+      kubernetes: false
+      swarm: false
+    - option: waitTimeout
+      shorthand: t
+      value_type: int
+      default_value: "10"
+      description: |
+        Use this waitTimeout in seconds for container shutdown when attached or when containers are already running.
+      deprecated: false
+      hidden: false
+      experimental: false
+      experimentalcli: false
+      kubernetes: false
+      swarm: false
 deprecated: false
 experimental: false
 experimentalcli: false

+ 2 - 1
pkg/api/api.go

@@ -140,7 +140,8 @@ type StartOptions struct {
 	// ExitCodeFrom return exit code from specified service
 	ExitCodeFrom string
 	// Wait won't return until containers reached the running|healthy state
-	Wait bool
+	Wait        bool
+	WaitTimeout time.Duration
 	// Services passed in the command line to be started
 	Services []string
 }

+ 10 - 0
pkg/compose/start.go

@@ -18,6 +18,7 @@ package compose
 
 import (
 	"context"
+	"fmt"
 	"strings"
 	"time"
 
@@ -107,8 +108,17 @@ func (s *composeService) start(ctx context.Context, projectName string, options
 				Condition: getDependencyCondition(s, project),
 			}
 		}
+		if options.WaitTimeout > 0 {
+			withTimeout, cancel := context.WithTimeout(ctx, options.WaitTimeout)
+			ctx = withTimeout
+			defer cancel()
+		}
+
 		err = s.waitDependencies(ctx, project, depends, containers)
 		if err != nil {
+			if ctx.Err() == context.DeadlineExceeded {
+				return fmt.Errorf("application not healthy after %s", options.WaitTimeout)
+			}
 			return err
 		}
 	}