github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/daemon/update.go (about) 1 package daemon 2 3 import ( 4 "fmt" 5 6 "github.com/docker/docker/api/types/container" 7 ) 8 9 // ContainerUpdate updates configuration of the container 10 func (daemon *Daemon) ContainerUpdate(name string, hostConfig *container.HostConfig) (container.ContainerUpdateOKBody, error) { 11 var warnings []string 12 13 warnings, err := daemon.verifyContainerSettings(hostConfig, nil, true) 14 if err != nil { 15 return container.ContainerUpdateOKBody{Warnings: warnings}, err 16 } 17 18 if err := daemon.update(name, hostConfig); err != nil { 19 return container.ContainerUpdateOKBody{Warnings: warnings}, err 20 } 21 22 return container.ContainerUpdateOKBody{Warnings: warnings}, nil 23 } 24 25 func (daemon *Daemon) update(name string, hostConfig *container.HostConfig) error { 26 if hostConfig == nil { 27 return nil 28 } 29 30 container, err := daemon.GetContainer(name) 31 if err != nil { 32 return err 33 } 34 35 restoreConfig := false 36 backupHostConfig := *container.HostConfig 37 defer func() { 38 if restoreConfig { 39 container.Lock() 40 container.HostConfig = &backupHostConfig 41 container.ToDisk() 42 container.Unlock() 43 } 44 }() 45 46 if container.RemovalInProgress || container.Dead { 47 return errCannotUpdate(container.ID, fmt.Errorf("Container is marked for removal and cannot be \"update\".")) 48 } 49 50 if err := container.UpdateContainer(hostConfig); err != nil { 51 restoreConfig = true 52 return errCannotUpdate(container.ID, err) 53 } 54 55 // if Restart Policy changed, we need to update container monitor 56 if hostConfig.RestartPolicy.Name != "" { 57 container.UpdateMonitor(hostConfig.RestartPolicy) 58 } 59 60 // If container is not running, update hostConfig struct is enough, 61 // resources will be updated when the container is started again. 62 // If container is running (including paused), we need to update configs 63 // to the real world. 64 if container.IsRunning() && !container.IsRestarting() { 65 if err := daemon.containerd.UpdateResources(container.ID, toContainerdResources(hostConfig.Resources)); err != nil { 66 restoreConfig = true 67 return errCannotUpdate(container.ID, err) 68 } 69 } 70 71 daemon.LogContainerEvent(container, "update") 72 73 return nil 74 } 75 76 func errCannotUpdate(containerID string, err error) error { 77 return fmt.Errorf("Cannot update container %s: %v", containerID, err) 78 }