commit.go 2.4 KB

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