commit.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. var newRef reference.Named
  114. if c.Repo != "" {
  115. ref, err := reference.ParseNormalizedNamed(c.Repo)
  116. if err != nil {
  117. return "", errdefs.InvalidParameter(err)
  118. }
  119. if c.Tag != "" {
  120. ref, err = reference.WithTag(ref, c.Tag)
  121. if err != nil {
  122. return "", errdefs.InvalidParameter(err)
  123. }
  124. } else {
  125. ref = reference.TagNameOnly(ref)
  126. }
  127. newRef = ref
  128. }
  129. container, err := daemon.GetContainer(name)
  130. if err != nil {
  131. return "", err
  132. }
  133. // It is not possible to commit a running container on Windows
  134. if isWindows && container.IsRunning() {
  135. return "", errors.Errorf("%+v does not support commit of a running container", runtime.GOOS)
  136. }
  137. if container.IsDead() {
  138. err := fmt.Errorf("You cannot commit container %s which is Dead", container.ID)
  139. return "", errdefs.Conflict(err)
  140. }
  141. if container.IsRemovalInProgress() {
  142. err := fmt.Errorf("You cannot commit container %s which is being removed", container.ID)
  143. return "", errdefs.Conflict(err)
  144. }
  145. if c.Pause && !container.IsPaused() {
  146. daemon.containerPause(container)
  147. defer daemon.containerUnpause(container)
  148. }
  149. if c.Config == nil {
  150. c.Config = container.Config
  151. }
  152. newConfig, err := dockerfile.BuildFromConfig(ctx, c.Config, c.Changes, container.OS)
  153. if err != nil {
  154. return "", err
  155. }
  156. if err := merge(newConfig, container.Config); err != nil {
  157. return "", err
  158. }
  159. id, err := daemon.imageService.CommitImage(ctx, backend.CommitConfig{
  160. Author: c.Author,
  161. Comment: c.Comment,
  162. Config: newConfig,
  163. ContainerConfig: container.Config,
  164. ContainerID: container.ID,
  165. ContainerMountLabel: container.MountLabel,
  166. ContainerOS: container.OS,
  167. ParentImageID: string(container.ImageID),
  168. })
  169. if err != nil {
  170. return "", err
  171. }
  172. imageRef := ""
  173. if newRef != nil {
  174. err = daemon.imageService.TagImage(ctx, id, newRef)
  175. if err != nil {
  176. return "", err
  177. }
  178. imageRef = reference.FamiliarString(newRef)
  179. }
  180. daemon.LogContainerEventWithAttributes(container, "commit", map[string]string{
  181. "comment": c.Comment,
  182. "imageID": id.String(),
  183. "imageRef": imageRef,
  184. })
  185. containerActions.WithValues("commit").UpdateSince(start)
  186. return id.String(), nil
  187. }