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