commit.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package daemon
  2. import (
  3. "encoding/json"
  4. "io"
  5. "runtime"
  6. "strings"
  7. "time"
  8. "github.com/docker/distribution/reference"
  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/image"
  14. "github.com/docker/docker/layer"
  15. "github.com/docker/docker/pkg/ioutils"
  16. "github.com/pkg/errors"
  17. )
  18. // merge merges two Config, the image container configuration (defaults values),
  19. // and the user container configuration, either passed by the API or generated
  20. // by the cli.
  21. // It will mutate the specified user configuration (userConf) with the image
  22. // configuration where the user configuration is incomplete.
  23. func merge(userConf, imageConf *containertypes.Config) error {
  24. if userConf.User == "" {
  25. userConf.User = imageConf.User
  26. }
  27. if len(userConf.ExposedPorts) == 0 {
  28. userConf.ExposedPorts = imageConf.ExposedPorts
  29. } else if imageConf.ExposedPorts != nil {
  30. for port := range imageConf.ExposedPorts {
  31. if _, exists := userConf.ExposedPorts[port]; !exists {
  32. userConf.ExposedPorts[port] = struct{}{}
  33. }
  34. }
  35. }
  36. if len(userConf.Env) == 0 {
  37. userConf.Env = imageConf.Env
  38. } else {
  39. for _, imageEnv := range imageConf.Env {
  40. found := false
  41. imageEnvKey := strings.Split(imageEnv, "=")[0]
  42. for _, userEnv := range userConf.Env {
  43. userEnvKey := strings.Split(userEnv, "=")[0]
  44. if runtime.GOOS == "windows" {
  45. // Case insensitive environment variables on Windows
  46. imageEnvKey = strings.ToUpper(imageEnvKey)
  47. userEnvKey = strings.ToUpper(userEnvKey)
  48. }
  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. for l, v := range imageConf.Labels {
  63. if _, ok := userConf.Labels[l]; !ok {
  64. userConf.Labels[l] = v
  65. }
  66. }
  67. if len(userConf.Entrypoint) == 0 {
  68. if len(userConf.Cmd) == 0 {
  69. userConf.Cmd = imageConf.Cmd
  70. userConf.ArgsEscaped = imageConf.ArgsEscaped
  71. }
  72. if userConf.Entrypoint == nil {
  73. userConf.Entrypoint = imageConf.Entrypoint
  74. }
  75. }
  76. if imageConf.Healthcheck != nil {
  77. if userConf.Healthcheck == nil {
  78. userConf.Healthcheck = imageConf.Healthcheck
  79. } else {
  80. if len(userConf.Healthcheck.Test) == 0 {
  81. userConf.Healthcheck.Test = imageConf.Healthcheck.Test
  82. }
  83. if userConf.Healthcheck.Interval == 0 {
  84. userConf.Healthcheck.Interval = imageConf.Healthcheck.Interval
  85. }
  86. if userConf.Healthcheck.Timeout == 0 {
  87. userConf.Healthcheck.Timeout = imageConf.Healthcheck.Timeout
  88. }
  89. if userConf.Healthcheck.StartPeriod == 0 {
  90. userConf.Healthcheck.StartPeriod = imageConf.Healthcheck.StartPeriod
  91. }
  92. if userConf.Healthcheck.Retries == 0 {
  93. userConf.Healthcheck.Retries = imageConf.Healthcheck.Retries
  94. }
  95. }
  96. }
  97. if userConf.WorkingDir == "" {
  98. userConf.WorkingDir = imageConf.WorkingDir
  99. }
  100. if len(userConf.Volumes) == 0 {
  101. userConf.Volumes = imageConf.Volumes
  102. } else {
  103. for k, v := range imageConf.Volumes {
  104. userConf.Volumes[k] = v
  105. }
  106. }
  107. if userConf.StopSignal == "" {
  108. userConf.StopSignal = imageConf.StopSignal
  109. }
  110. return nil
  111. }
  112. // Commit creates a new filesystem image from the current state of a container.
  113. // The image can optionally be tagged into a repository.
  114. func (daemon *Daemon) Commit(name string, c *backend.ContainerCommitConfig) (string, error) {
  115. start := time.Now()
  116. container, err := daemon.GetContainer(name)
  117. if err != nil {
  118. return "", err
  119. }
  120. // It is not possible to commit a running container on Windows and on Solaris.
  121. if (runtime.GOOS == "windows" || runtime.GOOS == "solaris") && container.IsRunning() {
  122. return "", errors.Errorf("%+v does not support commit of a running container", runtime.GOOS)
  123. }
  124. if c.Pause && !container.IsPaused() {
  125. daemon.containerPause(container)
  126. defer daemon.containerUnpause(container)
  127. }
  128. newConfig, err := dockerfile.BuildFromConfig(c.Config, c.Changes)
  129. if err != nil {
  130. return "", err
  131. }
  132. if c.MergeConfigs {
  133. if err := merge(newConfig, container.Config); err != nil {
  134. return "", err
  135. }
  136. }
  137. rwTar, err := daemon.exportContainerRw(container)
  138. if err != nil {
  139. return "", err
  140. }
  141. defer func() {
  142. if rwTar != nil {
  143. rwTar.Close()
  144. }
  145. }()
  146. var parent *image.Image
  147. if container.ImageID == "" {
  148. parent = new(image.Image)
  149. parent.RootFS = image.NewRootFS()
  150. } else {
  151. parent, err = daemon.stores[container.Platform].imageStore.Get(container.ImageID)
  152. if err != nil {
  153. return "", err
  154. }
  155. }
  156. l, err := daemon.stores[container.Platform].layerStore.Register(rwTar, rootFS.ChainID(), layer.Platform(container.Platform))
  157. if err != nil {
  158. return "", err
  159. }
  160. defer layer.ReleaseAndLog(daemon.stores[container.Platform].layerStore, l)
  161. containerConfig := c.ContainerConfig
  162. if containerConfig == nil {
  163. containerConfig = container.Config
  164. }
  165. cc := image.ChildConfig{
  166. ContainerID: container.ID,
  167. Author: c.Author,
  168. Comment: c.Comment,
  169. ContainerConfig: containerConfig,
  170. Config: newConfig,
  171. DiffID: l.DiffID(),
  172. }
  173. config, err := json.Marshal(image.NewChildImage(parent, cc, container.Platform))
  174. if err != nil {
  175. return "", err
  176. }
  177. id, err := daemon.stores[container.Platform].imageStore.Create(config)
  178. if err != nil {
  179. return "", err
  180. }
  181. if container.ImageID != "" {
  182. if err := daemon.stores[container.Platform].imageStore.SetParent(id, container.ImageID); err != nil {
  183. return "", err
  184. }
  185. }
  186. imageRef := ""
  187. if c.Repo != "" {
  188. newTag, err := reference.ParseNormalizedNamed(c.Repo) // todo: should move this to API layer
  189. if err != nil {
  190. return "", err
  191. }
  192. if !reference.IsNameOnly(newTag) {
  193. return "", errors.Errorf("unexpected repository name: %s", c.Repo)
  194. }
  195. if c.Tag != "" {
  196. if newTag, err = reference.WithTag(newTag, c.Tag); err != nil {
  197. return "", err
  198. }
  199. }
  200. if err := daemon.TagImageWithReference(id, container.Platform, newTag); err != nil {
  201. return "", err
  202. }
  203. imageRef = reference.FamiliarString(newTag)
  204. }
  205. attributes := map[string]string{
  206. "comment": c.Comment,
  207. "imageID": id.String(),
  208. "imageRef": imageRef,
  209. }
  210. daemon.LogContainerEventWithAttributes(container, "commit", attributes)
  211. containerActions.WithValues("commit").UpdateSince(start)
  212. return id.String(), nil
  213. }
  214. func (daemon *Daemon) exportContainerRw(container *container.Container) (io.ReadCloser, error) {
  215. if err := daemon.Mount(container); err != nil {
  216. return nil, err
  217. }
  218. archive, err := container.RWLayer.TarStream()
  219. if err != nil {
  220. daemon.Unmount(container) // logging is already handled in the `Unmount` function
  221. return nil, err
  222. }
  223. return ioutils.NewReadCloserWrapper(archive, func() error {
  224. archive.Close()
  225. return container.RWLayer.Unmount()
  226. }),
  227. nil
  228. }