mirror of https://github.com/docker/docs.git
Only restart containers on daemon load with policy of always
Signed-off-by: Michael Crosby <michael@docker.com>
This commit is contained in:
parent
617edd89f4
commit
41870a42be
|
@ -172,20 +172,24 @@ func (daemon *Daemon) load(id string) (*Container, error) {
|
||||||
if err := container.FromDisk(); err != nil {
|
if err := container.FromDisk(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if container.ID != id {
|
if container.ID != id {
|
||||||
return container, fmt.Errorf("Container %s is stored at %s", container.ID, id)
|
return container, fmt.Errorf("Container %s is stored at %s", container.ID, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
container.readHostConfig()
|
||||||
|
|
||||||
return container, nil
|
return container, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register makes a container object usable by the daemon as <container.ID>
|
// Register makes a container object usable by the daemon as <container.ID>
|
||||||
// This is a wrapper for register
|
// This is a wrapper for register
|
||||||
func (daemon *Daemon) Register(container *Container) error {
|
func (daemon *Daemon) Register(container *Container) error {
|
||||||
return daemon.register(container, true, nil)
|
return daemon.register(container, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// register makes a container object usable by the daemon as <container.ID>
|
// register makes a container object usable by the daemon as <container.ID>
|
||||||
func (daemon *Daemon) register(container *Container, updateSuffixarray bool, containersToStart *[]*Container) error {
|
func (daemon *Daemon) register(container *Container, updateSuffixarray bool) error {
|
||||||
if container.daemon != nil || daemon.Exists(container.ID) {
|
if container.daemon != nil || daemon.Exists(container.ID) {
|
||||||
return fmt.Errorf("Container is already loaded")
|
return fmt.Errorf("Container is already loaded")
|
||||||
}
|
}
|
||||||
|
@ -257,14 +261,6 @@ func (daemon *Daemon) register(container *Container, updateSuffixarray bool, con
|
||||||
if err := container.ToDisk(); err != nil {
|
if err := container.ToDisk(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if daemon.config.AutoRestart {
|
|
||||||
log.Debugf("Marking as restarting")
|
|
||||||
|
|
||||||
if containersToStart != nil {
|
|
||||||
*containersToStart = append(*containersToStart, container)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -296,10 +292,9 @@ func (daemon *Daemon) LogToDisk(src *broadcastwriter.BroadcastWriter, dst, strea
|
||||||
|
|
||||||
func (daemon *Daemon) restore() error {
|
func (daemon *Daemon) restore() error {
|
||||||
var (
|
var (
|
||||||
debug = (os.Getenv("DEBUG") != "" || os.Getenv("TEST") != "")
|
debug = (os.Getenv("DEBUG") != "" || os.Getenv("TEST") != "")
|
||||||
containers = make(map[string]*Container)
|
containers = make(map[string]*Container)
|
||||||
currentDriver = daemon.driver.String()
|
currentDriver = daemon.driver.String()
|
||||||
containersToStart = []*Container{}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if !debug {
|
if !debug {
|
||||||
|
@ -322,24 +317,33 @@ func (daemon *Daemon) restore() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ignore the container if it does not support the current driver being used by the graph
|
// Ignore the container if it does not support the current driver being used by the graph
|
||||||
if container.Driver == "" && currentDriver == "aufs" || container.Driver == currentDriver {
|
if (container.Driver == "" && currentDriver == "aufs") || container.Driver == currentDriver {
|
||||||
log.Debugf("Loaded container %v", container.ID)
|
log.Debugf("Loaded container %v", container.ID)
|
||||||
|
|
||||||
containers[container.ID] = container
|
containers[container.ID] = container
|
||||||
} else {
|
} else {
|
||||||
log.Debugf("Cannot load container %s because it was created with another graph driver.", container.ID)
|
log.Debugf("Cannot load container %s because it was created with another graph driver.", container.ID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
registeredContainers := []*Container{}
|
||||||
|
|
||||||
if entities := daemon.containerGraph.List("/", -1); entities != nil {
|
if entities := daemon.containerGraph.List("/", -1); entities != nil {
|
||||||
for _, p := range entities.Paths() {
|
for _, p := range entities.Paths() {
|
||||||
if !debug {
|
if !debug {
|
||||||
fmt.Print(".")
|
fmt.Print(".")
|
||||||
}
|
}
|
||||||
|
|
||||||
e := entities[p]
|
e := entities[p]
|
||||||
|
|
||||||
if container, ok := containers[e.ID()]; ok {
|
if container, ok := containers[e.ID()]; ok {
|
||||||
if err := daemon.register(container, false, &containersToStart); err != nil {
|
if err := daemon.register(container, false); err != nil {
|
||||||
log.Debugf("Failed to register container %s: %s", container.ID, err)
|
log.Debugf("Failed to register container %s: %s", container.ID, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
registeredContainers = append(registeredContainers, container)
|
||||||
|
|
||||||
|
// delete from the map so that a new name is not automatically generated
|
||||||
delete(containers, e.ID())
|
delete(containers, e.ID())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -352,15 +356,27 @@ func (daemon *Daemon) restore() error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugf("Setting default id - %s", err)
|
log.Debugf("Setting default id - %s", err)
|
||||||
}
|
}
|
||||||
if err := daemon.register(container, false, &containersToStart); err != nil {
|
|
||||||
|
if err := daemon.register(container, false); err != nil {
|
||||||
log.Debugf("Failed to register container %s: %s", container.ID, err)
|
log.Debugf("Failed to register container %s: %s", container.ID, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
registeredContainers = append(registeredContainers, container)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, container := range containersToStart {
|
// check the restart policy on the containers and restart any container with
|
||||||
log.Debugf("Starting container %d", container.ID)
|
// the restart policy of "always"
|
||||||
if err := container.Start(); err != nil {
|
if daemon.config.AutoRestart {
|
||||||
log.Debugf("Failed to start container %s: %s", container.ID, err)
|
log.Debugf("Restarting containers...")
|
||||||
|
|
||||||
|
for _, container := range registeredContainers {
|
||||||
|
if container.hostConfig.RestartPolicy.Name == "always" {
|
||||||
|
utils.Debugf("Starting container %s", container.ID)
|
||||||
|
|
||||||
|
if err := container.Start(); err != nil {
|
||||||
|
utils.Debugf("Failed to start container %s: %s", container.ID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -62,15 +62,13 @@ func (m *containerMonitor) Close() error {
|
||||||
// Cleanup networking and mounts
|
// Cleanup networking and mounts
|
||||||
m.container.cleanup()
|
m.container.cleanup()
|
||||||
|
|
||||||
if m.container.daemon != nil && m.container.daemon.srv != nil && m.container.daemon.srv.IsRunning() {
|
// FIXME: here is race condition between two RUN instructions in Dockerfile
|
||||||
// FIXME: here is race condition between two RUN instructions in Dockerfile
|
// because they share same runconfig and change image. Must be fixed
|
||||||
// because they share same runconfig and change image. Must be fixed
|
// in builder/builder.go
|
||||||
// in builder/builder.go
|
if err := m.container.toDisk(); err != nil {
|
||||||
if err := m.container.toDisk(); err != nil {
|
utils.Errorf("Error dumping container %s state to disk: %s\n", m.container.ID, err)
|
||||||
utils.Errorf("Error dumping container %s state to disk: %s\n", m.container.ID, err)
|
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in New Issue