2018-02-05 21:05:59 +00:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
import (
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2016-09-06 18:46:37 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
2018-07-03 02:31:05 +00:00
|
|
|
"strings"
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
)
|
|
|
|
|
2021-02-16 15:07:44 +00:00
|
|
|
// ImageBuild sends a request to the daemon to build images.
|
|
|
|
// The Body in the response implements an io.ReadCloser and it's up to the caller to
|
2016-09-06 18:46:37 +00:00
|
|
|
// close it.
|
|
|
|
func (cli *Client) ImageBuild(ctx context.Context, buildContext io.Reader, options types.ImageBuildOptions) (types.ImageBuildResponse, error) {
|
2016-11-03 00:43:32 +00:00
|
|
|
query, err := cli.imageBuildOptionsToQuery(options)
|
2016-09-06 18:46:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return types.ImageBuildResponse{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := json.Marshal(options.AuthConfigs)
|
|
|
|
if err != nil {
|
|
|
|
return types.ImageBuildResponse{}, err
|
|
|
|
}
|
2017-08-08 19:43:48 +00:00
|
|
|
|
2023-07-10 14:44:59 +00:00
|
|
|
headers := http.Header{}
|
|
|
|
headers.Add("X-Registry-Config", base64.URLEncoding.EncodeToString(buf))
|
2016-12-06 21:15:27 +00:00
|
|
|
headers.Set("Content-Type", "application/x-tar")
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
serverResp, err := cli.postRaw(ctx, "/build", query, buildContext, headers)
|
|
|
|
if err != nil {
|
|
|
|
return types.ImageBuildResponse{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return types.ImageBuildResponse{
|
|
|
|
Body: serverResp.body,
|
2023-07-10 14:44:59 +00:00
|
|
|
OSType: getDockerOS(serverResp.header.Get("Server")),
|
2016-09-06 18:46:37 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-11-03 00:43:32 +00:00
|
|
|
func (cli *Client) imageBuildOptionsToQuery(options types.ImageBuildOptions) (url.Values, error) {
|
2016-09-06 18:46:37 +00:00
|
|
|
query := url.Values{
|
2016-06-07 19:15:50 +00:00
|
|
|
"t": options.Tags,
|
|
|
|
"securityopt": options.SecurityOpt,
|
2017-01-13 15:01:58 +00:00
|
|
|
"extrahosts": options.ExtraHosts,
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
if options.SuppressOutput {
|
|
|
|
query.Set("q", "1")
|
|
|
|
}
|
|
|
|
if options.RemoteContext != "" {
|
|
|
|
query.Set("remote", options.RemoteContext)
|
|
|
|
}
|
|
|
|
if options.NoCache {
|
|
|
|
query.Set("nocache", "1")
|
|
|
|
}
|
|
|
|
if options.Remove {
|
|
|
|
query.Set("rm", "1")
|
|
|
|
} else {
|
|
|
|
query.Set("rm", "0")
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.ForceRemove {
|
|
|
|
query.Set("forcerm", "1")
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.PullParent {
|
|
|
|
query.Set("pull", "1")
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.Squash {
|
2016-11-03 00:43:32 +00:00
|
|
|
if err := cli.NewVersionError("1.25", "squash"); err != nil {
|
|
|
|
return query, err
|
|
|
|
}
|
2016-09-06 18:46:37 +00:00
|
|
|
query.Set("squash", "1")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !container.Isolation.IsDefault(options.Isolation) {
|
|
|
|
query.Set("isolation", string(options.Isolation))
|
|
|
|
}
|
|
|
|
|
|
|
|
query.Set("cpusetcpus", options.CPUSetCPUs)
|
2016-03-06 12:29:23 +00:00
|
|
|
query.Set("networkmode", options.NetworkMode)
|
2016-09-06 18:46:37 +00:00
|
|
|
query.Set("cpusetmems", options.CPUSetMems)
|
|
|
|
query.Set("cpushares", strconv.FormatInt(options.CPUShares, 10))
|
|
|
|
query.Set("cpuquota", strconv.FormatInt(options.CPUQuota, 10))
|
|
|
|
query.Set("cpuperiod", strconv.FormatInt(options.CPUPeriod, 10))
|
|
|
|
query.Set("memory", strconv.FormatInt(options.Memory, 10))
|
|
|
|
query.Set("memswap", strconv.FormatInt(options.MemorySwap, 10))
|
|
|
|
query.Set("cgroupparent", options.CgroupParent)
|
|
|
|
query.Set("shmsize", strconv.FormatInt(options.ShmSize, 10))
|
|
|
|
query.Set("dockerfile", options.Dockerfile)
|
2017-04-10 22:27:42 +00:00
|
|
|
query.Set("target", options.Target)
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
ulimitsJSON, err := json.Marshal(options.Ulimits)
|
|
|
|
if err != nil {
|
|
|
|
return query, err
|
|
|
|
}
|
|
|
|
query.Set("ulimits", string(ulimitsJSON))
|
|
|
|
|
|
|
|
buildArgsJSON, err := json.Marshal(options.BuildArgs)
|
|
|
|
if err != nil {
|
|
|
|
return query, err
|
|
|
|
}
|
|
|
|
query.Set("buildargs", string(buildArgsJSON))
|
|
|
|
|
|
|
|
labelsJSON, err := json.Marshal(options.Labels)
|
|
|
|
if err != nil {
|
|
|
|
return query, err
|
|
|
|
}
|
|
|
|
query.Set("labels", string(labelsJSON))
|
2016-09-22 21:38:00 +00:00
|
|
|
|
|
|
|
cacheFromJSON, err := json.Marshal(options.CacheFrom)
|
|
|
|
if err != nil {
|
|
|
|
return query, err
|
|
|
|
}
|
|
|
|
query.Set("cachefrom", string(cacheFromJSON))
|
2017-05-15 19:59:15 +00:00
|
|
|
if options.SessionID != "" {
|
|
|
|
query.Set("session", options.SessionID)
|
|
|
|
}
|
2018-07-03 02:31:05 +00:00
|
|
|
if options.Platform != "" {
|
|
|
|
if err := cli.NewVersionError("1.32", "platform"); err != nil {
|
|
|
|
return query, err
|
|
|
|
}
|
|
|
|
query.Set("platform", strings.ToLower(options.Platform))
|
2017-09-13 19:49:04 +00:00
|
|
|
}
|
2018-04-19 18:08:33 +00:00
|
|
|
if options.BuildID != "" {
|
|
|
|
query.Set("buildid", options.BuildID)
|
|
|
|
}
|
2018-05-18 05:47:34 +00:00
|
|
|
query.Set("version", string(options.Version))
|
2019-03-17 16:54:52 +00:00
|
|
|
|
|
|
|
if options.Outputs != nil {
|
|
|
|
outputsJSON, err := json.Marshal(options.Outputs)
|
|
|
|
if err != nil {
|
|
|
|
return query, err
|
|
|
|
}
|
|
|
|
query.Set("outputs", string(outputsJSON))
|
|
|
|
}
|
2016-09-06 18:46:37 +00:00
|
|
|
return query, nil
|
|
|
|
}
|