commit.go 6.6 KB

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