Skip to content

Commit 3c5e99c

Browse files
committed
fix: make restart can restart stopped container
Signed-off-by: Allen Sun <[email protected]>
1 parent 0713c83 commit 3c5e99c

File tree

4 files changed

+12
-17
lines changed

4 files changed

+12
-17
lines changed

daemon/mgr/container.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,20 +1160,19 @@ func (mgr *ContainerManager) Restart(ctx context.Context, name string, timeout i
11601160
c.Lock()
11611161
defer c.Unlock()
11621162

1163-
if !c.IsRunning() {
1164-
return fmt.Errorf("cannot restart a non running container")
1165-
}
1166-
11671163
if timeout == 0 {
11681164
timeout = c.StopTimeout()
11691165
}
11701166

1171-
// stop container
1172-
if err := mgr.stop(ctx, c, timeout); err != nil {
1173-
return errors.Wrapf(err, "failed to stop container")
1167+
if c.IsRunning() {
1168+
// stop container if it is running.
1169+
if err := mgr.stop(ctx, c, timeout); err != nil {
1170+
logrus.Errorf("failed to stop container %s when restarting: %v", c.ID(), err)
1171+
return errors.Wrapf(err, fmt.Sprintf("failed to stop container %s", c.ID()))
1172+
}
11741173
}
1175-
logrus.Debug("Restart: container " + c.ID() + " stopped succeeded")
11761174

1175+
logrus.Debugf("start container %s when restarting", c.ID())
11771176
// start container
11781177
return mgr.start(ctx, c, "")
11791178
}

test/api_container_restart_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (suite *APIContainerRestartSuite) TestAPIRestartStoppedContainer(c *check.C
5454

5555
resp, err := request.Post("/containers/"+cname+"/restart", query)
5656
c.Assert(err, check.IsNil)
57-
CheckRespStatus(c, resp, 500)
57+
CheckRespStatus(c, resp, 204)
5858

5959
DelContainerForceOk(c, cname)
6060
}

test/cli_restart_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (suite *PouchRestartSuite) TearDownTest(c *check.C) {
3434
func (suite *PouchRestartSuite) TestPouchRestart(c *check.C) {
3535
name := "TestPouchRestart"
3636

37-
command.PouchRun("run", "-d", "--cpu-share", "20", "--name", name, busyboxImage, "top").Assert(c, icmd.Success)
37+
command.PouchRun("run", "-d", "--cpu-share", "20", "--name", name, busyboxImage).Assert(c, icmd.Success)
3838

3939
res := command.PouchRun("restart", "-t", "1", name)
4040
c.Assert(res.Error, check.IsNil)
@@ -47,18 +47,14 @@ func (suite *PouchRestartSuite) TestPouchRestart(c *check.C) {
4747
}
4848

4949
// TestPouchRestartStoppedContainer is to verify the correctness of restarting a stopped container.
50+
// Pouch should be compatible with moby's API. Restarting a stopped container is allowed.
5051
func (suite *PouchRestartSuite) TestPouchRestartStoppedContainer(c *check.C) {
5152
name := "TestPouchRestartStoppedContainer"
5253

5354
command.PouchRun("create", "--name", name, busyboxImage).Assert(c, icmd.Success)
5455

5556
res := command.PouchRun("restart", "-t", "1", name)
56-
c.Assert(res.Error, check.NotNil)
57-
58-
expectString := "cannot restart a non running container"
59-
if out := res.Combined(); !strings.Contains(out, expectString) {
60-
c.Fatalf("unexpected output: %s, expected: %s", out, expectString)
61-
}
57+
c.Assert(res.Error, check.IsNil)
6258

6359
command.PouchRun("rm", "-f", name).Assert(c, icmd.Success)
6460
}

test/cli_upgrade_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (suite *PouchUpgradeSuite) TestPouchUpgradeStoppedContainer(c *check.C) {
8282
c.Assert(res.Error, check.IsNil)
8383

8484
if out := res.Combined(); !strings.Contains(out, name) {
85-
c.Fatal("unexpected output: %s, expected %s", out, name)
85+
c.Fatalf("unexpected output: %s, expected %s", out, name)
8686
}
8787

8888
command.PouchRun("start", name).Assert(c, icmd.Success)

0 commit comments

Comments
 (0)