d222bf097c
The existing runtimes reload logic went to great lengths to replace the directory containing runtime wrapper scripts as atomically as possible within the limitations of the Linux filesystem ABI. Trouble is, atomically swapping the wrapper scripts directory solves the wrong problem! The runtime configuration is "locked in" when a container is started, including the path to the runC binary. If a container is started with a runtime which requires a daemon-managed wrapper script and then the daemon is reloaded with a config which no longer requires the wrapper script (i.e. some args -> no args, or the runtime is dropped from the config), that container would become unmanageable. Any attempts to stop, exec or otherwise perform lifecycle management operations on the container are likely to fail due to the wrapper script no longer existing at its original path. Atomically swapping the wrapper scripts is also incompatible with the read-copy-update paradigm for reloading configuration. A handler in the daemon could retain a reference to the pre-reload configuration for an indeterminate amount of time after the daemon configuration has been reloaded and updated. It is possible for the daemon to attempt to start a container using a deleted wrapper script if a request to run a container races a reload. Solve the problem of deleting referenced wrapper scripts by ensuring that all wrapper scripts are *immutable* for the lifetime of the daemon process. Any given runtime wrapper script must always exist with the same contents, no matter how many times the daemon config is reloaded, or what changes are made to the config. This is accomplished by using everyone's favourite design pattern: content-addressable storage. Each wrapper script file name is suffixed with the SHA-256 digest of its contents to (probabilistically) guarantee immutability without needing any concurrency control. Stale runtime wrapper scripts are only cleaned up on the next daemon restart. Split the derived runtimes configuration from the user-supplied configuration to have a place to store derived state without mutating the user-supplied configuration or exposing daemon internals in API struct types. Hold the derived state and the user-supplied configuration in a single struct value so that they can be updated as an atomic unit. Signed-off-by: Cory Snider <csnider@mirantis.com>
70 lines
2.4 KiB
Go
70 lines
2.4 KiB
Go
package daemon // import "github.com/docker/docker/daemon"
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
|
"github.com/docker/docker/container"
|
|
)
|
|
|
|
// ContainerRestart stops and starts a container. It attempts to
|
|
// gracefully stop the container within the given timeout, forcefully
|
|
// stopping it if the timeout is exceeded. If given a negative
|
|
// timeout, ContainerRestart will wait forever until a graceful
|
|
// stop. Returns an error if the container cannot be found, or if
|
|
// there is an underlying error at any stage of the restart.
|
|
func (daemon *Daemon) ContainerRestart(ctx context.Context, name string, options containertypes.StopOptions) error {
|
|
ctr, err := daemon.GetContainer(name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = daemon.containerRestart(ctx, daemon.config(), ctr, options)
|
|
if err != nil {
|
|
return fmt.Errorf("Cannot restart container %s: %v", name, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// containerRestart attempts to gracefully stop and then start the
|
|
// container. When stopping, wait for the given duration in seconds to
|
|
// gracefully stop, before forcefully terminating the container. If
|
|
// given a negative duration, wait forever for a graceful stop.
|
|
func (daemon *Daemon) containerRestart(ctx context.Context, daemonCfg *configStore, container *container.Container, options containertypes.StopOptions) error {
|
|
// Determine isolation. If not specified in the hostconfig, use daemon default.
|
|
actualIsolation := container.HostConfig.Isolation
|
|
if containertypes.Isolation.IsDefault(actualIsolation) {
|
|
actualIsolation = daemon.defaultIsolation
|
|
}
|
|
|
|
// Avoid unnecessarily unmounting and then directly mounting
|
|
// the container when the container stops and then starts
|
|
// again. We do not do this for Hyper-V isolated containers
|
|
// (implying also on Windows) as the HCS must have exclusive
|
|
// access to mount the containers filesystem inside the utility
|
|
// VM.
|
|
if !containertypes.Isolation.IsHyperV(actualIsolation) {
|
|
if err := daemon.Mount(container); err == nil {
|
|
defer daemon.Unmount(container)
|
|
}
|
|
}
|
|
|
|
if container.IsRunning() {
|
|
container.Lock()
|
|
container.HasBeenManuallyRestarted = true
|
|
container.Unlock()
|
|
|
|
err := daemon.containerStop(ctx, container, options)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if err := daemon.containerStart(ctx, daemonCfg, container, "", "", true); err != nil {
|
|
return err
|
|
}
|
|
|
|
daemon.LogContainerEvent(container, "restart")
|
|
return nil
|
|
}
|