image_build.go 3.6 KB

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