github.com/moby/docker@v26.1.3+incompatible/client/image_build.go (about) 1 package client // import "github.com/docker/docker/client" 2 3 import ( 4 "context" 5 "encoding/base64" 6 "encoding/json" 7 "io" 8 "net/http" 9 "net/url" 10 "strconv" 11 "strings" 12 13 "github.com/docker/docker/api/types" 14 "github.com/docker/docker/api/types/container" 15 ) 16 17 // ImageBuild sends a request to the daemon to build images. 18 // The Body in the response implements an io.ReadCloser and it's up to the caller to 19 // close it. 20 func (cli *Client) ImageBuild(ctx context.Context, buildContext io.Reader, options types.ImageBuildOptions) (types.ImageBuildResponse, error) { 21 query, err := cli.imageBuildOptionsToQuery(ctx, options) 22 if err != nil { 23 return types.ImageBuildResponse{}, err 24 } 25 26 buf, err := json.Marshal(options.AuthConfigs) 27 if err != nil { 28 return types.ImageBuildResponse{}, err 29 } 30 31 headers := http.Header{} 32 headers.Add("X-Registry-Config", base64.URLEncoding.EncodeToString(buf)) 33 headers.Set("Content-Type", "application/x-tar") 34 35 serverResp, err := cli.postRaw(ctx, "/build", query, buildContext, headers) 36 if err != nil { 37 return types.ImageBuildResponse{}, err 38 } 39 40 return types.ImageBuildResponse{ 41 Body: serverResp.body, 42 OSType: getDockerOS(serverResp.header.Get("Server")), 43 }, nil 44 } 45 46 func (cli *Client) imageBuildOptionsToQuery(ctx context.Context, options types.ImageBuildOptions) (url.Values, error) { 47 query := url.Values{ 48 "t": options.Tags, 49 "securityopt": options.SecurityOpt, 50 "extrahosts": options.ExtraHosts, 51 } 52 if options.SuppressOutput { 53 query.Set("q", "1") 54 } 55 if options.RemoteContext != "" { 56 query.Set("remote", options.RemoteContext) 57 } 58 if options.NoCache { 59 query.Set("nocache", "1") 60 } 61 if options.Remove { 62 query.Set("rm", "1") 63 } else { 64 query.Set("rm", "0") 65 } 66 67 if options.ForceRemove { 68 query.Set("forcerm", "1") 69 } 70 71 if options.PullParent { 72 query.Set("pull", "1") 73 } 74 75 if options.Squash { 76 if err := cli.NewVersionError(ctx, "1.25", "squash"); err != nil { 77 return query, err 78 } 79 query.Set("squash", "1") 80 } 81 82 if !container.Isolation.IsDefault(options.Isolation) { 83 query.Set("isolation", string(options.Isolation)) 84 } 85 86 query.Set("cpusetcpus", options.CPUSetCPUs) 87 query.Set("networkmode", options.NetworkMode) 88 query.Set("cpusetmems", options.CPUSetMems) 89 query.Set("cpushares", strconv.FormatInt(options.CPUShares, 10)) 90 query.Set("cpuquota", strconv.FormatInt(options.CPUQuota, 10)) 91 query.Set("cpuperiod", strconv.FormatInt(options.CPUPeriod, 10)) 92 query.Set("memory", strconv.FormatInt(options.Memory, 10)) 93 query.Set("memswap", strconv.FormatInt(options.MemorySwap, 10)) 94 query.Set("cgroupparent", options.CgroupParent) 95 query.Set("shmsize", strconv.FormatInt(options.ShmSize, 10)) 96 query.Set("dockerfile", options.Dockerfile) 97 query.Set("target", options.Target) 98 99 ulimitsJSON, err := json.Marshal(options.Ulimits) 100 if err != nil { 101 return query, err 102 } 103 query.Set("ulimits", string(ulimitsJSON)) 104 105 buildArgsJSON, err := json.Marshal(options.BuildArgs) 106 if err != nil { 107 return query, err 108 } 109 query.Set("buildargs", string(buildArgsJSON)) 110 111 labelsJSON, err := json.Marshal(options.Labels) 112 if err != nil { 113 return query, err 114 } 115 query.Set("labels", string(labelsJSON)) 116 117 cacheFromJSON, err := json.Marshal(options.CacheFrom) 118 if err != nil { 119 return query, err 120 } 121 query.Set("cachefrom", string(cacheFromJSON)) 122 if options.SessionID != "" { 123 query.Set("session", options.SessionID) 124 } 125 if options.Platform != "" { 126 if err := cli.NewVersionError(ctx, "1.32", "platform"); err != nil { 127 return query, err 128 } 129 query.Set("platform", strings.ToLower(options.Platform)) 130 } 131 if options.BuildID != "" { 132 query.Set("buildid", options.BuildID) 133 } 134 query.Set("version", string(options.Version)) 135 136 if options.Outputs != nil { 137 outputsJSON, err := json.Marshal(options.Outputs) 138 if err != nil { 139 return query, err 140 } 141 query.Set("outputs", string(outputsJSON)) 142 } 143 return query, nil 144 }