commit.go 6.8 KB

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