commit.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. var (
  34. containerID, parentImageID string
  35. containerConfig *runconfig.Config
  36. )
  37. if container != nil {
  38. containerID = container.ID
  39. parentImageID = container.ImageID
  40. containerConfig = container.Config
  41. }
  42. img, err := daemon.graph.Create(rwTar, containerID, parentImageID, c.Comment, c.Author, containerConfig, c.Config)
  43. if err != nil {
  44. return nil, err
  45. }
  46. // Register the image if needed
  47. if c.Repo != "" {
  48. if err := daemon.repositories.Tag(c.Repo, c.Tag, img.ID, true); err != nil {
  49. return img, err
  50. }
  51. }
  52. container.logEvent("commit")
  53. return img, nil
  54. }