diff --git a/daemon/daemon.go b/daemon/daemon.go index 36ad2d4b9c..cfd7ba0f2a 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -6,6 +6,7 @@ import ( "path" "plugin" "reflect" + "time" "github.com/alibaba/pouch/apis/plugins" "github.com/alibaba/pouch/apis/server" @@ -169,6 +170,13 @@ func (d *Daemon) Run() error { } d.containerMgr = containerMgr + if err := containerMgr.Restore(ctx); err != nil { + return err + } + + // ensure all container has been restored + time.Sleep(100 * time.Millisecond) + networkMgr, err := internal.GenNetworkMgr(d.config, d) if err != nil { return err @@ -176,12 +184,6 @@ func (d *Daemon) Run() error { d.networkMgr = networkMgr containerMgr.(*mgr.ContainerManager).NetworkMgr = networkMgr - // Notes(ziren): we must call containerMgr.Restore after NetworkMgr initialized, - // otherwize will panic - if err := containerMgr.Restore(ctx); err != nil { - return err - } - if err := d.addSystemLabels(); err != nil { return err } diff --git a/daemon/mgr/container.go b/daemon/mgr/container.go index 5dc8b82e72..97b5449672 100644 --- a/daemon/mgr/container.go +++ b/daemon/mgr/container.go @@ -1688,6 +1688,14 @@ func (mgr *ContainerManager) markStoppedAndRelease(c *Container, m *ctrd.Message mgr.IOs.Remove(c.ID) } + // NetworkMgr is nil, which means the pouch daemon is initializing. + // And the libnetwork will also initialize, which will release all + // staled network resources(endpoint, network and namespace). So we + // don't need release the network resources. + if mgr.NetworkMgr == nil { + return nil + } + // No network binded, just return if c.NetworkSettings == nil { return nil