github.com/kinvolk/docker@v1.13.1/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  
    11  	"golang.org/x/net/context"
    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  	headers.Set("Content-Type", "application/tar")
    33  
    34  	serverResp, err := cli.postRaw(ctx, "/build", query, buildContext, headers)
    35  	if err != nil {
    36  		return types.ImageBuildResponse{}, err
    37  	}
    38  
    39  	osType := getDockerOS(serverResp.header.Get("Server"))
    40  
    41  	return types.ImageBuildResponse{
    42  		Body:   serverResp.body,
    43  		OSType: osType,
    44  	}, nil
    45  }
    46  
    47  func (cli *Client) imageBuildOptionsToQuery(options types.ImageBuildOptions) (url.Values, error) {
    48  	query := url.Values{
    49  		"t":           options.Tags,
    50  		"securityopt": options.SecurityOpt,
    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("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  
    98  	ulimitsJSON, err := json.Marshal(options.Ulimits)
    99  	if err != nil {
   100  		return query, err
   101  	}
   102  	query.Set("ulimits", string(ulimitsJSON))
   103  
   104  	buildArgsJSON, err := json.Marshal(options.BuildArgs)
   105  	if err != nil {
   106  		return query, err
   107  	}
   108  	query.Set("buildargs", string(buildArgsJSON))
   109  
   110  	labelsJSON, err := json.Marshal(options.Labels)
   111  	if err != nil {
   112  		return query, err
   113  	}
   114  	query.Set("labels", string(labelsJSON))
   115  
   116  	cacheFromJSON, err := json.Marshal(options.CacheFrom)
   117  	if err != nil {
   118  		return query, err
   119  	}
   120  	query.Set("cachefrom", string(cacheFromJSON))
   121  
   122  	return query, nil
   123  }