commit.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package daemon // import "github.com/docker/docker/daemon"
  2. import (
  3. "context"
  4. "fmt"
  5. "runtime"
  6. "strings"
  7. "time"
  8. "github.com/docker/docker/api/types/backend"
  9. containertypes "github.com/docker/docker/api/types/container"
  10. "github.com/docker/docker/builder/dockerfile"
  11. "github.com/docker/docker/errdefs"
  12. "github.com/pkg/errors"
  13. )
  14. // merge merges two Config, the image container configuration (defaults values),
  15. // and the user container configuration, either passed by the API or generated
  16. // by the cli.
  17. // It will mutate the specified user configuration (userConf) with the image
  18. // configuration where the user configuration is incomplete.
  19. func merge(userConf, imageConf *containertypes.Config) error {
  20. if userConf.User == "" {
  21. userConf.User = imageConf.User
  22. }
  23. if len(userConf.ExposedPorts) == 0 {
  24. userConf.ExposedPorts = imageConf.ExposedPorts
  25. } else if imageConf.ExposedPorts != nil {
  26. for port := range imageConf.ExposedPorts {
  27. if _, exists := userConf.ExposedPorts[port]; !exists {
  28. userConf.ExposedPorts[port] = struct{}{}
  29. }
  30. }
  31. }
  32. if len(userConf.Env) == 0 {
  33. userConf.Env = imageConf.Env
  34. } else {
  35. for _, imageEnv := range imageConf.Env {
  36. found := false
  37. imageEnvKey, _, _ := strings.Cut(imageEnv, "=")
  38. for _, userEnv := range userConf.Env {
  39. userEnvKey, _, _ := strings.Cut(userEnv, "=")
  40. if isWindows {
  41. // Case insensitive environment variables on Windows
  42. found = strings.EqualFold(imageEnvKey, userEnvKey)
  43. } else {
  44. found = imageEnvKey == userEnvKey
  45. }
  46. if found {
  47. break
  48. }
  49. }
  50. if !found {
  51. userConf.Env = append(userConf.Env, imageEnv)
  52. }
  53. }
  54. }
  55. if userConf.Labels == nil {
  56. userConf.Labels = map[string]string{}
  57. }
  58. for l, v := range imageConf.Labels {
  59. if _, ok := userConf.Labels[l]; !ok {
  60. userConf.Labels[l] = v
  61. }
  62. }
  63. if len(userConf.Entrypoint) == 0 {
  64. if len(userConf.Cmd) == 0 {
  65. userConf.Cmd = imageConf.Cmd
  66. }
  67. if userConf.Entrypoint == nil {
  68. userConf.Entrypoint = imageConf.Entrypoint
  69. }
  70. }
  71. if imageConf.Healthcheck != nil {
  72. if userConf.Healthcheck == nil {
  73. userConf.Healthcheck = imageConf.Healthcheck
  74. } else {
  75. if len(userConf.Healthcheck.Test) == 0 {
  76. userConf.Healthcheck.Test = imageConf.Healthcheck.Test
  77. }
  78. if userConf.Healthcheck.Interval == 0 {
  79. userConf.Healthcheck.Interval = imageConf.Healthcheck.Interval
  80. }
  81. if userConf.Healthcheck.Timeout == 0 {
  82. userConf.Healthcheck.Timeout = imageConf.Healthcheck.Timeout
  83. }
  84. if userConf.Healthcheck.StartPeriod == 0 {
  85. userConf.Healthcheck.StartPeriod = imageConf.Healthcheck.StartPeriod
  86. }
  87. if userConf.Healthcheck.Retries == 0 {
  88. userConf.Healthcheck.Retries = imageConf.Healthcheck.Retries
  89. }
  90. }
  91. }
  92. if userConf.WorkingDir == "" {
  93. userConf.WorkingDir = imageConf.WorkingDir
  94. }
  95. if len(userConf.Volumes) == 0 {
  96. userConf.Volumes = imageConf.Volumes
  97. } else {
  98. for k, v := range imageConf.Volumes {
  99. userConf.Volumes[k] = v
  100. }
  101. }
  102. if userConf.StopSignal == "" {
  103. userConf.StopSignal = imageConf.StopSignal
  104. }
  105. return nil
  106. }
  107. // CreateImageFromContainer creates a new image from a container. The container
  108. // config will be updated by applying the change set to the custom config, then
  109. // applying that config over the existing container config.
  110. func (daemon *Daemon) CreateImageFromContainer(ctx context.Context, name string, c *backend.CreateImageConfig) (string, error) {
  111. start := time.Now()
  112. container, err := daemon.GetContainer(name)
  113. if err != nil {
  114. return "", err
  115. }
  116. // It is not possible to commit a running container on Windows
  117. if isWindows && container.IsRunning() {
  118. return "", errors.Errorf("%+v does not support commit of a running container", runtime.GOOS)
  119. }
  120. if container.IsDead() {
  121. err := fmt.Errorf("You cannot commit container %s which is Dead", container.ID)
  122. return "", errdefs.Conflict(err)
  123. }
  124. if container.IsRemovalInProgress() {
  125. err := fmt.Errorf("You cannot commit container %s which is being removed", container.ID)
  126. return "", errdefs.Conflict(err)
  127. }
  128. if c.Pause && !container.IsPaused() {
  129. daemon.containerPause(container)
  130. defer daemon.containerUnpause(container)
  131. }
  132. if c.Config == nil {
  133. c.Config = container.Config
  134. }
  135. newConfig, err := dockerfile.BuildFromConfig(ctx, c.Config, c.Changes, container.OS)
  136. if err != nil {
  137. return "", err
  138. }
  139. if err := merge(newConfig, container.Config); err != nil {
  140. return "", err
  141. }
  142. id, err := daemon.imageService.CommitImage(backend.CommitConfig{
  143. Author: c.Author,
  144. Comment: c.Comment,
  145. Config: newConfig,
  146. ContainerConfig: container.Config,
  147. ContainerID: container.ID,
  148. ContainerMountLabel: container.MountLabel,
  149. ContainerOS: container.OS,
  150. ParentImageID: string(container.ImageID),
  151. })
  152. if err != nil {
  153. return "", err
  154. }
  155. var imageRef string
  156. if c.Repo != "" {
  157. imageRef, err = daemon.imageService.TagImage(string(id), c.Repo, c.Tag)
  158. if err != nil {
  159. return "", err
  160. }
  161. }
  162. daemon.LogContainerEventWithAttributes(container, "commit", map[string]string{
  163. "comment": c.Comment,
  164. "imageID": id.String(),
  165. "imageRef": imageRef,
  166. })
  167. containerActions.WithValues("commit").UpdateSince(start)
  168. return id.String(), nil
  169. }