commit.go 5.3 KB

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