update.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package daemon
  2. import (
  3. "fmt"
  4. "github.com/docker/docker/api/types/container"
  5. "github.com/pkg/errors"
  6. )
  7. // ContainerUpdate updates configuration of the container
  8. func (daemon *Daemon) ContainerUpdate(name string, hostConfig *container.HostConfig) (container.ContainerUpdateOKBody, error) {
  9. var warnings []string
  10. c, err := daemon.GetContainer(name)
  11. if err != nil {
  12. return container.ContainerUpdateOKBody{Warnings: warnings}, err
  13. }
  14. warnings, err = daemon.verifyContainerSettings(c.Platform, hostConfig, nil, true)
  15. if err != nil {
  16. return container.ContainerUpdateOKBody{Warnings: warnings}, validationError{err}
  17. }
  18. if err := daemon.update(name, hostConfig); err != nil {
  19. return container.ContainerUpdateOKBody{Warnings: warnings}, err
  20. }
  21. return container.ContainerUpdateOKBody{Warnings: warnings}, nil
  22. }
  23. func (daemon *Daemon) update(name string, hostConfig *container.HostConfig) error {
  24. if hostConfig == nil {
  25. return nil
  26. }
  27. container, err := daemon.GetContainer(name)
  28. if err != nil {
  29. return err
  30. }
  31. restoreConfig := false
  32. backupHostConfig := *container.HostConfig
  33. defer func() {
  34. if restoreConfig {
  35. container.Lock()
  36. container.HostConfig = &backupHostConfig
  37. container.CheckpointTo(daemon.containersReplica)
  38. container.Unlock()
  39. }
  40. }()
  41. if container.RemovalInProgress || container.Dead {
  42. return errCannotUpdate(container.ID, fmt.Errorf("container is marked for removal and cannot be \"update\""))
  43. }
  44. container.Lock()
  45. if err := container.UpdateContainer(hostConfig); err != nil {
  46. restoreConfig = true
  47. container.Unlock()
  48. return errCannotUpdate(container.ID, err)
  49. }
  50. if err := container.CheckpointTo(daemon.containersReplica); err != nil {
  51. restoreConfig = true
  52. container.Unlock()
  53. return errCannotUpdate(container.ID, err)
  54. }
  55. container.Unlock()
  56. // if Restart Policy changed, we need to update container monitor
  57. if hostConfig.RestartPolicy.Name != "" {
  58. container.UpdateMonitor(hostConfig.RestartPolicy)
  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. // TODO: it would be nice if containerd responded with better errors here so we can classify this better.
  68. return errCannotUpdate(container.ID, systemError{err})
  69. }
  70. }
  71. daemon.LogContainerEvent(container, "update")
  72. return nil
  73. }
  74. func errCannotUpdate(containerID string, err error) error {
  75. return errors.Wrap(err, "Cannot update container "+containerID)
  76. }