add --signal option to stop and restart

Wording and documentation still need to be updated, but will do
so in a follow-up.

Also removing the default "10 seconds" from the timeout flags, as
this default is not actually used, and may not match the actual
default (which is defined on the daemon side).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-02-18 12:25:17 +01:00
parent 53f8ed4bec
commit 86c30e6a0d
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
4 changed files with 22 additions and 16 deletions

View File

@ -14,8 +14,9 @@ import (
) )
type restartOptions struct { type restartOptions struct {
nSeconds int signal string
nSecondsChanged bool timeout int
timeoutChanged bool
containers []string containers []string
} }
@ -30,14 +31,15 @@ func NewRestartCommand(dockerCli command.Cli) *cobra.Command {
Args: cli.RequiresMinArgs(1), Args: cli.RequiresMinArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
opts.containers = args opts.containers = args
opts.nSecondsChanged = cmd.Flags().Changed("time") opts.timeoutChanged = cmd.Flags().Changed("time")
return runRestart(dockerCli, &opts) return runRestart(dockerCli, &opts)
}, },
ValidArgsFunction: completion.ContainerNames(dockerCli, true), ValidArgsFunction: completion.ContainerNames(dockerCli, true),
} }
flags := cmd.Flags() flags := cmd.Flags()
flags.IntVarP(&opts.nSeconds, "time", "t", 10, "Seconds to wait for stop before killing the container") flags.StringVarP(&opts.signal, "signal", "s", "", "Signal to send to the container")
flags.IntVarP(&opts.timeout, "time", "t", 0, "Seconds to wait before killing the container")
return cmd return cmd
} }
@ -45,11 +47,12 @@ func runRestart(dockerCli command.Cli, opts *restartOptions) error {
ctx := context.Background() ctx := context.Background()
var errs []string var errs []string
var timeout *int var timeout *int
if opts.nSecondsChanged { if opts.timeoutChanged {
timeout = &opts.nSeconds timeout = &opts.timeout
} }
for _, name := range opts.containers { for _, name := range opts.containers {
err := dockerCli.Client().ContainerRestart(ctx, name, container.StopOptions{ err := dockerCli.Client().ContainerRestart(ctx, name, container.StopOptions{
Signal: opts.signal,
Timeout: timeout, Timeout: timeout,
}) })
if err != nil { if err != nil {

View File

@ -14,8 +14,9 @@ import (
) )
type stopOptions struct { type stopOptions struct {
time int signal string
timeChanged bool timeout int
timeoutChanged bool
containers []string containers []string
} }
@ -30,25 +31,27 @@ func NewStopCommand(dockerCli command.Cli) *cobra.Command {
Args: cli.RequiresMinArgs(1), Args: cli.RequiresMinArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
opts.containers = args opts.containers = args
opts.timeChanged = cmd.Flags().Changed("time") opts.timeoutChanged = cmd.Flags().Changed("time")
return runStop(dockerCli, &opts) return runStop(dockerCli, &opts)
}, },
ValidArgsFunction: completion.ContainerNames(dockerCli, false), ValidArgsFunction: completion.ContainerNames(dockerCli, false),
} }
flags := cmd.Flags() flags := cmd.Flags()
flags.IntVarP(&opts.time, "time", "t", 10, "Seconds to wait for stop before killing it") flags.StringVarP(&opts.signal, "signal", "s", "", "Signal to send to the container")
flags.IntVarP(&opts.timeout, "time", "t", 0, "Seconds to wait before killing the container")
return cmd return cmd
} }
func runStop(dockerCli command.Cli, opts *stopOptions) error { func runStop(dockerCli command.Cli, opts *stopOptions) error {
var timeout *int var timeout *int
if opts.timeChanged { if opts.timeoutChanged {
timeout = &opts.time timeout = &opts.timeout
} }
errChan := parallelOperation(context.Background(), opts.containers, func(ctx context.Context, id string) error { errChan := parallelOperation(context.Background(), opts.containers, func(ctx context.Context, id string) error {
return dockerCli.Client().ContainerStop(ctx, id, container.StopOptions{ return dockerCli.Client().ContainerStop(ctx, id, container.StopOptions{
Signal: opts.signal,
Timeout: timeout, Timeout: timeout,
}) })
}) })

View File

@ -12,8 +12,8 @@ Usage: docker restart [OPTIONS] CONTAINER [CONTAINER...]
Restart one or more containers Restart one or more containers
Options: Options:
--help Print usage -s, --signal string Signal to send to the container
-t, --time int Seconds to wait for stop before killing the container (default 10) -t, --time int Seconds to wait before killing the container
``` ```
## Examples ## Examples

View File

@ -12,8 +12,8 @@ Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]
Stop one or more running containers Stop one or more running containers
Options: Options:
--help Print usage -s, --signal string Signal to send to the container
-t, --time int Seconds to wait for stop before killing it (default 10) -t, --time int Seconds to wait before killing the container
``` ```
## Description ## Description