commit.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package daemon
  2. import (
  3. "github.com/docker/docker/graph"
  4. "github.com/docker/docker/runconfig"
  5. )
  6. type ContainerCommitConfig struct {
  7. Pause bool
  8. Repo string
  9. Tag string
  10. Author string
  11. Comment string
  12. Config *runconfig.Config
  13. }
  14. // Commit creates a new filesystem image from the current state of a container.
  15. // The image can optionally be tagged into a repository
  16. func (daemon *Daemon) Commit(container *Container, c *ContainerCommitConfig) (*graph.Image, error) {
  17. if c.Pause && !container.IsPaused() {
  18. container.Pause()
  19. defer container.Unpause()
  20. }
  21. rwTar, err := container.ExportRw()
  22. if err != nil {
  23. return nil, err
  24. }
  25. defer func() {
  26. if rwTar != nil {
  27. rwTar.Close()
  28. }
  29. }()
  30. // Create a new image from the container's base layers + a new layer from container changes
  31. var (
  32. containerID, parentImageID string
  33. containerConfig *runconfig.Config
  34. )
  35. if container != nil {
  36. containerID = container.ID
  37. parentImageID = container.ImageID
  38. containerConfig = container.Config
  39. }
  40. img, err := daemon.graph.Create(rwTar, containerID, parentImageID, c.Comment, c.Author, containerConfig, c.Config)
  41. if err != nil {
  42. return nil, err
  43. }
  44. // Register the image if needed
  45. if c.Repo != "" {
  46. if err := daemon.repositories.Tag(c.Repo, c.Tag, img.ID, true); err != nil {
  47. return img, err
  48. }
  49. }
  50. container.LogEvent("commit")
  51. return img, nil
  52. }