commit.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package daemon
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "runtime"
  6. "strings"
  7. "time"
  8. "github.com/docker/docker/container"
  9. "github.com/docker/docker/dockerversion"
  10. "github.com/docker/docker/image"
  11. "github.com/docker/docker/layer"
  12. "github.com/docker/docker/pkg/archive"
  13. "github.com/docker/docker/pkg/ioutils"
  14. "github.com/docker/docker/reference"
  15. "github.com/docker/engine-api/types"
  16. containertypes "github.com/docker/engine-api/types/container"
  17. "github.com/docker/go-connections/nat"
  18. )
  19. // merge merges two Config, the image container configuration (defaults values),
  20. // and the user container configuration, either passed by the API or generated
  21. // by the cli.
  22. // It will mutate the specified user configuration (userConf) with the image
  23. // configuration where the user configuration is incomplete.
  24. func merge(userConf, imageConf *containertypes.Config) error {
  25. if userConf.User == "" {
  26. userConf.User = imageConf.User
  27. }
  28. if len(userConf.ExposedPorts) == 0 {
  29. userConf.ExposedPorts = imageConf.ExposedPorts
  30. } else if imageConf.ExposedPorts != nil {
  31. if userConf.ExposedPorts == nil {
  32. userConf.ExposedPorts = make(nat.PortSet)
  33. }
  34. for port := range imageConf.ExposedPorts {
  35. if _, exists := userConf.ExposedPorts[port]; !exists {
  36. userConf.ExposedPorts[port] = struct{}{}
  37. }
  38. }
  39. }
  40. if len(userConf.Env) == 0 {
  41. userConf.Env = imageConf.Env
  42. } else {
  43. for _, imageEnv := range imageConf.Env {
  44. found := false
  45. imageEnvKey := strings.Split(imageEnv, "=")[0]
  46. for _, userEnv := range userConf.Env {
  47. userEnvKey := strings.Split(userEnv, "=")[0]
  48. if imageEnvKey == userEnvKey {
  49. found = true
  50. break
  51. }
  52. }
  53. if !found {
  54. userConf.Env = append(userConf.Env, imageEnv)
  55. }
  56. }
  57. }
  58. if userConf.Labels == nil {
  59. userConf.Labels = map[string]string{}
  60. }
  61. if imageConf.Labels != nil {
  62. for l := range userConf.Labels {
  63. imageConf.Labels[l] = userConf.Labels[l]
  64. }
  65. userConf.Labels = imageConf.Labels
  66. }
  67. if userConf.Entrypoint.Len() == 0 {
  68. if userConf.Cmd.Len() == 0 {
  69. userConf.Cmd = imageConf.Cmd
  70. }
  71. if userConf.Entrypoint == nil {
  72. userConf.Entrypoint = imageConf.Entrypoint
  73. }
  74. }
  75. if userConf.WorkingDir == "" {
  76. userConf.WorkingDir = imageConf.WorkingDir
  77. }
  78. if len(userConf.Volumes) == 0 {
  79. userConf.Volumes = imageConf.Volumes
  80. } else {
  81. for k, v := range imageConf.Volumes {
  82. userConf.Volumes[k] = v
  83. }
  84. }
  85. return nil
  86. }
  87. // Commit creates a new filesystem image from the current state of a container.
  88. // The image can optionally be tagged into a repository.
  89. func (daemon *Daemon) Commit(name string, c *types.ContainerCommitConfig) (string, error) {
  90. container, err := daemon.GetContainer(name)
  91. if err != nil {
  92. return "", err
  93. }
  94. // It is not possible to commit a running container on Windows
  95. if runtime.GOOS == "windows" && container.IsRunning() {
  96. return "", fmt.Errorf("Windows does not support commit of a running container")
  97. }
  98. if c.Pause && !container.IsPaused() {
  99. daemon.containerPause(container)
  100. defer daemon.containerUnpause(container)
  101. }
  102. if c.MergeConfigs {
  103. if err := merge(c.Config, container.Config); err != nil {
  104. return "", err
  105. }
  106. }
  107. rwTar, err := daemon.exportContainerRw(container)
  108. if err != nil {
  109. return "", err
  110. }
  111. defer func() {
  112. if rwTar != nil {
  113. rwTar.Close()
  114. }
  115. }()
  116. var history []image.History
  117. rootFS := image.NewRootFS()
  118. if container.ImageID != "" {
  119. img, err := daemon.imageStore.Get(container.ImageID)
  120. if err != nil {
  121. return "", err
  122. }
  123. history = img.History
  124. rootFS = img.RootFS
  125. }
  126. l, err := daemon.layerStore.Register(rwTar, rootFS.ChainID())
  127. if err != nil {
  128. return "", err
  129. }
  130. defer layer.ReleaseAndLog(daemon.layerStore, l)
  131. h := image.History{
  132. Author: c.Author,
  133. Created: time.Now().UTC(),
  134. CreatedBy: strings.Join(container.Config.Cmd.Slice(), " "),
  135. Comment: c.Comment,
  136. EmptyLayer: true,
  137. }
  138. if diffID := l.DiffID(); layer.DigestSHA256EmptyTar != diffID {
  139. h.EmptyLayer = false
  140. rootFS.Append(diffID)
  141. }
  142. history = append(history, h)
  143. config, err := json.Marshal(&image.Image{
  144. V1Image: image.V1Image{
  145. DockerVersion: dockerversion.Version,
  146. Config: c.Config,
  147. Architecture: runtime.GOARCH,
  148. OS: runtime.GOOS,
  149. Container: container.ID,
  150. ContainerConfig: *container.Config,
  151. Author: c.Author,
  152. Created: h.Created,
  153. },
  154. RootFS: rootFS,
  155. History: history,
  156. })
  157. if err != nil {
  158. return "", err
  159. }
  160. id, err := daemon.imageStore.Create(config)
  161. if err != nil {
  162. return "", err
  163. }
  164. if container.ImageID != "" {
  165. if err := daemon.imageStore.SetParent(id, container.ImageID); err != nil {
  166. return "", err
  167. }
  168. }
  169. if c.Repo != "" {
  170. newTag, err := reference.WithName(c.Repo) // todo: should move this to API layer
  171. if err != nil {
  172. return "", err
  173. }
  174. if c.Tag != "" {
  175. if newTag, err = reference.WithTag(newTag, c.Tag); err != nil {
  176. return "", err
  177. }
  178. }
  179. if err := daemon.TagImage(newTag, id.String()); err != nil {
  180. return "", err
  181. }
  182. }
  183. attributes := map[string]string{
  184. "comment": c.Comment,
  185. }
  186. daemon.LogContainerEventWithAttributes(container, "commit", attributes)
  187. return id.String(), nil
  188. }
  189. func (daemon *Daemon) exportContainerRw(container *container.Container) (archive.Archive, error) {
  190. if err := daemon.Mount(container); err != nil {
  191. return nil, err
  192. }
  193. archive, err := container.RWLayer.TarStream()
  194. if err != nil {
  195. return nil, err
  196. }
  197. return ioutils.NewReadCloserWrapper(archive, func() error {
  198. archive.Close()
  199. return container.RWLayer.Unmount()
  200. }),
  201. nil
  202. }