image_build.go 3.2 KB

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