commit.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. rwTar, err := container.ExportRw()
  23. if err != nil {
  24. return nil, err
  25. }
  26. defer func() {
  27. if rwTar != nil {
  28. rwTar.Close()
  29. }
  30. }()
  31. // Create a new image from the container's base layers + a new layer from container changes
  32. var (
  33. containerID, parentImageID string
  34. containerConfig *runconfig.Config
  35. )
  36. if container != nil {
  37. containerID = container.ID
  38. parentImageID = container.ImageID
  39. containerConfig = container.Config
  40. }
  41. img, err := daemon.graph.Create(rwTar, containerID, parentImageID, comment, author, containerConfig, config)
  42. if err != nil {
  43. return nil, err
  44. }
  45. // Register the image if needed
  46. if repository != "" {
  47. if err := daemon.repositories.Tag(repository, tag, img.ID, true); err != nil {
  48. return img, err
  49. }
  50. }
  51. return img, nil
  52. }