commit.go 6.5 KB

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