image_pull.go 999 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package client
  2. import (
  3. "io"
  4. "net/http"
  5. "net/url"
  6. "golang.org/x/net/context"
  7. "github.com/docker/engine-api/types"
  8. )
  9. // ImagePull request the docker host to pull an image from a remote registry.
  10. // It executes the privileged function if the operation is unauthorized
  11. // and it tries one more time.
  12. // It's up to the caller to handle the io.ReadCloser and close it properly.
  13. func (cli *Client) ImagePull(ctx context.Context, options types.ImagePullOptions, privilegeFunc RequestPrivilegeFunc) (io.ReadCloser, error) {
  14. query := url.Values{}
  15. query.Set("fromImage", options.ImageID)
  16. if options.Tag != "" {
  17. query.Set("tag", options.Tag)
  18. }
  19. resp, err := cli.tryImageCreate(ctx, query, options.RegistryAuth)
  20. if resp.statusCode == http.StatusUnauthorized {
  21. newAuthHeader, privilegeErr := privilegeFunc()
  22. if privilegeErr != nil {
  23. return nil, privilegeErr
  24. }
  25. resp, err = cli.tryImageCreate(ctx, query, newAuthHeader)
  26. }
  27. if err != nil {
  28. return nil, err
  29. }
  30. return resp.body, nil
  31. }