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