commit.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.Retries == 0 {
  89. userConf.Healthcheck.Retries = imageConf.Healthcheck.Retries
  90. }
  91. }
  92. }
  93. if userConf.WorkingDir == "" {
  94. userConf.WorkingDir = imageConf.WorkingDir
  95. }
  96. if len(userConf.Volumes) == 0 {
  97. userConf.Volumes = imageConf.Volumes
  98. } else {
  99. for k, v := range imageConf.Volumes {
  100. userConf.Volumes[k] = v
  101. }
  102. }
  103. if userConf.StopSignal == "" {
  104. userConf.StopSignal = imageConf.StopSignal
  105. }
  106. return nil
  107. }
  108. // CreateImageFromContainer creates a new image from a container. The container
  109. // config will be updated by applying the change set to the custom config, then
  110. // applying that config over the existing container config.
  111. func (daemon *Daemon) CreateImageFromContainer(ctx context.Context, name string, c *backend.CreateImageConfig) (string, error) {
  112. start := time.Now()
  113. container, err := daemon.GetContainer(name)
  114. if err != nil {
  115. return "", err
  116. }
  117. // It is not possible to commit a running container on Windows
  118. if isWindows && container.IsRunning() {
  119. return "", errors.Errorf("%+v does not support commit of a running container", runtime.GOOS)
  120. }
  121. if container.IsDead() {
  122. err := fmt.Errorf("You cannot commit container %s which is Dead", container.ID)
  123. return "", errdefs.Conflict(err)
  124. }
  125. if container.IsRemovalInProgress() {
  126. err := fmt.Errorf("You cannot commit container %s which is being removed", container.ID)
  127. return "", errdefs.Conflict(err)
  128. }
  129. if c.Pause && !container.IsPaused() {
  130. daemon.containerPause(container)
  131. defer daemon.containerUnpause(container)
  132. }
  133. if c.Config == nil {
  134. c.Config = container.Config
  135. }
  136. newConfig, err := dockerfile.BuildFromConfig(ctx, c.Config, c.Changes, container.OS)
  137. if err != nil {
  138. return "", err
  139. }
  140. if err := merge(newConfig, container.Config); err != nil {
  141. return "", err
  142. }
  143. id, err := daemon.imageService.CommitImage(ctx, backend.CommitConfig{
  144. Author: c.Author,
  145. Comment: c.Comment,
  146. Config: newConfig,
  147. ContainerConfig: container.Config,
  148. ContainerID: container.ID,
  149. ContainerMountLabel: container.MountLabel,
  150. ContainerOS: container.OS,
  151. ParentImageID: string(container.ImageID),
  152. })
  153. if err != nil {
  154. return "", err
  155. }
  156. imageRef := ""
  157. if c.Tag != nil {
  158. err = daemon.imageService.TagImage(ctx, id, c.Tag)
  159. if err != nil {
  160. return "", err
  161. }
  162. imageRef = reference.FamiliarString(c.Tag)
  163. }
  164. daemon.LogContainerEventWithAttributes(container, "commit", map[string]string{
  165. "comment": c.Comment,
  166. "imageID": id.String(),
  167. "imageRef": imageRef,
  168. })
  169. containerActions.WithValues("commit").UpdateSince(start)
  170. return id.String(), nil
  171. }