commit.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package daemon
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "runtime"
  7. "strings"
  8. "time"
  9. "github.com/docker/distribution/reference"
  10. "github.com/docker/docker/api/types/backend"
  11. containertypes "github.com/docker/docker/api/types/container"
  12. "github.com/docker/docker/builder/dockerfile"
  13. "github.com/docker/docker/container"
  14. "github.com/docker/docker/errdefs"
  15. "github.com/docker/docker/image"
  16. "github.com/docker/docker/layer"
  17. "github.com/docker/docker/pkg/ioutils"
  18. "github.com/docker/docker/pkg/system"
  19. "github.com/pkg/errors"
  20. )
  21. // merge merges two Config, the image container configuration (defaults values),
  22. // and the user container configuration, either passed by the API or generated
  23. // by the cli.
  24. // It will mutate the specified user configuration (userConf) with the image
  25. // configuration where the user configuration is incomplete.
  26. func merge(userConf, imageConf *containertypes.Config) error {
  27. if userConf.User == "" {
  28. userConf.User = imageConf.User
  29. }
  30. if len(userConf.ExposedPorts) == 0 {
  31. userConf.ExposedPorts = imageConf.ExposedPorts
  32. } else if imageConf.ExposedPorts != nil {
  33. for port := range imageConf.ExposedPorts {
  34. if _, exists := userConf.ExposedPorts[port]; !exists {
  35. userConf.ExposedPorts[port] = struct{}{}
  36. }
  37. }
  38. }
  39. if len(userConf.Env) == 0 {
  40. userConf.Env = imageConf.Env
  41. } else {
  42. for _, imageEnv := range imageConf.Env {
  43. found := false
  44. imageEnvKey := strings.Split(imageEnv, "=")[0]
  45. for _, userEnv := range userConf.Env {
  46. userEnvKey := strings.Split(userEnv, "=")[0]
  47. if runtime.GOOS == "windows" {
  48. // Case insensitive environment variables on Windows
  49. imageEnvKey = strings.ToUpper(imageEnvKey)
  50. userEnvKey = strings.ToUpper(userEnvKey)
  51. }
  52. if imageEnvKey == userEnvKey {
  53. found = true
  54. break
  55. }
  56. }
  57. if !found {
  58. userConf.Env = append(userConf.Env, imageEnv)
  59. }
  60. }
  61. }
  62. if userConf.Labels == nil {
  63. userConf.Labels = map[string]string{}
  64. }
  65. for l, v := range imageConf.Labels {
  66. if _, ok := userConf.Labels[l]; !ok {
  67. userConf.Labels[l] = v
  68. }
  69. }
  70. if len(userConf.Entrypoint) == 0 {
  71. if len(userConf.Cmd) == 0 {
  72. userConf.Cmd = imageConf.Cmd
  73. userConf.ArgsEscaped = imageConf.ArgsEscaped
  74. }
  75. if userConf.Entrypoint == nil {
  76. userConf.Entrypoint = imageConf.Entrypoint
  77. }
  78. }
  79. if imageConf.Healthcheck != nil {
  80. if userConf.Healthcheck == nil {
  81. userConf.Healthcheck = imageConf.Healthcheck
  82. } else {
  83. if len(userConf.Healthcheck.Test) == 0 {
  84. userConf.Healthcheck.Test = imageConf.Healthcheck.Test
  85. }
  86. if userConf.Healthcheck.Interval == 0 {
  87. userConf.Healthcheck.Interval = imageConf.Healthcheck.Interval
  88. }
  89. if userConf.Healthcheck.Timeout == 0 {
  90. userConf.Healthcheck.Timeout = imageConf.Healthcheck.Timeout
  91. }
  92. if userConf.Healthcheck.StartPeriod == 0 {
  93. userConf.Healthcheck.StartPeriod = imageConf.Healthcheck.StartPeriod
  94. }
  95. if userConf.Healthcheck.Retries == 0 {
  96. userConf.Healthcheck.Retries = imageConf.Healthcheck.Retries
  97. }
  98. }
  99. }
  100. if userConf.WorkingDir == "" {
  101. userConf.WorkingDir = imageConf.WorkingDir
  102. }
  103. if len(userConf.Volumes) == 0 {
  104. userConf.Volumes = imageConf.Volumes
  105. } else {
  106. for k, v := range imageConf.Volumes {
  107. userConf.Volumes[k] = v
  108. }
  109. }
  110. if userConf.StopSignal == "" {
  111. userConf.StopSignal = imageConf.StopSignal
  112. }
  113. return nil
  114. }
  115. // Commit creates a new filesystem image from the current state of a container.
  116. // The image can optionally be tagged into a repository.
  117. func (daemon *Daemon) Commit(name string, c *backend.ContainerCommitConfig) (string, error) {
  118. start := time.Now()
  119. container, err := daemon.GetContainer(name)
  120. if err != nil {
  121. return "", err
  122. }
  123. // It is not possible to commit a running container on Windows
  124. if (runtime.GOOS == "windows") && container.IsRunning() {
  125. return "", errors.Errorf("%+v does not support commit of a running container", runtime.GOOS)
  126. }
  127. if container.IsDead() {
  128. err := fmt.Errorf("You cannot commit container %s which is Dead", container.ID)
  129. return "", errdefs.Conflict(err)
  130. }
  131. if container.IsRemovalInProgress() {
  132. err := fmt.Errorf("You cannot commit container %s which is being removed", container.ID)
  133. return "", errdefs.Conflict(err)
  134. }
  135. if c.Pause && !container.IsPaused() {
  136. daemon.containerPause(container)
  137. defer daemon.containerUnpause(container)
  138. }
  139. if !system.IsOSSupported(container.OS) {
  140. return "", system.ErrNotSupportedOperatingSystem
  141. }
  142. if c.MergeConfigs && c.Config == nil {
  143. c.Config = container.Config
  144. }
  145. newConfig, err := dockerfile.BuildFromConfig(c.Config, c.Changes, container.OS)
  146. if err != nil {
  147. return "", err
  148. }
  149. if c.MergeConfigs {
  150. if err := merge(newConfig, container.Config); err != nil {
  151. return "", err
  152. }
  153. }
  154. rwTar, err := daemon.exportContainerRw(container)
  155. if err != nil {
  156. return "", err
  157. }
  158. defer func() {
  159. if rwTar != nil {
  160. rwTar.Close()
  161. }
  162. }()
  163. var parent *image.Image
  164. if container.ImageID == "" {
  165. parent = new(image.Image)
  166. parent.RootFS = image.NewRootFS()
  167. } else {
  168. parent, err = daemon.imageStore.Get(container.ImageID)
  169. if err != nil {
  170. return "", err
  171. }
  172. }
  173. l, err := daemon.layerStores[container.OS].Register(rwTar, parent.RootFS.ChainID())
  174. if err != nil {
  175. return "", err
  176. }
  177. defer layer.ReleaseAndLog(daemon.layerStores[container.OS], l)
  178. containerConfig := c.ContainerConfig
  179. if containerConfig == nil {
  180. containerConfig = container.Config
  181. }
  182. cc := image.ChildConfig{
  183. ContainerID: container.ID,
  184. Author: c.Author,
  185. Comment: c.Comment,
  186. ContainerConfig: containerConfig,
  187. Config: newConfig,
  188. DiffID: l.DiffID(),
  189. }
  190. config, err := json.Marshal(image.NewChildImage(parent, cc, container.OS))
  191. if err != nil {
  192. return "", err
  193. }
  194. id, err := daemon.imageStore.Create(config)
  195. if err != nil {
  196. return "", err
  197. }
  198. if container.ImageID != "" {
  199. if err := daemon.imageStore.SetParent(id, container.ImageID); err != nil {
  200. return "", err
  201. }
  202. }
  203. imageRef := ""
  204. if c.Repo != "" {
  205. newTag, err := reference.ParseNormalizedNamed(c.Repo) // todo: should move this to API layer
  206. if err != nil {
  207. return "", err
  208. }
  209. if !reference.IsNameOnly(newTag) {
  210. return "", errors.Errorf("unexpected repository name: %s", c.Repo)
  211. }
  212. if c.Tag != "" {
  213. if newTag, err = reference.WithTag(newTag, c.Tag); err != nil {
  214. return "", err
  215. }
  216. }
  217. if err := daemon.TagImageWithReference(id, newTag); err != nil {
  218. return "", err
  219. }
  220. imageRef = reference.FamiliarString(newTag)
  221. }
  222. attributes := map[string]string{
  223. "comment": c.Comment,
  224. "imageID": id.String(),
  225. "imageRef": imageRef,
  226. }
  227. daemon.LogContainerEventWithAttributes(container, "commit", attributes)
  228. containerActions.WithValues("commit").UpdateSince(start)
  229. return id.String(), nil
  230. }
  231. func (daemon *Daemon) exportContainerRw(container *container.Container) (arch io.ReadCloser, err error) {
  232. // Note: Indexing by OS is safe as only called from `Commit` which has already performed validation
  233. rwlayer, err := daemon.layerStores[container.OS].GetRWLayer(container.ID)
  234. if err != nil {
  235. return nil, err
  236. }
  237. defer func() {
  238. if err != nil {
  239. daemon.layerStores[container.OS].ReleaseRWLayer(rwlayer)
  240. }
  241. }()
  242. // TODO: this mount call is not necessary as we assume that TarStream() should
  243. // mount the layer if needed. But the Diff() function for windows requests that
  244. // the layer should be mounted when calling it. So we reserve this mount call
  245. // until windows driver can implement Diff() interface correctly.
  246. _, err = rwlayer.Mount(container.GetMountLabel())
  247. if err != nil {
  248. return nil, err
  249. }
  250. archive, err := rwlayer.TarStream()
  251. if err != nil {
  252. rwlayer.Unmount()
  253. return nil, err
  254. }
  255. return ioutils.NewReadCloserWrapper(archive, func() error {
  256. archive.Close()
  257. err = rwlayer.Unmount()
  258. daemon.layerStores[container.OS].ReleaseRWLayer(rwlayer)
  259. return err
  260. }),
  261. nil
  262. }