commit.go 2.5 KB

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