image_build.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "encoding/json"
  6. "io"
  7. "net/http"
  8. "net/url"
  9. "strconv"
  10. "strings"
  11. "github.com/docker/docker/api/types"
  12. "github.com/docker/docker/api/types/container"
  13. )
  14. // ImageBuild sends a request to the daemon to build images.
  15. // The Body in the response implements 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. headers.Set("Content-Type", "application/x-tar")
  29. serverResp, err := cli.postRaw(ctx, "/build", query, buildContext, headers)
  30. if err != nil {
  31. return types.ImageBuildResponse{}, err
  32. }
  33. osType := getDockerOS(serverResp.header.Get("Server"))
  34. return types.ImageBuildResponse{
  35. Body: serverResp.body,
  36. OSType: osType,
  37. }, nil
  38. }
  39. func (cli *Client) imageBuildOptionsToQuery(options types.ImageBuildOptions) (url.Values, error) {
  40. query := url.Values{
  41. "t": options.Tags,
  42. "securityopt": options.SecurityOpt,
  43. "extrahosts": options.ExtraHosts,
  44. }
  45. if options.SuppressOutput {
  46. query.Set("q", "1")
  47. }
  48. if options.RemoteContext != "" {
  49. query.Set("remote", options.RemoteContext)
  50. }
  51. if options.NoCache {
  52. query.Set("nocache", "1")
  53. }
  54. if options.Remove {
  55. query.Set("rm", "1")
  56. } else {
  57. query.Set("rm", "0")
  58. }
  59. if options.ForceRemove {
  60. query.Set("forcerm", "1")
  61. }
  62. if options.PullParent {
  63. query.Set("pull", "1")
  64. }
  65. if options.Squash {
  66. if err := cli.NewVersionError("1.25", "squash"); err != nil {
  67. return query, err
  68. }
  69. query.Set("squash", "1")
  70. }
  71. if !container.Isolation.IsDefault(options.Isolation) {
  72. query.Set("isolation", string(options.Isolation))
  73. }
  74. query.Set("cpusetcpus", options.CPUSetCPUs)
  75. query.Set("networkmode", options.NetworkMode)
  76. query.Set("cpusetmems", options.CPUSetMems)
  77. query.Set("cpushares", strconv.FormatInt(options.CPUShares, 10))
  78. query.Set("cpuquota", strconv.FormatInt(options.CPUQuota, 10))
  79. query.Set("cpuperiod", strconv.FormatInt(options.CPUPeriod, 10))
  80. query.Set("memory", strconv.FormatInt(options.Memory, 10))
  81. query.Set("memswap", strconv.FormatInt(options.MemorySwap, 10))
  82. query.Set("cgroupparent", options.CgroupParent)
  83. query.Set("shmsize", strconv.FormatInt(options.ShmSize, 10))
  84. query.Set("dockerfile", options.Dockerfile)
  85. query.Set("target", options.Target)
  86. ulimitsJSON, err := json.Marshal(options.Ulimits)
  87. if err != nil {
  88. return query, err
  89. }
  90. query.Set("ulimits", string(ulimitsJSON))
  91. buildArgsJSON, err := json.Marshal(options.BuildArgs)
  92. if err != nil {
  93. return query, err
  94. }
  95. query.Set("buildargs", string(buildArgsJSON))
  96. labelsJSON, err := json.Marshal(options.Labels)
  97. if err != nil {
  98. return query, err
  99. }
  100. query.Set("labels", string(labelsJSON))
  101. cacheFromJSON, err := json.Marshal(options.CacheFrom)
  102. if err != nil {
  103. return query, err
  104. }
  105. query.Set("cachefrom", string(cacheFromJSON))
  106. if options.SessionID != "" {
  107. query.Set("session", options.SessionID)
  108. }
  109. if options.Platform != "" {
  110. if err := cli.NewVersionError("1.32", "platform"); err != nil {
  111. return query, err
  112. }
  113. query.Set("platform", strings.ToLower(options.Platform))
  114. }
  115. if options.BuildID != "" {
  116. query.Set("buildid", options.BuildID)
  117. }
  118. query.Set("version", string(options.Version))
  119. if options.Outputs != nil {
  120. outputsJSON, err := json.Marshal(options.Outputs)
  121. if err != nil {
  122. return query, err
  123. }
  124. query.Set("outputs", string(outputsJSON))
  125. }
  126. return query, nil
  127. }