github.1git.de/docker/cli@v26.1.3+incompatible/cli/command/container/restart.go (about) 1 package container 2 3 import ( 4 "context" 5 "fmt" 6 "strings" 7 8 "github.com/docker/cli/cli" 9 "github.com/docker/cli/cli/command" 10 "github.com/docker/cli/cli/command/completion" 11 "github.com/docker/docker/api/types/container" 12 "github.com/pkg/errors" 13 "github.com/spf13/cobra" 14 ) 15 16 type restartOptions struct { 17 signal string 18 timeout int 19 timeoutChanged bool 20 21 containers []string 22 } 23 24 // NewRestartCommand creates a new cobra.Command for `docker restart` 25 func NewRestartCommand(dockerCli command.Cli) *cobra.Command { 26 var opts restartOptions 27 28 cmd := &cobra.Command{ 29 Use: "restart [OPTIONS] CONTAINER [CONTAINER...]", 30 Short: "Restart one or more containers", 31 Args: cli.RequiresMinArgs(1), 32 RunE: func(cmd *cobra.Command, args []string) error { 33 opts.containers = args 34 opts.timeoutChanged = cmd.Flags().Changed("time") 35 return runRestart(cmd.Context(), dockerCli, &opts) 36 }, 37 Annotations: map[string]string{ 38 "aliases": "docker container restart, docker restart", 39 }, 40 ValidArgsFunction: completion.ContainerNames(dockerCli, true), 41 } 42 43 flags := cmd.Flags() 44 flags.StringVarP(&opts.signal, "signal", "s", "", "Signal to send to the container") 45 flags.IntVarP(&opts.timeout, "time", "t", 0, "Seconds to wait before killing the container") 46 return cmd 47 } 48 49 func runRestart(ctx context.Context, dockerCli command.Cli, opts *restartOptions) error { 50 var errs []string 51 var timeout *int 52 if opts.timeoutChanged { 53 timeout = &opts.timeout 54 } 55 for _, name := range opts.containers { 56 err := dockerCli.Client().ContainerRestart(ctx, name, container.StopOptions{ 57 Signal: opts.signal, 58 Timeout: timeout, 59 }) 60 if err != nil { 61 errs = append(errs, err.Error()) 62 continue 63 } 64 _, _ = fmt.Fprintln(dockerCli.Out(), name) 65 } 66 if len(errs) > 0 { 67 return errors.New(strings.Join(errs, "\n")) 68 } 69 return nil 70 }