commit.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package daemon
  2. import (
  3. "fmt"
  4. "runtime"
  5. "github.com/docker/docker/image"
  6. "github.com/docker/docker/pkg/archive"
  7. "github.com/docker/docker/pkg/ioutils"
  8. "github.com/docker/docker/runconfig"
  9. )
  10. // ContainerCommitConfig contains build configs for commit operation,
  11. // and is used when making a commit with the current state of the container.
  12. type ContainerCommitConfig struct {
  13. Pause bool
  14. Repo string
  15. Tag string
  16. Author string
  17. Comment string
  18. // merge container config into commit config before commit
  19. MergeConfigs bool
  20. Config *runconfig.Config
  21. }
  22. // Commit creates a new filesystem image from the current state of a container.
  23. // The image can optionally be tagged into a repository.
  24. func (daemon *Daemon) Commit(name string, c *ContainerCommitConfig) (*image.Image, error) {
  25. container, err := daemon.Get(name)
  26. if err != nil {
  27. return nil, err
  28. }
  29. // It is not possible to commit a running container on Windows
  30. if runtime.GOOS == "windows" && container.IsRunning() {
  31. return nil, fmt.Errorf("Windows does not support commit of a running container")
  32. }
  33. if c.Pause && !container.isPaused() {
  34. daemon.containerPause(container)
  35. defer daemon.containerUnpause(container)
  36. }
  37. if c.MergeConfigs {
  38. if err := runconfig.Merge(c.Config, container.Config); err != nil {
  39. return nil, err
  40. }
  41. }
  42. rwTar, err := daemon.exportContainerRw(container)
  43. if err != nil {
  44. return nil, err
  45. }
  46. defer func() {
  47. if rwTar != nil {
  48. rwTar.Close()
  49. }
  50. }()
  51. // Create a new image from the container's base layers + a new layer from container changes
  52. img, err := daemon.graph.Create(rwTar, container.ID, container.ImageID, c.Comment, c.Author, container.Config, c.Config)
  53. if err != nil {
  54. return nil, err
  55. }
  56. // Register the image if needed
  57. if c.Repo != "" {
  58. if err := daemon.repositories.Tag(c.Repo, c.Tag, img.ID, true); err != nil {
  59. return img, err
  60. }
  61. }
  62. daemon.LogContainerEvent(container, "commit")
  63. return img, nil
  64. }
  65. func (daemon *Daemon) exportContainerRw(container *Container) (archive.Archive, error) {
  66. archive, err := daemon.diff(container)
  67. if err != nil {
  68. return nil, err
  69. }
  70. return ioutils.NewReadCloserWrapper(archive, func() error {
  71. err := archive.Close()
  72. return err
  73. }),
  74. nil
  75. }