Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cmd/nerdctl/container/container_restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func NewRestartCommand() *cobra.Command {
SilenceErrors: true,
}
restartCommand.Flags().UintP("time", "t", 10, "Seconds to wait for stop before killing it")
restartCommand.Flags().StringP("signal", "s", "", "Signal to send to stop the container, before killing it")
return restartCommand
}

Expand All @@ -58,10 +59,21 @@ func processContainerRestartOptions(cmd *cobra.Command) (types.ContainerRestartO
timeout = &t
}

var signal string
if cmd.Flags().Changed("signal") {
// Signal to send to stop the container, before killing it
sig, err := cmd.Flags().GetString("signal")
if err != nil {
return types.ContainerRestartOptions{}, err
}
signal = sig
}

return types.ContainerRestartOptions{
Stdout: cmd.OutOrStdout(),
GOption: globalOptions,
Timeout: timeout,
Signal: signal,
}, err
}

Expand Down
29 changes: 29 additions & 0 deletions cmd/nerdctl/container/container_restart_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,32 @@ func TestRestartWithTime(t *testing.T) {
// ensure that stop took at least 5 seconds
assert.Assert(t, timePostRestart.Sub(timePreRestart) >= time.Second*5)
}

func TestRestartWithSignal(t *testing.T) {
t.Parallel()
base := testutil.NewBase(t)
tID := testutil.Identifier(t)

base.Cmd("run", "-d", "--name", tID, testutil.AlpineImage, "sh", "-c", `
trap 'echo "Received SIGUSR1"; exit 0' SIGUSR1
echo "Starting"
while true; do
sleep 1
done
`).AssertOK()
defer base.Cmd("rm", "-f", tID).Run()

base.EnsureContainerStarted(tID)

inspect := base.InspectContainer(tID)
initialPid := inspect.State.Pid

base.Cmd("restart", "--signal", "SIGUSR1", tID).AssertOK()
base.EnsureContainerStarted(tID)

newInspect := base.InspectContainer(tID)
newPid := newInspect.State.Pid

assert.Assert(t, initialPid != newPid, "Container PID should have changed after restart")

}
2 changes: 2 additions & 0 deletions docs/command-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ Flags:

- :whale: `-t, --time=SECONDS`: Seconds to wait for stop before killing it (default "10")
- Tips: If the init process in container is exited after receiving SIGTERM or exited before the time you specified, the container will be exited immediately
- :whale: `-s, --signal=SIGNAL`: Signal to send to the container (e.g. SIGINT).

### :whale: nerdctl start

Expand All @@ -597,6 +598,7 @@ Flags:

- :whale: `-t, --time=SECONDS`: Seconds to wait for stop before killing it (default "10")
- Tips: If the init process in container is exited after receiving SIGTERM or exited before the time you specified, the container will be exited immediately
- :whale: `-s, --signal=SIGNAL`: Signal to send to the container (e.g. SIGINT).

### :whale: nerdctl update

Expand Down
2 changes: 2 additions & 0 deletions pkg/api/types/container_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ type ContainerRestartOptions struct {
GOption GlobalCommandOptions
// Time to wait after sending a SIGTERM and before sending a SIGKILL.
Timeout *time.Duration
// Signal to send to stop the container, before sending SIGKILL
Signal string
}

// ContainerPauseOptions specifies options for `nerdctl (container) pause`.
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/container/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func Restart(ctx context.Context, client *containerd.Client, containers []string
if found.MatchCount > 1 {
return fmt.Errorf("multiple IDs found with provided prefix: %s", found.Req)
}
if err := containerutil.Stop(ctx, found.Container, options.Timeout, "SIGTERM"); err != nil {
if err := containerutil.Stop(ctx, found.Container, options.Timeout, options.Signal); err != nil {
return err
}
if err := containerutil.Start(ctx, found.Container, false, client, ""); err != nil {
Expand Down
Loading