Skip to content

Commit

Permalink
Merge pull request #3614 from thaJeztah/signal_options
Browse files Browse the repository at this point in the history
add --signal option to stop and restart
  • Loading branch information
thaJeztah authored May 17, 2022
2 parents e55151f + 86c30e6 commit b655203
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
15 changes: 9 additions & 6 deletions cli/command/container/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (
)

type restartOptions struct {
nSeconds int
nSecondsChanged bool
signal string
timeout int
timeoutChanged bool

containers []string
}
Expand All @@ -30,26 +31,28 @@ func NewRestartCommand(dockerCli command.Cli) *cobra.Command {
Args: cli.RequiresMinArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.containers = args
opts.nSecondsChanged = cmd.Flags().Changed("time")
opts.timeoutChanged = cmd.Flags().Changed("time")
return runRestart(dockerCli, &opts)
},
ValidArgsFunction: completion.ContainerNames(dockerCli, true),
}

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
}

func runRestart(dockerCli command.Cli, opts *restartOptions) error {
ctx := context.Background()
var errs []string
var timeout *int
if opts.nSecondsChanged {
timeout = &opts.nSeconds
if opts.timeoutChanged {
timeout = &opts.timeout
}
for _, name := range opts.containers {
err := dockerCli.Client().ContainerRestart(ctx, name, container.StopOptions{
Signal: opts.signal,
Timeout: timeout,
})
if err != nil {
Expand Down
15 changes: 9 additions & 6 deletions cli/command/container/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (
)

type stopOptions struct {
time int
timeChanged bool
signal string
timeout int
timeoutChanged bool

containers []string
}
Expand All @@ -30,25 +31,27 @@ func NewStopCommand(dockerCli command.Cli) *cobra.Command {
Args: cli.RequiresMinArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.containers = args
opts.timeChanged = cmd.Flags().Changed("time")
opts.timeoutChanged = cmd.Flags().Changed("time")
return runStop(dockerCli, &opts)
},
ValidArgsFunction: completion.ContainerNames(dockerCli, false),
}

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
}

func runStop(dockerCli command.Cli, opts *stopOptions) error {
var timeout *int
if opts.timeChanged {
timeout = &opts.time
if opts.timeoutChanged {
timeout = &opts.timeout
}

errChan := parallelOperation(context.Background(), opts.containers, func(ctx context.Context, id string) error {
return dockerCli.Client().ContainerStop(ctx, id, container.StopOptions{
Signal: opts.signal,
Timeout: timeout,
})
})
Expand Down
4 changes: 2 additions & 2 deletions docs/reference/commandline/restart.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ Usage: docker restart [OPTIONS] CONTAINER [CONTAINER...]
Restart one or more containers

Options:
--help Print usage
-t, --time int Seconds to wait for stop before killing the container (default 10)
-s, --signal string Signal to send to the container
-t, --time int Seconds to wait before killing the container
```

## Examples
Expand Down
4 changes: 2 additions & 2 deletions docs/reference/commandline/stop.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]
Stop one or more running containers

Options:
--help Print usage
-t, --time int Seconds to wait for stop before killing it (default 10)
-s, --signal string Signal to send to the container
-t, --time int Seconds to wait before killing the container
```

## Description
Expand Down

0 comments on commit b655203

Please sign in to comment.