8799c4fc |
package daemon
import (
"fmt"
|
907407d0 |
"github.com/docker/engine-api/types/container" |
8799c4fc |
)
|
ff3ea4c9 |
// ContainerUpdate updates configuration of the container |
8799c4fc |
func (daemon *Daemon) ContainerUpdate(name string, hostConfig *container.HostConfig) ([]string, error) {
var warnings []string
|
8ae6f6ac |
warnings, err := daemon.verifyContainerSettings(hostConfig, nil, true) |
8799c4fc |
if err != nil {
return warnings, err
}
if err := daemon.update(name, hostConfig); err != nil {
return warnings, err
}
return warnings, nil
}
|
9c332b16 |
// ContainerUpdateCmdOnBuild updates Path and Args for the container with ID cID.
func (daemon *Daemon) ContainerUpdateCmdOnBuild(cID string, cmd []string) error { |
bb05c188 |
if len(cmd) == 0 {
return nil
} |
9c332b16 |
c, err := daemon.GetContainer(cID)
if err != nil {
return err
}
c.Path = cmd[0]
c.Args = cmd[1:]
return nil
}
|
8799c4fc |
func (daemon *Daemon) update(name string, hostConfig *container.HostConfig) error {
if hostConfig == nil {
return nil
}
container, err := daemon.GetContainer(name)
if err != nil {
return err
}
|
fa3b1971 |
restoreConfig := false
backupHostConfig := *container.HostConfig
defer func() {
if restoreConfig {
container.Lock()
container.HostConfig = &backupHostConfig
container.ToDisk()
container.Unlock()
}
}()
|
8799c4fc |
if container.RemovalInProgress || container.Dead { |
a793564b |
return errCannotUpdate(container.ID, fmt.Errorf("Container is marked for removal and cannot be \"update\".")) |
8799c4fc |
}
if container.IsRunning() && hostConfig.KernelMemory != 0 { |
a793564b |
return errCannotUpdate(container.ID, fmt.Errorf("Can not update kernel memory to a running container, please stop it first.")) |
8799c4fc |
}
if err := container.UpdateContainer(hostConfig); err != nil { |
fa3b1971 |
restoreConfig = true |
a793564b |
return errCannotUpdate(container.ID, err) |
8799c4fc |
}
|
ff3ea4c9 |
// if Restart Policy changed, we need to update container monitor
container.UpdateMonitor(hostConfig.RestartPolicy)
|
8799c4fc |
// If container is not running, update hostConfig struct is enough,
// resources will be updated when the container is started again.
// If container is running (including paused), we need to update configs
// to the real world. |
ff3ea4c9 |
if container.IsRunning() && !container.IsRestarting() { |
9c4570a9 |
if err := daemon.containerd.UpdateResources(container.ID, toContainerdResources(hostConfig.Resources)); err != nil { |
fa3b1971 |
restoreConfig = true |
a793564b |
return errCannotUpdate(container.ID, err) |
8799c4fc |
}
}
|
72f1881d |
daemon.LogContainerEvent(container, "update")
|
8799c4fc |
return nil
} |
a793564b |
func errCannotUpdate(containerID string, err error) error {
return fmt.Errorf("Cannot update container %s: %v", containerID, err)
} |