commit.go 5.1 KB

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