commit.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package daemon // import "github.com/docker/docker/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/errdefs"
  13. "github.com/docker/docker/image"
  14. "github.com/docker/docker/layer"
  15. "github.com/docker/docker/pkg/ioutils"
  16. "github.com/docker/docker/pkg/system"
  17. "github.com/pkg/errors"
  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. for l, v := range imageConf.Labels {
  64. if _, ok := userConf.Labels[l]; !ok {
  65. userConf.Labels[l] = v
  66. }
  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.StartPeriod == 0 {
  91. userConf.Healthcheck.StartPeriod = imageConf.Healthcheck.StartPeriod
  92. }
  93. if userConf.Healthcheck.Retries == 0 {
  94. userConf.Healthcheck.Retries = imageConf.Healthcheck.Retries
  95. }
  96. }
  97. }
  98. if userConf.WorkingDir == "" {
  99. userConf.WorkingDir = imageConf.WorkingDir
  100. }
  101. if len(userConf.Volumes) == 0 {
  102. userConf.Volumes = imageConf.Volumes
  103. } else {
  104. for k, v := range imageConf.Volumes {
  105. userConf.Volumes[k] = v
  106. }
  107. }
  108. if userConf.StopSignal == "" {
  109. userConf.StopSignal = imageConf.StopSignal
  110. }
  111. return nil
  112. }
  113. // CreateImageFromContainer creates a new image from a container. The container
  114. // config will be updated by applying the change set to the custom config, then
  115. // applying that config over the existing container config.
  116. func (daemon *Daemon) CreateImageFromContainer(name string, c *backend.CreateImageConfig) (string, error) {
  117. start := time.Now()
  118. container, err := daemon.GetContainer(name)
  119. if err != nil {
  120. return "", err
  121. }
  122. // It is not possible to commit a running container on Windows
  123. if (runtime.GOOS == "windows") && container.IsRunning() {
  124. return "", errors.Errorf("%+v does not support commit of a running container", runtime.GOOS)
  125. }
  126. if container.IsDead() {
  127. err := fmt.Errorf("You cannot commit container %s which is Dead", container.ID)
  128. return "", errdefs.Conflict(err)
  129. }
  130. if container.IsRemovalInProgress() {
  131. err := fmt.Errorf("You cannot commit container %s which is being removed", container.ID)
  132. return "", errdefs.Conflict(err)
  133. }
  134. if c.Pause && !container.IsPaused() {
  135. daemon.containerPause(container)
  136. defer daemon.containerUnpause(container)
  137. }
  138. if c.Config == nil {
  139. c.Config = container.Config
  140. }
  141. newConfig, err := dockerfile.BuildFromConfig(c.Config, c.Changes, container.OS)
  142. if err != nil {
  143. return "", err
  144. }
  145. if err := merge(newConfig, container.Config); err != nil {
  146. return "", err
  147. }
  148. id, err := daemon.commitImage(backend.CommitConfig{
  149. Author: c.Author,
  150. Comment: c.Comment,
  151. Config: newConfig,
  152. ContainerConfig: container.Config,
  153. ContainerID: container.ID,
  154. ContainerMountLabel: container.MountLabel,
  155. ContainerOS: container.OS,
  156. ParentImageID: string(container.ImageID),
  157. })
  158. if err != nil {
  159. return "", err
  160. }
  161. var imageRef string
  162. if c.Repo != "" {
  163. imageRef, err = daemon.TagImage(string(id), c.Repo, c.Tag)
  164. if err != nil {
  165. return "", err
  166. }
  167. }
  168. daemon.LogContainerEventWithAttributes(container, "commit", map[string]string{
  169. "comment": c.Comment,
  170. "imageID": id.String(),
  171. "imageRef": imageRef,
  172. })
  173. containerActions.WithValues("commit").UpdateSince(start)
  174. return id.String(), nil
  175. }
  176. func (daemon *Daemon) commitImage(c backend.CommitConfig) (image.ID, error) {
  177. layerStore, ok := daemon.layerStores[c.ContainerOS]
  178. if !ok {
  179. return "", system.ErrNotSupportedOperatingSystem
  180. }
  181. rwTar, err := exportContainerRw(layerStore, c.ContainerID, c.ContainerMountLabel)
  182. if err != nil {
  183. return "", err
  184. }
  185. defer func() {
  186. if rwTar != nil {
  187. rwTar.Close()
  188. }
  189. }()
  190. var parent *image.Image
  191. if c.ParentImageID == "" {
  192. parent = new(image.Image)
  193. parent.RootFS = image.NewRootFS()
  194. } else {
  195. parent, err = daemon.imageStore.Get(image.ID(c.ParentImageID))
  196. if err != nil {
  197. return "", err
  198. }
  199. }
  200. l, err := layerStore.Register(rwTar, parent.RootFS.ChainID())
  201. if err != nil {
  202. return "", err
  203. }
  204. defer layer.ReleaseAndLog(layerStore, l)
  205. cc := image.ChildConfig{
  206. ContainerID: c.ContainerID,
  207. Author: c.Author,
  208. Comment: c.Comment,
  209. ContainerConfig: c.ContainerConfig,
  210. Config: c.Config,
  211. DiffID: l.DiffID(),
  212. }
  213. config, err := json.Marshal(image.NewChildImage(parent, cc, c.ContainerOS))
  214. if err != nil {
  215. return "", err
  216. }
  217. id, err := daemon.imageStore.Create(config)
  218. if err != nil {
  219. return "", err
  220. }
  221. if c.ParentImageID != "" {
  222. if err := daemon.imageStore.SetParent(id, image.ID(c.ParentImageID)); err != nil {
  223. return "", err
  224. }
  225. }
  226. return id, nil
  227. }
  228. func exportContainerRw(layerStore layer.Store, id, mountLabel string) (arch io.ReadCloser, err error) {
  229. rwlayer, err := layerStore.GetRWLayer(id)
  230. if err != nil {
  231. return nil, err
  232. }
  233. defer func() {
  234. if err != nil {
  235. layerStore.ReleaseRWLayer(rwlayer)
  236. }
  237. }()
  238. // TODO: this mount call is not necessary as we assume that TarStream() should
  239. // mount the layer if needed. But the Diff() function for windows requests that
  240. // the layer should be mounted when calling it. So we reserve this mount call
  241. // until windows driver can implement Diff() interface correctly.
  242. _, err = rwlayer.Mount(mountLabel)
  243. if err != nil {
  244. return nil, err
  245. }
  246. archive, err := rwlayer.TarStream()
  247. if err != nil {
  248. rwlayer.Unmount()
  249. return nil, err
  250. }
  251. return ioutils.NewReadCloserWrapper(archive, func() error {
  252. archive.Close()
  253. err = rwlayer.Unmount()
  254. layerStore.ReleaseRWLayer(rwlayer)
  255. return err
  256. }),
  257. nil
  258. }
  259. // CommitBuildStep is used by the builder to create an image for each step in
  260. // the build.
  261. //
  262. // This method is different from CreateImageFromContainer:
  263. // * it doesn't attempt to validate container state
  264. // * it doesn't send a commit action to metrics
  265. // * it doesn't log a container commit event
  266. //
  267. // This is a temporary shim. Should be removed when builder stops using commit.
  268. func (daemon *Daemon) CommitBuildStep(c backend.CommitConfig) (image.ID, error) {
  269. container, err := daemon.GetContainer(c.ContainerID)
  270. if err != nil {
  271. return "", err
  272. }
  273. c.ContainerMountLabel = container.MountLabel
  274. c.ContainerOS = container.OS
  275. c.ParentImageID = string(container.ImageID)
  276. return daemon.commitImage(c)
  277. }