image_create.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package client
  2. import (
  3. "io"
  4. "net/url"
  5. "strings"
  6. "golang.org/x/net/context"
  7. "github.com/docker/distribution/reference"
  8. "github.com/docker/docker/api/types"
  9. )
  10. // ImageCreate creates a new image based in the parent options.
  11. // It returns the JSON content in the response body.
  12. func (cli *Client) ImageCreate(ctx context.Context, parentReference string, options types.ImageCreateOptions) (io.ReadCloser, error) {
  13. ref, err := reference.ParseNormalizedNamed(parentReference)
  14. if err != nil {
  15. return nil, err
  16. }
  17. query := url.Values{}
  18. query.Set("fromImage", reference.FamiliarName(ref))
  19. query.Set("tag", getAPITagFromNamedRef(ref))
  20. if options.Platform != "" {
  21. query.Set("platform", strings.ToLower(options.Platform))
  22. }
  23. resp, err := cli.tryImageCreate(ctx, query, options.RegistryAuth)
  24. if err != nil {
  25. return nil, err
  26. }
  27. return resp.body, nil
  28. }
  29. func (cli *Client) tryImageCreate(ctx context.Context, query url.Values, registryAuth string) (serverResponse, error) {
  30. headers := map[string][]string{"X-Registry-Auth": {registryAuth}}
  31. return cli.post(ctx, "/images/create", query, nil, headers)
  32. }