commit.go 1.4 KB

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