فهرست منبع

add --timeout option to compose up

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 4 سال پیش
والد
کامیت
2858911c1c
1فایلهای تغییر یافته به همراه13 افزوده شده و 0 حذف شده
  1. 13 0
      cli/cmd/compose/up.go

+ 13 - 0
cli/cmd/compose/up.go

@@ -25,6 +25,7 @@ import (
 	"strconv"
 	"strings"
 	"syscall"
+	"time"
 
 	"github.com/docker/compose-cli/api/client"
 	"github.com/docker/compose-cli/api/compose"
@@ -60,6 +61,8 @@ type upOptions struct {
 	scale         []string
 	noColor       bool
 	noPrefix      bool
+	timeChanged   bool
+	timeout       int
 }
 
 func (o upOptions) recreateStrategy() string {
@@ -82,6 +85,7 @@ func upCommand(p *projectOptions, contextType string) *cobra.Command {
 		Use:   "up [SERVICE...]",
 		Short: "Create and start containers",
 		RunE: func(cmd *cobra.Command, args []string) error {
+			opts.timeChanged = cmd.Flags().Changed("timeout")
 			switch contextType {
 			case store.LocalContextType, store.DefaultContextType, store.EcsLocalSimulationContextType:
 				if opts.exitCodeFrom != "" {
@@ -117,6 +121,7 @@ func upCommand(p *projectOptions, contextType string) *cobra.Command {
 		flags.BoolVar(&opts.noStart, "no-start", false, "Don't start the services after creating them.")
 		flags.BoolVar(&opts.cascadeStop, "abort-on-container-exit", false, "Stops all containers if any container was stopped. Incompatible with -d")
 		flags.StringVar(&opts.exitCodeFrom, "exit-code-from", "", "Return the exit code of the selected service container. Implies --abort-on-container-exit")
+		flags.IntVarP(&opts.timeout, "timeout", "t", 10, "Use this timeout in seconds for container shutdown when attached or when containers are already running.")
 	}
 
 	return upCmd
@@ -159,6 +164,14 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
 		}
 	}
 
+	if opts.timeChanged {
+		timeoutValue := types.Duration(time.Duration(opts.timeout) * time.Second)
+		for i, s := range project.Services {
+			s.StopGracePeriod = &timeoutValue
+			project.Services[i] = s
+		}
+	}
+
 	_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
 		err := c.ComposeService().Create(ctx, project, compose.CreateOptions{
 			RemoveOrphans: opts.removeOrphans,