From ad3a2b83ddef92c022f176c2468c7ad44df9bf83 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Mon, 13 Feb 2023 17:42:11 +0100 Subject: [PATCH] introduce --timeout on `up` Signed-off-by: Nicolas De Loof --- cmd/compose/up.go | 10 ++++-- docs/reference/compose_up.md | 51 ++++++++++++++------------- docs/reference/docker_compose_up.yaml | 34 +++++++++++------- pkg/api/api.go | 3 +- pkg/compose/start.go | 10 ++++++ 5 files changed, 68 insertions(+), 40 deletions(-) diff --git a/cmd/compose/up.go b/cmd/compose/up.go index 70575399f49..435fca133ce 100644 --- a/cmd/compose/up.go +++ b/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, }, }) diff --git a/docs/reference/compose_up.md b/docs/reference/compose_up.md index 6cd10b1559e..43029840fcc 100644 --- a/docs/reference/compose_up.md +++ b/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. | diff --git a/docs/reference/docker_compose_up.yaml b/docs/reference/docker_compose_up.yaml index f8ab468e7bb..fb3dcb98cca 100644 --- a/docs/reference/docker_compose_up.yaml +++ b/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 diff --git a/pkg/api/api.go b/pkg/api/api.go index 1f3bd2b5c62..a48202f297b 100644 --- a/pkg/api/api.go +++ b/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 } diff --git a/pkg/compose/start.go b/pkg/compose/start.go index d236365a3f0..265a448dee5 100644 --- a/pkg/compose/start.go +++ b/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 } }