diff --git a/cmd/dockerd/daemon.go b/cmd/dockerd/daemon.go index 9a795046b6..b1fad91deb 100644 --- a/cmd/dockerd/daemon.go +++ b/cmd/dockerd/daemon.go @@ -164,7 +164,11 @@ func (cli *DaemonCli) start(opts *daemonOptions) (err error) { } ctx, cancel := context.WithCancel(context.Background()) - if err := cli.initContainerD(ctx); err != nil { + waitForContainerDShutdown, err := cli.initContainerD(ctx) + if waitForContainerDShutdown != nil { + defer waitForContainerDShutdown(10 * time.Second) + } + if err != nil { cancel() return err } diff --git a/cmd/dockerd/daemon_unix.go b/cmd/dockerd/daemon_unix.go index d224d0e2bd..2876c057ad 100644 --- a/cmd/dockerd/daemon_unix.go +++ b/cmd/dockerd/daemon_unix.go @@ -145,30 +145,31 @@ func newCgroupParent(config *config.Config) string { return cgroupParent } -func (cli *DaemonCli) initContainerD(ctx context.Context) error { +func (cli *DaemonCli) initContainerD(ctx context.Context) (func(time.Duration) error, error) { + var waitForShutdown func(time.Duration) error if cli.Config.ContainerdAddr == "" { systemContainerdAddr, ok, err := systemContainerdRunning(cli.Config.IsRootless()) if err != nil { - return errors.Wrap(err, "could not determine whether the system containerd is running") + return nil, errors.Wrap(err, "could not determine whether the system containerd is running") } if !ok { opts, err := cli.getContainerdDaemonOpts() if err != nil { - return errors.Wrap(err, "failed to generate containerd options") + return nil, errors.Wrap(err, "failed to generate containerd options") } r, err := supervisor.Start(ctx, filepath.Join(cli.Config.Root, "containerd"), filepath.Join(cli.Config.ExecRoot, "containerd"), opts...) if err != nil { - return errors.Wrap(err, "failed to start containerd") + return nil, errors.Wrap(err, "failed to start containerd") } cli.Config.ContainerdAddr = r.Address() // Try to wait for containerd to shutdown - defer r.WaitTimeout(10 * time.Second) + waitForShutdown = r.WaitTimeout } else { cli.Config.ContainerdAddr = systemContainerdAddr } } - return nil + return waitForShutdown, nil } diff --git a/cmd/dockerd/daemon_windows.go b/cmd/dockerd/daemon_windows.go index c6dd7408b1..bf7b9efa3f 100644 --- a/cmd/dockerd/daemon_windows.go +++ b/cmd/dockerd/daemon_windows.go @@ -5,6 +5,7 @@ import ( "fmt" "net" "os" + "time" "github.com/docker/docker/daemon/config" "github.com/docker/docker/libcontainerd/supervisor" @@ -88,7 +89,7 @@ func newCgroupParent(config *config.Config) string { return "" } -func (cli *DaemonCli) initContainerD(_ context.Context) error { +func (cli *DaemonCli) initContainerD(_ context.Context) (func(time.Duration) error, error) { system.InitContainerdRuntime(cli.Config.Experimental, cli.Config.ContainerdAddr) - return nil + return nil, nil }