commit.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package daemon
  2. import (
  3. "github.com/docker/docker/image"
  4. "github.com/docker/docker/runconfig"
  5. )
  6. // ContainerCommitConfig contains build configs for commit operation,
  7. // and is used when making a commit with the current state of the container.
  8. type ContainerCommitConfig struct {
  9. Pause bool
  10. Repo string
  11. Tag string
  12. Author string
  13. Comment string
  14. Config *runconfig.Config
  15. }
  16. // Commit creates a new filesystem image from the current state of a container.
  17. // The image can optionally be tagged into a repository.
  18. func (daemon *Daemon) Commit(container *Container, c *ContainerCommitConfig) (*image.Image, error) {
  19. if c.Pause && !container.isPaused() {
  20. container.pause()
  21. defer container.unpause()
  22. }
  23. rwTar, err := container.exportRw()
  24. if err != nil {
  25. return nil, err
  26. }
  27. defer func() {
  28. if rwTar != nil {
  29. rwTar.Close()
  30. }
  31. }()
  32. // Create a new image from the container's base layers + a new layer from container changes
  33. img, err := daemon.graph.Create(rwTar, container.ID, container.ImageID, c.Comment, c.Author, container.Config, c.Config)
  34. if err != nil {
  35. return nil, err
  36. }
  37. // Register the image if needed
  38. if c.Repo != "" {
  39. if err := daemon.repositories.Tag(c.Repo, c.Tag, img.ID, true); err != nil {
  40. return img, err
  41. }
  42. }
  43. container.logEvent("commit")
  44. return img, nil
  45. }