github.com/lazyboychen7/engine@v17.12.1-ce-rc2+incompatible/client/image_build.go (about)

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