github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+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 request to the daemon to build images.
    18  // The Body in the response implement 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(options)
    22  	if err != nil {
    23  		return types.ImageBuildResponse{}, err
    24  	}
    25  
    26  	headers := http.Header(make(map[string][]string))
    27  	buf, err := json.Marshal(options.AuthConfigs)
    28  	if err != nil {
    29  		return types.ImageBuildResponse{}, err
    30  	}
    31  	headers.Add("X-Registry-Config", base64.URLEncoding.EncodeToString(buf))
    32  
    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  	osType := getDockerOS(serverResp.header.Get("Server"))
    41  
    42  	return types.ImageBuildResponse{
    43  		Body:   serverResp.body,
    44  		OSType: osType,
    45  	}, nil
    46  }
    47  
    48  func (cli *Client) imageBuildOptionsToQuery(options types.ImageBuildOptions) (url.Values, error) {
    49  	query := url.Values{
    50  		"t":           options.Tags,
    51  		"securityopt": options.SecurityOpt,
    52  		"extrahosts":  options.ExtraHosts,
    53  	}
    54  	if options.SuppressOutput {
    55  		query.Set("q", "1")
    56  	}
    57  	if options.RemoteContext != "" {
    58  		query.Set("remote", options.RemoteContext)
    59  	}
    60  	if options.NoCache {
    61  		query.Set("nocache", "1")
    62  	}
    63  	if options.Remove {
    64  		query.Set("rm", "1")
    65  	} else {
    66  		query.Set("rm", "0")
    67  	}
    68  
    69  	if options.ForceRemove {
    70  		query.Set("forcerm", "1")
    71  	}
    72  
    73  	if options.PullParent {
    74  		query.Set("pull", "1")
    75  	}
    76  
    77  	if options.Squash {
    78  		if err := cli.NewVersionError("1.25", "squash"); err != nil {
    79  			return query, err
    80  		}
    81  		query.Set("squash", "1")
    82  	}
    83  
    84  	if !container.Isolation.IsDefault(options.Isolation) {
    85  		query.Set("isolation", string(options.Isolation))
    86  	}
    87  
    88  	query.Set("cpusetcpus", options.CPUSetCPUs)
    89  	query.Set("networkmode", options.NetworkMode)
    90  	query.Set("cpusetmems", options.CPUSetMems)
    91  	query.Set("cpushares", strconv.FormatInt(options.CPUShares, 10))
    92  	query.Set("cpuquota", strconv.FormatInt(options.CPUQuota, 10))
    93  	query.Set("cpuperiod", strconv.FormatInt(options.CPUPeriod, 10))
    94  	query.Set("memory", strconv.FormatInt(options.Memory, 10))
    95  	query.Set("memswap", strconv.FormatInt(options.MemorySwap, 10))
    96  	query.Set("cgroupparent", options.CgroupParent)
    97  	query.Set("shmsize", strconv.FormatInt(options.ShmSize, 10))
    98  	query.Set("dockerfile", options.Dockerfile)
    99  	query.Set("target", options.Target)
   100  
   101  	ulimitsJSON, err := json.Marshal(options.Ulimits)
   102  	if err != nil {
   103  		return query, err
   104  	}
   105  	query.Set("ulimits", string(ulimitsJSON))
   106  
   107  	buildArgsJSON, err := json.Marshal(options.BuildArgs)
   108  	if err != nil {
   109  		return query, err
   110  	}
   111  	query.Set("buildargs", string(buildArgsJSON))
   112  
   113  	labelsJSON, err := json.Marshal(options.Labels)
   114  	if err != nil {
   115  		return query, err
   116  	}
   117  	query.Set("labels", string(labelsJSON))
   118  
   119  	cacheFromJSON, err := json.Marshal(options.CacheFrom)
   120  	if err != nil {
   121  		return query, err
   122  	}
   123  	query.Set("cachefrom", string(cacheFromJSON))
   124  	if options.SessionID != "" {
   125  		query.Set("session", options.SessionID)
   126  	}
   127  	if options.Platform != "" {
   128  		if err := cli.NewVersionError("1.32", "platform"); err != nil {
   129  			return query, err
   130  		}
   131  		query.Set("platform", strings.ToLower(options.Platform))
   132  	}
   133  	if options.BuildID != "" {
   134  		query.Set("buildid", options.BuildID)
   135  	}
   136  	query.Set("version", string(options.Version))
   137  
   138  	if options.Outputs != nil {
   139  		outputsJSON, err := json.Marshal(options.Outputs)
   140  		if err != nil {
   141  			return query, err
   142  		}
   143  		query.Set("outputs", string(outputsJSON))
   144  	}
   145  	return query, nil
   146  }