commit.go 5.3 KB

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