image_create.go 1.1 KB

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