commit.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 and on Solaris.
  122. if (runtime.GOOS == "windows" || runtime.GOOS == "solaris") && 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. newConfig, err := dockerfile.BuildFromConfig(c.Config, c.Changes)
  138. if err != nil {
  139. return "", err
  140. }
  141. if c.MergeConfigs {
  142. if err := merge(newConfig, container.Config); err != nil {
  143. return "", err
  144. }
  145. }
  146. rwTar, err := daemon.exportContainerRw(container)
  147. if err != nil {
  148. return "", err
  149. }
  150. defer func() {
  151. if rwTar != nil {
  152. rwTar.Close()
  153. }
  154. }()
  155. var parent *image.Image
  156. if container.ImageID == "" {
  157. parent = new(image.Image)
  158. parent.RootFS = image.NewRootFS()
  159. } else {
  160. parent, err = daemon.stores[container.OS].imageStore.Get(container.ImageID)
  161. if err != nil {
  162. return "", err
  163. }
  164. }
  165. l, err := daemon.stores[container.OS].layerStore.Register(rwTar, parent.RootFS.ChainID(), layer.OS(container.OS))
  166. if err != nil {
  167. return "", err
  168. }
  169. defer layer.ReleaseAndLog(daemon.stores[container.OS].layerStore, l)
  170. containerConfig := c.ContainerConfig
  171. if containerConfig == nil {
  172. containerConfig = container.Config
  173. }
  174. cc := image.ChildConfig{
  175. ContainerID: container.ID,
  176. Author: c.Author,
  177. Comment: c.Comment,
  178. ContainerConfig: containerConfig,
  179. Config: newConfig,
  180. DiffID: l.DiffID(),
  181. }
  182. config, err := json.Marshal(image.NewChildImage(parent, cc, container.OS))
  183. if err != nil {
  184. return "", err
  185. }
  186. id, err := daemon.stores[container.OS].imageStore.Create(config)
  187. if err != nil {
  188. return "", err
  189. }
  190. if container.ImageID != "" {
  191. if err := daemon.stores[container.OS].imageStore.SetParent(id, container.ImageID); err != nil {
  192. return "", err
  193. }
  194. }
  195. imageRef := ""
  196. if c.Repo != "" {
  197. newTag, err := reference.ParseNormalizedNamed(c.Repo) // todo: should move this to API layer
  198. if err != nil {
  199. return "", err
  200. }
  201. if !reference.IsNameOnly(newTag) {
  202. return "", errors.Errorf("unexpected repository name: %s", c.Repo)
  203. }
  204. if c.Tag != "" {
  205. if newTag, err = reference.WithTag(newTag, c.Tag); err != nil {
  206. return "", err
  207. }
  208. }
  209. if err := daemon.TagImageWithReference(id, container.OS, newTag); err != nil {
  210. return "", err
  211. }
  212. imageRef = reference.FamiliarString(newTag)
  213. }
  214. attributes := map[string]string{
  215. "comment": c.Comment,
  216. "imageID": id.String(),
  217. "imageRef": imageRef,
  218. }
  219. daemon.LogContainerEventWithAttributes(container, "commit", attributes)
  220. containerActions.WithValues("commit").UpdateSince(start)
  221. return id.String(), nil
  222. }
  223. func (daemon *Daemon) exportContainerRw(container *container.Container) (arch io.ReadCloser, err error) {
  224. rwlayer, err := daemon.stores[container.OS].layerStore.GetRWLayer(container.ID)
  225. if err != nil {
  226. return nil, err
  227. }
  228. defer func() {
  229. if err != nil {
  230. daemon.stores[container.OS].layerStore.ReleaseRWLayer(rwlayer)
  231. }
  232. }()
  233. // TODO: this mount call is not necessary as we assume that TarStream() should
  234. // mount the layer if needed. But the Diff() function for windows requests that
  235. // the layer should be mounted when calling it. So we reserve this mount call
  236. // until windows driver can implement Diff() interface correctly.
  237. _, err = rwlayer.Mount(container.GetMountLabel())
  238. if err != nil {
  239. return nil, err
  240. }
  241. archive, err := rwlayer.TarStream()
  242. if err != nil {
  243. rwlayer.Unmount()
  244. return nil, err
  245. }
  246. return ioutils.NewReadCloserWrapper(archive, func() error {
  247. archive.Close()
  248. err = rwlayer.Unmount()
  249. daemon.stores[container.OS].layerStore.ReleaseRWLayer(rwlayer)
  250. return err
  251. }),
  252. nil
  253. }