commit.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 len(userConf.Entrypoint) == 0 {
  68. if len(userConf.Cmd) == 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. if userConf.StopSignal == "" {
  86. userConf.StopSignal = imageConf.StopSignal
  87. }
  88. return nil
  89. }
  90. // Commit creates a new filesystem image from the current state of a container.
  91. // The image can optionally be tagged into a repository.
  92. func (daemon *Daemon) Commit(name string, c *types.ContainerCommitConfig) (string, error) {
  93. container, err := daemon.GetContainer(name)
  94. if err != nil {
  95. return "", err
  96. }
  97. // It is not possible to commit a running container on Windows
  98. if runtime.GOOS == "windows" && container.IsRunning() {
  99. return "", fmt.Errorf("Windows does not support commit of a running container")
  100. }
  101. if c.Pause && !container.IsPaused() {
  102. daemon.containerPause(container)
  103. defer daemon.containerUnpause(container)
  104. }
  105. if c.MergeConfigs {
  106. if err := merge(c.Config, container.Config); err != nil {
  107. return "", err
  108. }
  109. }
  110. rwTar, err := daemon.exportContainerRw(container)
  111. if err != nil {
  112. return "", err
  113. }
  114. defer func() {
  115. if rwTar != nil {
  116. rwTar.Close()
  117. }
  118. }()
  119. var history []image.History
  120. rootFS := image.NewRootFS()
  121. if container.ImageID != "" {
  122. img, err := daemon.imageStore.Get(container.ImageID)
  123. if err != nil {
  124. return "", err
  125. }
  126. history = img.History
  127. rootFS = img.RootFS
  128. }
  129. l, err := daemon.layerStore.Register(rwTar, rootFS.ChainID())
  130. if err != nil {
  131. return "", err
  132. }
  133. defer layer.ReleaseAndLog(daemon.layerStore, l)
  134. h := image.History{
  135. Author: c.Author,
  136. Created: time.Now().UTC(),
  137. CreatedBy: strings.Join(container.Config.Cmd, " "),
  138. Comment: c.Comment,
  139. EmptyLayer: true,
  140. }
  141. if diffID := l.DiffID(); layer.DigestSHA256EmptyTar != diffID {
  142. h.EmptyLayer = false
  143. rootFS.Append(diffID)
  144. }
  145. history = append(history, h)
  146. config, err := json.Marshal(&image.Image{
  147. V1Image: image.V1Image{
  148. DockerVersion: dockerversion.Version,
  149. Config: c.Config,
  150. Architecture: runtime.GOARCH,
  151. OS: runtime.GOOS,
  152. Container: container.ID,
  153. ContainerConfig: *container.Config,
  154. Author: c.Author,
  155. Created: h.Created,
  156. },
  157. RootFS: rootFS,
  158. History: history,
  159. })
  160. if err != nil {
  161. return "", err
  162. }
  163. id, err := daemon.imageStore.Create(config)
  164. if err != nil {
  165. return "", err
  166. }
  167. if container.ImageID != "" {
  168. if err := daemon.imageStore.SetParent(id, container.ImageID); err != nil {
  169. return "", err
  170. }
  171. }
  172. if c.Repo != "" {
  173. newTag, err := reference.WithName(c.Repo) // todo: should move this to API layer
  174. if err != nil {
  175. return "", err
  176. }
  177. if c.Tag != "" {
  178. if newTag, err = reference.WithTag(newTag, c.Tag); err != nil {
  179. return "", err
  180. }
  181. }
  182. if err := daemon.TagImage(newTag, id.String()); err != nil {
  183. return "", err
  184. }
  185. }
  186. attributes := map[string]string{
  187. "comment": c.Comment,
  188. }
  189. daemon.LogContainerEventWithAttributes(container, "commit", attributes)
  190. return id.String(), nil
  191. }
  192. func (daemon *Daemon) exportContainerRw(container *container.Container) (archive.Archive, error) {
  193. if err := daemon.Mount(container); err != nil {
  194. return nil, err
  195. }
  196. archive, err := container.RWLayer.TarStream()
  197. if err != nil {
  198. daemon.Unmount(container) // logging is already handled in the `Unmount` function
  199. return nil, err
  200. }
  201. return ioutils.NewReadCloserWrapper(archive, func() error {
  202. archive.Close()
  203. return container.RWLayer.Unmount()
  204. }),
  205. nil
  206. }