image_commit.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package daemon // import "github.com/docker/docker/daemon"
  2. import (
  3. "encoding/json"
  4. "io"
  5. "github.com/docker/docker/api/types/backend"
  6. "github.com/docker/docker/image"
  7. "github.com/docker/docker/layer"
  8. "github.com/docker/docker/pkg/ioutils"
  9. "github.com/docker/docker/pkg/system"
  10. "github.com/pkg/errors"
  11. )
  12. // CommitImage creates a new image from a commit config
  13. func (i *imageService) CommitImage(c backend.CommitConfig) (image.ID, error) {
  14. layerStore, ok := i.layerStores[c.ContainerOS]
  15. if !ok {
  16. return "", system.ErrNotSupportedOperatingSystem
  17. }
  18. rwTar, err := exportContainerRw(layerStore, c.ContainerID, c.ContainerMountLabel)
  19. if err != nil {
  20. return "", err
  21. }
  22. defer func() {
  23. if rwTar != nil {
  24. rwTar.Close()
  25. }
  26. }()
  27. var parent *image.Image
  28. if c.ParentImageID == "" {
  29. parent = new(image.Image)
  30. parent.RootFS = image.NewRootFS()
  31. } else {
  32. parent, err = i.imageStore.Get(image.ID(c.ParentImageID))
  33. if err != nil {
  34. return "", err
  35. }
  36. }
  37. l, err := layerStore.Register(rwTar, parent.RootFS.ChainID())
  38. if err != nil {
  39. return "", err
  40. }
  41. defer layer.ReleaseAndLog(layerStore, l)
  42. cc := image.ChildConfig{
  43. ContainerID: c.ContainerID,
  44. Author: c.Author,
  45. Comment: c.Comment,
  46. ContainerConfig: c.ContainerConfig,
  47. Config: c.Config,
  48. DiffID: l.DiffID(),
  49. }
  50. config, err := json.Marshal(image.NewChildImage(parent, cc, c.ContainerOS))
  51. if err != nil {
  52. return "", err
  53. }
  54. id, err := i.imageStore.Create(config)
  55. if err != nil {
  56. return "", err
  57. }
  58. if c.ParentImageID != "" {
  59. if err := i.imageStore.SetParent(id, image.ID(c.ParentImageID)); err != nil {
  60. return "", err
  61. }
  62. }
  63. return id, nil
  64. }
  65. func exportContainerRw(layerStore layer.Store, id, mountLabel string) (arch io.ReadCloser, err error) {
  66. rwlayer, err := layerStore.GetRWLayer(id)
  67. if err != nil {
  68. return nil, err
  69. }
  70. defer func() {
  71. if err != nil {
  72. layerStore.ReleaseRWLayer(rwlayer)
  73. }
  74. }()
  75. // TODO: this mount call is not necessary as we assume that TarStream() should
  76. // mount the layer if needed. But the Diff() function for windows requests that
  77. // the layer should be mounted when calling it. So we reserve this mount call
  78. // until windows driver can implement Diff() interface correctly.
  79. _, err = rwlayer.Mount(mountLabel)
  80. if err != nil {
  81. return nil, err
  82. }
  83. archive, err := rwlayer.TarStream()
  84. if err != nil {
  85. rwlayer.Unmount()
  86. return nil, err
  87. }
  88. return ioutils.NewReadCloserWrapper(archive, func() error {
  89. archive.Close()
  90. err = rwlayer.Unmount()
  91. layerStore.ReleaseRWLayer(rwlayer)
  92. return err
  93. }),
  94. nil
  95. }
  96. // CommitBuildStep is used by the builder to create an image for each step in
  97. // the build.
  98. //
  99. // This method is different from CreateImageFromContainer:
  100. // * it doesn't attempt to validate container state
  101. // * it doesn't send a commit action to metrics
  102. // * it doesn't log a container commit event
  103. //
  104. // This is a temporary shim. Should be removed when builder stops using commit.
  105. func (i *imageService) CommitBuildStep(c backend.CommitConfig) (image.ID, error) {
  106. container := i.containers.Get(c.ContainerID)
  107. if container == nil {
  108. // TODO: use typed error
  109. return "", errors.Errorf("container not found: %s", c.ContainerID)
  110. }
  111. c.ContainerMountLabel = container.MountLabel
  112. c.ContainerOS = container.OS
  113. c.ParentImageID = string(container.ImageID)
  114. return i.CommitImage(c)
  115. }