Skip to content
This repository was archived by the owner on Mar 30, 2020. It is now read-only.

Commit 45c3b03

Browse files
committed
Change killContainer to stopContainer and wait for graceful shutdown
1 parent 6841747 commit 45c3b03

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

core/docker.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
)
1616

1717
const LatestTag = "latest"
18+
const GracefulShutdownTime = 20
1819

1920
type Docker struct {
2021
endPoint string
@@ -85,11 +86,13 @@ func (d *Docker) cleanContainers(p *Project) error {
8586
for _, c := range l {
8687
if c.IsRunning() {
8788
Debug("Stoping container and image", "project", p, "container", c.GetShortID(), "end-point", d.endPoint)
88-
if err := d.killContainer(c); err != nil {
89+
if err := d.stopContainer(c); err != nil {
8990
return err
9091
}
9192
}
9293

94+
d.waitForGracefulShutdown(c)
95+
9396
Debug("Removing container", "project", p, "container", c.GetShortID(), "end-point", d.endPoint)
9497
if err := d.removeContainer(c); err != nil {
9598
return err
@@ -99,9 +102,8 @@ func (d *Docker) cleanContainers(p *Project) error {
99102
return nil
100103
}
101104

102-
func (d *Docker) killContainer(c *Container) error {
103-
kopts := docker.KillContainerOptions{ID: c.ID}
104-
if err := d.client.KillContainer(kopts); err != nil {
105+
func (d *Docker) stopContainer(c *Container) error {
106+
if err := d.client.StopContainer(c.ID, GracefulShutdownTime); err != nil {
105107
return err
106108
}
107109

@@ -455,15 +457,25 @@ func (d *Docker) restartLinkedContainers(p *Project) error {
455457
return nil
456458
}
457459

460+
func (d *Docker) waitForGracefulShutdown(c * Container) {
461+
timedOut := 0
462+
for c.IsRunning() && timedOut >= GracefulShutdownTime {
463+
<-time.After(time.Second)
464+
timedOut++
465+
}
466+
}
467+
458468
func (d *Docker) restartContainer(p *Project, c *Container) error {
459469
if !c.IsRunning() {
460470
return nil
461471
}
462472

463-
if err := d.killContainer(c); err != nil {
473+
if err := d.stopContainer(c); err != nil {
464474
return err
465475
}
466476

477+
d.waitForGracefulShutdown(c)
478+
467479
if err := d.startContainer(p, c); err != nil {
468480
return err
469481
}

0 commit comments

Comments
 (0)