commit.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package daemon
  2. import (
  3. "github.com/docker/docker/image"
  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. Changes []string
  13. Config *runconfig.Config
  14. }
  15. // Commit creates a new filesystem image from the current state of a container.
  16. // The image can optionally be tagged into a repository
  17. func (daemon *Daemon) Commit(container *Container, repository, tag, comment, author string, pause bool, config *runconfig.Config) (*image.Image, error) {
  18. if pause && !container.IsPaused() {
  19. container.Pause()
  20. defer container.Unpause()
  21. }
  22. if err := container.Mount(); err != nil {
  23. return nil, err
  24. }
  25. defer container.Unmount()
  26. rwTar, err := container.ExportRw()
  27. if err != nil {
  28. return nil, err
  29. }
  30. defer func() {
  31. if rwTar != nil {
  32. rwTar.Close()
  33. }
  34. }()
  35. // Create a new image from the container's base layers + a new layer from container changes
  36. var (
  37. containerID, parentImageID string
  38. containerConfig *runconfig.Config
  39. )
  40. if container != nil {
  41. containerID = container.ID
  42. parentImageID = container.ImageID
  43. containerConfig = container.Config
  44. }
  45. img, err := daemon.graph.Create(rwTar, containerID, parentImageID, comment, author, containerConfig, config)
  46. if err != nil {
  47. return nil, err
  48. }
  49. // Register the image if needed
  50. if repository != "" {
  51. if err := daemon.repositories.Tag(repository, tag, img.ID, true); err != nil {
  52. return img, err
  53. }
  54. }
  55. container.LogEvent("commit")
  56. return img, nil
  57. }