commit.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package daemon
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "runtime"
  6. "strings"
  7. "time"
  8. "github.com/docker/distribution/reference"
  9. "github.com/docker/docker/container"
  10. "github.com/docker/docker/dockerversion"
  11. "github.com/docker/docker/image"
  12. "github.com/docker/docker/layer"
  13. "github.com/docker/docker/pkg/archive"
  14. "github.com/docker/docker/pkg/ioutils"
  15. "github.com/docker/docker/runconfig"
  16. )
  17. // ContainerCommitConfig contains build configs for commit operation,
  18. // and is used when making a commit with the current state of the container.
  19. type ContainerCommitConfig struct {
  20. Pause bool
  21. Repo string
  22. Tag string
  23. Author string
  24. Comment string
  25. // merge container config into commit config before commit
  26. MergeConfigs bool
  27. Config *runconfig.Config
  28. }
  29. // Commit creates a new filesystem image from the current state of a container.
  30. // The image can optionally be tagged into a repository.
  31. func (daemon *Daemon) Commit(name string, c *ContainerCommitConfig) (string, error) {
  32. container, err := daemon.Get(name)
  33. if err != nil {
  34. return "", err
  35. }
  36. // It is not possible to commit a running container on Windows
  37. if runtime.GOOS == "windows" && container.IsRunning() {
  38. return "", fmt.Errorf("Windows does not support commit of a running container")
  39. }
  40. if c.Pause && !container.IsPaused() {
  41. daemon.containerPause(container)
  42. defer daemon.containerUnpause(container)
  43. }
  44. if c.MergeConfigs {
  45. if err := runconfig.Merge(c.Config, container.Config); err != nil {
  46. return "", err
  47. }
  48. }
  49. rwTar, err := daemon.exportContainerRw(container)
  50. if err != nil {
  51. return "", err
  52. }
  53. defer func() {
  54. if rwTar != nil {
  55. rwTar.Close()
  56. }
  57. }()
  58. var history []image.History
  59. rootFS := image.NewRootFS()
  60. if container.ImageID != "" {
  61. img, err := daemon.imageStore.Get(container.ImageID)
  62. if err != nil {
  63. return "", err
  64. }
  65. history = img.History
  66. rootFS = img.RootFS
  67. }
  68. l, err := daemon.layerStore.Register(rwTar, rootFS.ChainID())
  69. if err != nil {
  70. return "", err
  71. }
  72. defer layer.ReleaseAndLog(daemon.layerStore, l)
  73. h := image.History{
  74. Author: c.Author,
  75. Created: time.Now().UTC(),
  76. CreatedBy: strings.Join(container.Config.Cmd.Slice(), " "),
  77. Comment: c.Comment,
  78. EmptyLayer: true,
  79. }
  80. if diffID := l.DiffID(); layer.DigestSHA256EmptyTar != diffID {
  81. h.EmptyLayer = false
  82. rootFS.Append(diffID)
  83. }
  84. history = append(history, h)
  85. config, err := json.Marshal(&image.Image{
  86. V1Image: image.V1Image{
  87. DockerVersion: dockerversion.Version,
  88. Config: c.Config,
  89. Architecture: runtime.GOARCH,
  90. OS: runtime.GOOS,
  91. Container: container.ID,
  92. ContainerConfig: *container.Config,
  93. Author: c.Author,
  94. Created: h.Created,
  95. },
  96. RootFS: rootFS,
  97. History: history,
  98. })
  99. if err != nil {
  100. return "", err
  101. }
  102. id, err := daemon.imageStore.Create(config)
  103. if err != nil {
  104. return "", err
  105. }
  106. if container.ImageID != "" {
  107. if err := daemon.imageStore.SetParent(id, container.ImageID); err != nil {
  108. return "", err
  109. }
  110. }
  111. if c.Repo != "" {
  112. newTag, err := reference.WithName(c.Repo) // todo: should move this to API layer
  113. if err != nil {
  114. return "", err
  115. }
  116. if c.Tag != "" {
  117. if newTag, err = reference.WithTag(newTag, c.Tag); err != nil {
  118. return "", err
  119. }
  120. }
  121. if err := daemon.TagImage(newTag, id.String()); err != nil {
  122. return "", err
  123. }
  124. }
  125. daemon.LogContainerEvent(container, "commit")
  126. return id.String(), nil
  127. }
  128. func (daemon *Daemon) exportContainerRw(container *container.Container) (archive.Archive, error) {
  129. if err := daemon.Mount(container); err != nil {
  130. return nil, err
  131. }
  132. archive, err := container.RWLayer.TarStream()
  133. if err != nil {
  134. return nil, err
  135. }
  136. return ioutils.NewReadCloserWrapper(archive, func() error {
  137. archive.Close()
  138. return daemon.layerStore.Unmount(container.ID)
  139. }),
  140. nil
  141. }