commit.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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/image"
  15. "github.com/docker/docker/layer"
  16. "github.com/docker/docker/pkg/ioutils"
  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. // Commit creates a new filesystem image from the current state of a container.
  114. // The image can optionally be tagged into a repository.
  115. func (daemon *Daemon) Commit(name string, c *backend.ContainerCommitConfig) (string, error) {
  116. start := time.Now()
  117. container, err := daemon.GetContainer(name)
  118. if err != nil {
  119. return "", err
  120. }
  121. // It is not possible to commit a running container on Windows
  122. if (runtime.GOOS == "windows") && container.IsRunning() {
  123. return "", errors.Errorf("%+v does not support commit of a running container", runtime.GOOS)
  124. }
  125. if container.IsDead() {
  126. err := fmt.Errorf("You cannot commit container %s which is Dead", container.ID)
  127. return "", stateConflictError{err}
  128. }
  129. if container.IsRemovalInProgress() {
  130. err := fmt.Errorf("You cannot commit container %s which is being removed", container.ID)
  131. return "", stateConflictError{err}
  132. }
  133. if c.Pause && !container.IsPaused() {
  134. daemon.containerPause(container)
  135. defer daemon.containerUnpause(container)
  136. }
  137. if c.MergeConfigs && c.Config == nil {
  138. c.Config = container.Config
  139. }
  140. newConfig, err := dockerfile.BuildFromConfig(c.Config, c.Changes)
  141. if err != nil {
  142. return "", err
  143. }
  144. if c.MergeConfigs {
  145. if err := merge(newConfig, container.Config); err != nil {
  146. return "", err
  147. }
  148. }
  149. rwTar, err := daemon.exportContainerRw(container)
  150. if err != nil {
  151. return "", err
  152. }
  153. defer func() {
  154. if rwTar != nil {
  155. rwTar.Close()
  156. }
  157. }()
  158. var parent *image.Image
  159. if container.ImageID == "" {
  160. parent = new(image.Image)
  161. parent.RootFS = image.NewRootFS()
  162. } else {
  163. parent, err = daemon.stores[container.OS].imageStore.Get(container.ImageID)
  164. if err != nil {
  165. return "", err
  166. }
  167. }
  168. l, err := daemon.stores[container.OS].layerStore.Register(rwTar, parent.RootFS.ChainID(), layer.OS(container.OS))
  169. if err != nil {
  170. return "", err
  171. }
  172. defer layer.ReleaseAndLog(daemon.stores[container.OS].layerStore, l)
  173. containerConfig := c.ContainerConfig
  174. if containerConfig == nil {
  175. containerConfig = container.Config
  176. }
  177. cc := image.ChildConfig{
  178. ContainerID: container.ID,
  179. Author: c.Author,
  180. Comment: c.Comment,
  181. ContainerConfig: containerConfig,
  182. Config: newConfig,
  183. DiffID: l.DiffID(),
  184. }
  185. config, err := json.Marshal(image.NewChildImage(parent, cc, container.OS))
  186. if err != nil {
  187. return "", err
  188. }
  189. id, err := daemon.stores[container.OS].imageStore.Create(config)
  190. if err != nil {
  191. return "", err
  192. }
  193. if container.ImageID != "" {
  194. if err := daemon.stores[container.OS].imageStore.SetParent(id, container.ImageID); err != nil {
  195. return "", err
  196. }
  197. }
  198. imageRef := ""
  199. if c.Repo != "" {
  200. newTag, err := reference.ParseNormalizedNamed(c.Repo) // todo: should move this to API layer
  201. if err != nil {
  202. return "", err
  203. }
  204. if !reference.IsNameOnly(newTag) {
  205. return "", errors.Errorf("unexpected repository name: %s", c.Repo)
  206. }
  207. if c.Tag != "" {
  208. if newTag, err = reference.WithTag(newTag, c.Tag); err != nil {
  209. return "", err
  210. }
  211. }
  212. if err := daemon.TagImageWithReference(id, container.OS, newTag); err != nil {
  213. return "", err
  214. }
  215. imageRef = reference.FamiliarString(newTag)
  216. }
  217. attributes := map[string]string{
  218. "comment": c.Comment,
  219. "imageID": id.String(),
  220. "imageRef": imageRef,
  221. }
  222. daemon.LogContainerEventWithAttributes(container, "commit", attributes)
  223. containerActions.WithValues("commit").UpdateSince(start)
  224. return id.String(), nil
  225. }
  226. func (daemon *Daemon) exportContainerRw(container *container.Container) (arch io.ReadCloser, err error) {
  227. rwlayer, err := daemon.stores[container.OS].layerStore.GetRWLayer(container.ID)
  228. if err != nil {
  229. return nil, err
  230. }
  231. defer func() {
  232. if err != nil {
  233. daemon.stores[container.OS].layerStore.ReleaseRWLayer(rwlayer)
  234. }
  235. }()
  236. // TODO: this mount call is not necessary as we assume that TarStream() should
  237. // mount the layer if needed. But the Diff() function for windows requests that
  238. // the layer should be mounted when calling it. So we reserve this mount call
  239. // until windows driver can implement Diff() interface correctly.
  240. _, err = rwlayer.Mount(container.GetMountLabel())
  241. if err != nil {
  242. return nil, err
  243. }
  244. archive, err := rwlayer.TarStream()
  245. if err != nil {
  246. rwlayer.Unmount()
  247. return nil, err
  248. }
  249. return ioutils.NewReadCloserWrapper(archive, func() error {
  250. archive.Close()
  251. err = rwlayer.Unmount()
  252. daemon.stores[container.OS].layerStore.ReleaseRWLayer(rwlayer)
  253. return err
  254. }),
  255. nil
  256. }