update.go 2.2 KB

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