commit.go 5.5 KB

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