Skip to content
Open
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
77 changes: 77 additions & 0 deletions cmd/nerdctl/container/container_run_restart_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,80 @@

testCase.Run(t)
}

// assertStoppedNotRestarting checks that stop/kill left the container stopped
// and that the restart plugin will not bring it back. On older containerd
// (e.g. v1.7), inspect may report Status as "" once the task is gone even
// though Running is false and the container is effectively exited.
func assertStoppedNotRestarting(dc []dockercompat.Container, t tig.T) {
assert.Equal(t, 1, len(dc))
assert.Assert(t, dc[0].State != nil, "State is nil")
assert.Equal(t, false, dc[0].State.Running)
// Must not still be running or in a restart loop.
assert.Assert(t, dc[0].State.Status != "running" && dc[0].State.Status != "restarting",
"unexpected Status %q", dc[0].State.Status)
if !nerdtest.IsDocker() {
assert.Assert(t, dc[0].Config != nil && dc[0].Config.Labels != nil)
assert.Equal(t, "stopped", dc[0].Config.Labels[restart.StatusLabel])
}
}

func TestRunRestartAlwaysStop(t *testing.T) {
testCase := nerdtest.Setup()
if !nerdtest.IsDocker() {
testCase.Require = nerdtest.ContainerdPlugin("io.containerd.internal.v1", "restart", []string{"always"})
}

testCase.Setup = func(data test.Data, helpers test.Helpers) {
helpers.Ensure("run", "-d", "--restart=always", "--name", data.Identifier(), testutil.CommonImage, "sleep", "infinity")
helpers.Ensure("stop", data.Identifier())
}

testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rm", "-f", data.Identifier())
}

testCase.Command = func(data test.Data, helpers test.Helpers) test.TestableCommand {
time.Sleep(3 * time.Second)
return helpers.Command("inspect", data.Identifier())
}

testCase.Expected = func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
ExitCode: expect.ExitCodeSuccess,
Output: expect.JSON([]dockercompat.Container{}, assertStoppedNotRestarting),

Check failure on line 426 in cmd/nerdctl/container/container_run_restart_linux_test.go

View workflow job for this annotation

GitHub Actions / go / linux

File is not properly formatted (gci)
}
}

testCase.Run(t)
}

func TestRunRestartAlwaysKill(t *testing.T) {
testCase := nerdtest.Setup()
if !nerdtest.IsDocker() {
testCase.Require = nerdtest.ContainerdPlugin("io.containerd.internal.v1", "restart", []string{"always"})
}

testCase.Setup = func(data test.Data, helpers test.Helpers) {
helpers.Ensure("run", "-d", "--restart=always", "--name", data.Identifier(), testutil.CommonImage, "sleep", "infinity")
helpers.Ensure("kill", data.Identifier())
}

testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rm", "-f", data.Identifier())
}

testCase.Command = func(data test.Data, helpers test.Helpers) test.TestableCommand {
time.Sleep(3 * time.Second)
return helpers.Command("inspect", data.Identifier())
}

testCase.Expected = func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
ExitCode: expect.ExitCodeSuccess,
Output: expect.JSON([]dockercompat.Container{}, assertStoppedNotRestarting),
}
}

testCase.Run(t)
}
8 changes: 8 additions & 0 deletions pkg/cmd/container/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/moby/sys/signal"

containerd "github.com/containerd/containerd/v2/client"
"github.com/containerd/containerd/v2/core/runtime/restart"
"github.com/containerd/containerd/v2/pkg/cio"
"github.com/containerd/errdefs"
"github.com/containerd/go-cni"
Expand Down Expand Up @@ -88,6 +89,13 @@ func killContainer(ctx context.Context, container containerd.Container, signal s
if err := containerutil.UpdateExplicitlyStoppedLabel(ctx, container, true); err != nil {
return err
}
if l, err := container.Labels(ctx); err == nil {

@ningmingxiao ningmingxiao Aug 24, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if restart policy is always we should skip to set it because if containerd daemon is restarted we should let containerd start the container.

if _, ok := l[restart.PolicyLabel]; ok {
if err := containerutil.UpdateStatusLabel(ctx, container, containerd.Stopped); err != nil {
return err
}
}
}
task, err := container.Task(ctx, cio.Load)
if err != nil {
return err
Expand Down
6 changes: 6 additions & 0 deletions pkg/containerutil/containerutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,12 @@ func Stop(ctx context.Context, container containerd.Container, timeout *time.Dur
if err != nil {
return err
}

if _, ok := l[restart.PolicyLabel]; ok {
if err := UpdateStatusLabel(ctx, container, containerd.Stopped); err != nil {
return err
}
}
ipc, err := ipcutil.DecodeIPCLabel(l[labels.IPC])
if err != nil {
return err
Expand Down
Loading