commit.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package daemon
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io"
  6. "github.com/Sirupsen/logrus"
  7. "github.com/docker/docker/engine"
  8. "github.com/docker/docker/image"
  9. "github.com/docker/docker/runconfig"
  10. )
  11. type ContainerCommitConfig struct {
  12. Pause bool
  13. Repo string
  14. Tag string
  15. Author string
  16. Comment string
  17. Changes []string
  18. Config io.ReadCloser
  19. }
  20. func (daemon *Daemon) ContainerCommit(name string, c *ContainerCommitConfig) (string, error) {
  21. container, err := daemon.Get(name)
  22. if err != nil {
  23. return "", err
  24. }
  25. var (
  26. subenv engine.Env
  27. config = container.Config
  28. stdoutBuffer = bytes.NewBuffer(nil)
  29. newConfig runconfig.Config
  30. )
  31. if err := subenv.Decode(c.Config); err != nil {
  32. logrus.Errorf("%s", err)
  33. }
  34. buildConfigJob := daemon.eng.Job("build_config")
  35. buildConfigJob.Stdout.Add(stdoutBuffer)
  36. buildConfigJob.SetenvList("changes", c.Changes)
  37. // FIXME this should be remove when we remove deprecated config param
  38. buildConfigJob.SetenvSubEnv("config", &subenv)
  39. if err := buildConfigJob.Run(); err != nil {
  40. return "", err
  41. }
  42. if err := json.NewDecoder(stdoutBuffer).Decode(&newConfig); err != nil {
  43. return "", err
  44. }
  45. if err := runconfig.Merge(&newConfig, config); err != nil {
  46. return "", err
  47. }
  48. img, err := daemon.Commit(container, c.Repo, c.Tag, c.Comment, c.Author, c.Pause, &newConfig)
  49. if err != nil {
  50. return "", err
  51. }
  52. return img.ID, nil
  53. }
  54. // Commit creates a new filesystem image from the current state of a container.
  55. // The image can optionally be tagged into a repository
  56. func (daemon *Daemon) Commit(container *Container, repository, tag, comment, author string, pause bool, config *runconfig.Config) (*image.Image, error) {
  57. if pause && !container.IsPaused() {
  58. container.Pause()
  59. defer container.Unpause()
  60. }
  61. if err := container.Mount(); err != nil {
  62. return nil, err
  63. }
  64. defer container.Unmount()
  65. rwTar, err := container.ExportRw()
  66. if err != nil {
  67. return nil, err
  68. }
  69. defer rwTar.Close()
  70. // Create a new image from the container's base layers + a new layer from container changes
  71. var (
  72. containerID, parentImageID string
  73. containerConfig *runconfig.Config
  74. )
  75. if container != nil {
  76. containerID = container.ID
  77. parentImageID = container.ImageID
  78. containerConfig = container.Config
  79. }
  80. img, err := daemon.graph.Create(rwTar, containerID, parentImageID, comment, author, containerConfig, config)
  81. if err != nil {
  82. return nil, err
  83. }
  84. // Register the image if needed
  85. if repository != "" {
  86. if err := daemon.repositories.Set(repository, tag, img.ID, true); err != nil {
  87. return img, err
  88. }
  89. }
  90. return img, nil
  91. }