From f067e263677fc86f9610ca61fbe42f63efad91f2 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Tue, 25 Mar 2014 23:21:07 +0000 Subject: [PATCH] Ensure that all containers are stopped cleanly at shutdown Docker-DCO-1.1-Signed-off-by: Michael Crosby (github: crosbymichael) --- runtime/runtime.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/runtime/runtime.go b/runtime/runtime.go index b035f5df9..85880ff9a 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -778,8 +778,31 @@ func NewRuntimeFromDirectory(config *daemonconfig.Config, eng *engine.Engine) (* return runtime, nil } +func (runtime *Runtime) shutdown() error { + group := sync.WaitGroup{} + utils.Debugf("starting clean shutdown of all containers...") + for _, container := range runtime.List() { + if container.State.IsRunning() { + utils.Debugf("stopping %s", container.ID) + group.Add(1) + + go func() { + defer group.Done() + container.Stop(10) + }() + } + } + group.Wait() + + return nil +} + func (runtime *Runtime) Close() error { errorsStrings := []string{} + if err := runtime.shutdown(); err != nil { + utils.Errorf("runtime.shutdown(): %s", err) + errorsStrings = append(errorsStrings, err.Error()) + } if err := portallocator.ReleaseAll(); err != nil { utils.Errorf("portallocator.ReleaseAll(): %s", err) errorsStrings = append(errorsStrings, err.Error())