commit.go 3.4 KB

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