pull.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "encoding/base64"
  17. "encoding/json"
  18. "errors"
  19. "io"
  20. "strings"
  21. "github.com/compose-spec/compose-go/types"
  22. "github.com/distribution/distribution/v3/reference"
  23. "github.com/docker/buildx/driver"
  24. moby "github.com/docker/docker/api/types"
  25. "github.com/docker/docker/pkg/jsonmessage"
  26. "github.com/docker/docker/registry"
  27. "golang.org/x/sync/errgroup"
  28. "github.com/docker/compose/v2/pkg/api"
  29. "github.com/docker/compose/v2/pkg/progress"
  30. )
  31. func (s *composeService) Pull(ctx context.Context, project *types.Project, options api.PullOptions) error {
  32. if options.Quiet {
  33. return s.pull(ctx, project, options)
  34. }
  35. return progress.Run(ctx, func(ctx context.Context) error {
  36. return s.pull(ctx, project, options)
  37. })
  38. }
  39. func (s *composeService) pull(ctx context.Context, project *types.Project, opts api.PullOptions) error {
  40. info, err := s.apiClient().Info(ctx)
  41. if err != nil {
  42. return err
  43. }
  44. if info.IndexServerAddress == "" {
  45. info.IndexServerAddress = registry.IndexServer
  46. }
  47. images, err := s.getLocalImagesDigests(ctx, project)
  48. if err != nil {
  49. return err
  50. }
  51. w := progress.ContextWriter(ctx)
  52. eg, ctx := errgroup.WithContext(ctx)
  53. var mustBuild []string
  54. for _, service := range project.Services {
  55. service := service
  56. if service.Image == "" {
  57. w.Event(progress.Event{
  58. ID: service.Name,
  59. Status: progress.Done,
  60. Text: "Skipped",
  61. })
  62. continue
  63. }
  64. switch service.PullPolicy {
  65. case types.PullPolicyNever, types.PullPolicyBuild:
  66. w.Event(progress.Event{
  67. ID: service.Name,
  68. Status: progress.Done,
  69. Text: "Skipped",
  70. })
  71. continue
  72. case types.PullPolicyMissing, types.PullPolicyIfNotPresent:
  73. if _, ok := images[service.Image]; ok {
  74. w.Event(progress.Event{
  75. ID: service.Name,
  76. Status: progress.Done,
  77. Text: "Exists",
  78. })
  79. continue
  80. }
  81. }
  82. eg.Go(func() error {
  83. _, err := s.pullServiceImage(ctx, service, info, s.configFile(), w, false)
  84. if err != nil {
  85. if !opts.IgnoreFailures {
  86. if service.Build != nil {
  87. mustBuild = append(mustBuild, service.Name)
  88. }
  89. return err
  90. }
  91. w.TailMsgf("Pulling %s: %s", service.Name, err.Error())
  92. }
  93. return nil
  94. })
  95. }
  96. err = eg.Wait()
  97. if !opts.IgnoreFailures && len(mustBuild) > 0 {
  98. w.TailMsgf("WARNING: Some service image(s) must be built from source by running:\n docker compose build %s", strings.Join(mustBuild, " "))
  99. }
  100. return err
  101. }
  102. func (s *composeService) pullServiceImage(ctx context.Context, service types.ServiceConfig, info moby.Info, configFile driver.Auth, w progress.Writer, quietPull bool) (string, error) {
  103. w.Event(progress.Event{
  104. ID: service.Name,
  105. Status: progress.Working,
  106. Text: "Pulling",
  107. })
  108. ref, err := reference.ParseNormalizedNamed(service.Image)
  109. if err != nil {
  110. return "", err
  111. }
  112. repoInfo, err := registry.ParseRepositoryInfo(ref)
  113. if err != nil {
  114. return "", err
  115. }
  116. key := repoInfo.Index.Name
  117. if repoInfo.Index.Official {
  118. key = info.IndexServerAddress
  119. }
  120. authConfig, err := configFile.GetAuthConfig(key)
  121. if err != nil {
  122. return "", err
  123. }
  124. buf, err := json.Marshal(authConfig)
  125. if err != nil {
  126. return "", err
  127. }
  128. stream, err := s.apiClient().ImagePull(ctx, service.Image, moby.ImagePullOptions{
  129. RegistryAuth: base64.URLEncoding.EncodeToString(buf),
  130. Platform: service.Platform,
  131. })
  132. if err != nil {
  133. w.Event(progress.Event{
  134. ID: service.Name,
  135. Status: progress.Error,
  136. Text: "Error",
  137. })
  138. return "", WrapCategorisedComposeError(err, PullFailure)
  139. }
  140. dec := json.NewDecoder(stream)
  141. for {
  142. var jm jsonmessage.JSONMessage
  143. if err := dec.Decode(&jm); err != nil {
  144. if err == io.EOF {
  145. break
  146. }
  147. return "", WrapCategorisedComposeError(err, PullFailure)
  148. }
  149. if jm.Error != nil {
  150. return "", WrapCategorisedComposeError(errors.New(jm.Error.Message), PullFailure)
  151. }
  152. if !quietPull {
  153. toPullProgressEvent(service.Name, jm, w)
  154. }
  155. }
  156. w.Event(progress.Event{
  157. ID: service.Name,
  158. Status: progress.Done,
  159. Text: "Pulled",
  160. })
  161. inspected, _, err := s.dockerCli.Client().ImageInspectWithRaw(ctx, service.Image)
  162. if err != nil {
  163. return "", err
  164. }
  165. return inspected.ID, nil
  166. }
  167. func (s *composeService) pullRequiredImages(ctx context.Context, project *types.Project, images map[string]string, quietPull bool) error {
  168. info, err := s.apiClient().Info(ctx)
  169. if err != nil {
  170. return err
  171. }
  172. if info.IndexServerAddress == "" {
  173. info.IndexServerAddress = registry.IndexServer
  174. }
  175. var needPull []types.ServiceConfig
  176. for _, service := range project.Services {
  177. if service.Image == "" {
  178. continue
  179. }
  180. switch service.PullPolicy {
  181. case "", types.PullPolicyMissing, types.PullPolicyIfNotPresent:
  182. if _, ok := images[service.Image]; ok {
  183. continue
  184. }
  185. case types.PullPolicyNever, types.PullPolicyBuild:
  186. continue
  187. case types.PullPolicyAlways:
  188. // force pull
  189. }
  190. needPull = append(needPull, service)
  191. }
  192. if len(needPull) == 0 {
  193. return nil
  194. }
  195. return progress.Run(ctx, func(ctx context.Context) error {
  196. w := progress.ContextWriter(ctx)
  197. eg, ctx := errgroup.WithContext(ctx)
  198. pulledImages := make([]string, len(needPull))
  199. for i, service := range needPull {
  200. i, service := i, service
  201. eg.Go(func() error {
  202. id, err := s.pullServiceImage(ctx, service, info, s.configFile(), w, quietPull)
  203. pulledImages[i] = id
  204. if err != nil && service.Build != nil {
  205. // image can be built, so we can ignore pull failure
  206. return nil
  207. }
  208. return err
  209. })
  210. }
  211. for i, service := range needPull {
  212. if pulledImages[i] != "" {
  213. images[service.Image] = pulledImages[i]
  214. }
  215. }
  216. err := eg.Wait()
  217. if err != nil {
  218. return err
  219. }
  220. return err
  221. })
  222. }
  223. func toPullProgressEvent(parent string, jm jsonmessage.JSONMessage, w progress.Writer) {
  224. if jm.ID == "" || jm.Progress == nil {
  225. return
  226. }
  227. var (
  228. text string
  229. status = progress.Working
  230. )
  231. text = jm.Progress.String()
  232. if jm.Status == "Pull complete" ||
  233. jm.Status == "Already exists" ||
  234. strings.Contains(jm.Status, "Image is up to date") ||
  235. strings.Contains(jm.Status, "Downloaded newer image") {
  236. status = progress.Done
  237. }
  238. if jm.Error != nil {
  239. status = progress.Error
  240. text = jm.Error.Message
  241. }
  242. w.Event(progress.Event{
  243. ID: jm.ID,
  244. ParentID: parent,
  245. Text: jm.Status,
  246. Status: status,
  247. StatusText: text,
  248. })
  249. }