commit.go 3.7 KB

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