commit.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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/errdefs"
  11. "github.com/docker/docker/api/types/backend"
  12. containertypes "github.com/docker/docker/api/types/container"
  13. "github.com/docker/docker/builder/dockerfile"
  14. "github.com/docker/docker/container"
  15. "github.com/docker/docker/image"
  16. "github.com/docker/docker/layer"
  17. "github.com/docker/docker/pkg/ioutils"
  18. "github.com/pkg/errors"
  19. )
  20. // merge merges two Config, the image container configuration (defaults values),
  21. // and the user container configuration, either passed by the API or generated
  22. // by the cli.
  23. // It will mutate the specified user configuration (userConf) with the image
  24. // configuration where the user configuration is incomplete.
  25. func merge(userConf, imageConf *containertypes.Config) error {
  26. if userConf.User == "" {
  27. userConf.User = imageConf.User
  28. }
  29. if len(userConf.ExposedPorts) == 0 {
  30. userConf.ExposedPorts = imageConf.ExposedPorts
  31. } else if imageConf.ExposedPorts != nil {
  32. for port := range imageConf.ExposedPorts {
  33. if _, exists := userConf.ExposedPorts[port]; !exists {
  34. userConf.ExposedPorts[port] = struct{}{}
  35. }
  36. }
  37. }
  38. if len(userConf.Env) == 0 {
  39. userConf.Env = imageConf.Env
  40. } else {
  41. for _, imageEnv := range imageConf.Env {
  42. found := false
  43. imageEnvKey := strings.Split(imageEnv, "=")[0]
  44. for _, userEnv := range userConf.Env {
  45. userEnvKey := strings.Split(userEnv, "=")[0]
  46. if runtime.GOOS == "windows" {
  47. // Case insensitive environment variables on Windows
  48. imageEnvKey = strings.ToUpper(imageEnvKey)
  49. userEnvKey = strings.ToUpper(userEnvKey)
  50. }
  51. if imageEnvKey == userEnvKey {
  52. found = true
  53. break
  54. }
  55. }
  56. if !found {
  57. userConf.Env = append(userConf.Env, imageEnv)
  58. }
  59. }
  60. }
  61. if userConf.Labels == nil {
  62. userConf.Labels = map[string]string{}
  63. }
  64. for l, v := range imageConf.Labels {
  65. if _, ok := userConf.Labels[l]; !ok {
  66. userConf.Labels[l] = v
  67. }
  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.StartPeriod == 0 {
  92. userConf.Healthcheck.StartPeriod = imageConf.Healthcheck.StartPeriod
  93. }
  94. if userConf.Healthcheck.Retries == 0 {
  95. userConf.Healthcheck.Retries = imageConf.Healthcheck.Retries
  96. }
  97. }
  98. }
  99. if userConf.WorkingDir == "" {
  100. userConf.WorkingDir = imageConf.WorkingDir
  101. }
  102. if len(userConf.Volumes) == 0 {
  103. userConf.Volumes = imageConf.Volumes
  104. } else {
  105. for k, v := range imageConf.Volumes {
  106. userConf.Volumes[k] = v
  107. }
  108. }
  109. if userConf.StopSignal == "" {
  110. userConf.StopSignal = imageConf.StopSignal
  111. }
  112. return nil
  113. }
  114. // Commit creates a new filesystem image from the current state of a container.
  115. // The image can optionally be tagged into a repository.
  116. func (daemon *Daemon) Commit(name string, c *backend.ContainerCommitConfig) (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.MergeConfigs && c.Config == nil {
  139. c.Config = container.Config
  140. }
  141. newConfig, err := dockerfile.BuildFromConfig(c.Config, c.Changes)
  142. if err != nil {
  143. return "", err
  144. }
  145. if c.MergeConfigs {
  146. if err := merge(newConfig, container.Config); err != nil {
  147. return "", err
  148. }
  149. }
  150. rwTar, err := daemon.exportContainerRw(container)
  151. if err != nil {
  152. return "", err
  153. }
  154. defer func() {
  155. if rwTar != nil {
  156. rwTar.Close()
  157. }
  158. }()
  159. var parent *image.Image
  160. if container.ImageID == "" {
  161. parent = new(image.Image)
  162. parent.RootFS = image.NewRootFS()
  163. } else {
  164. parent, err = daemon.stores[container.OS].imageStore.Get(container.ImageID)
  165. if err != nil {
  166. return "", err
  167. }
  168. }
  169. l, err := daemon.stores[container.OS].layerStore.Register(rwTar, parent.RootFS.ChainID(), layer.OS(container.OS))
  170. if err != nil {
  171. return "", err
  172. }
  173. defer layer.ReleaseAndLog(daemon.stores[container.OS].layerStore, l)
  174. containerConfig := c.ContainerConfig
  175. if containerConfig == nil {
  176. containerConfig = container.Config
  177. }
  178. cc := image.ChildConfig{
  179. ContainerID: container.ID,
  180. Author: c.Author,
  181. Comment: c.Comment,
  182. ContainerConfig: containerConfig,
  183. Config: newConfig,
  184. DiffID: l.DiffID(),
  185. }
  186. config, err := json.Marshal(image.NewChildImage(parent, cc, container.OS))
  187. if err != nil {
  188. return "", err
  189. }
  190. id, err := daemon.stores[container.OS].imageStore.Create(config)
  191. if err != nil {
  192. return "", err
  193. }
  194. if container.ImageID != "" {
  195. if err := daemon.stores[container.OS].imageStore.SetParent(id, container.ImageID); err != nil {
  196. return "", err
  197. }
  198. }
  199. imageRef := ""
  200. if c.Repo != "" {
  201. newTag, err := reference.ParseNormalizedNamed(c.Repo) // todo: should move this to API layer
  202. if err != nil {
  203. return "", err
  204. }
  205. if !reference.IsNameOnly(newTag) {
  206. return "", errors.Errorf("unexpected repository name: %s", c.Repo)
  207. }
  208. if c.Tag != "" {
  209. if newTag, err = reference.WithTag(newTag, c.Tag); err != nil {
  210. return "", err
  211. }
  212. }
  213. if err := daemon.TagImageWithReference(id, container.OS, newTag); err != nil {
  214. return "", err
  215. }
  216. imageRef = reference.FamiliarString(newTag)
  217. }
  218. attributes := map[string]string{
  219. "comment": c.Comment,
  220. "imageID": id.String(),
  221. "imageRef": imageRef,
  222. }
  223. daemon.LogContainerEventWithAttributes(container, "commit", attributes)
  224. containerActions.WithValues("commit").UpdateSince(start)
  225. return id.String(), nil
  226. }
  227. func (daemon *Daemon) exportContainerRw(container *container.Container) (arch io.ReadCloser, err error) {
  228. rwlayer, err := daemon.stores[container.OS].layerStore.GetRWLayer(container.ID)
  229. if err != nil {
  230. return nil, err
  231. }
  232. defer func() {
  233. if err != nil {
  234. daemon.stores[container.OS].layerStore.ReleaseRWLayer(rwlayer)
  235. }
  236. }()
  237. // TODO: this mount call is not necessary as we assume that TarStream() should
  238. // mount the layer if needed. But the Diff() function for windows requests that
  239. // the layer should be mounted when calling it. So we reserve this mount call
  240. // until windows driver can implement Diff() interface correctly.
  241. _, err = rwlayer.Mount(container.GetMountLabel())
  242. if err != nil {
  243. return nil, err
  244. }
  245. archive, err := rwlayer.TarStream()
  246. if err != nil {
  247. rwlayer.Unmount()
  248. return nil, err
  249. }
  250. return ioutils.NewReadCloserWrapper(archive, func() error {
  251. archive.Close()
  252. err = rwlayer.Unmount()
  253. daemon.stores[container.OS].layerStore.ReleaseRWLayer(rwlayer)
  254. return err
  255. }),
  256. nil
  257. }