image_build.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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(ctx, options)
  19. if err != nil {
  20. return types.ImageBuildResponse{}, err
  21. }
  22. buf, err := json.Marshal(options.AuthConfigs)
  23. if err != nil {
  24. return types.ImageBuildResponse{}, err
  25. }
  26. headers := http.Header{}
  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. return types.ImageBuildResponse{
  34. Body: serverResp.body,
  35. OSType: getDockerOS(serverResp.header.Get("Server")),
  36. }, nil
  37. }
  38. func (cli *Client) imageBuildOptionsToQuery(ctx context.Context, 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(ctx, "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. if options.SessionID != "" {
  106. query.Set("session", options.SessionID)
  107. }
  108. if options.Platform != "" {
  109. if err := cli.NewVersionError(ctx, "1.32", "platform"); err != nil {
  110. return query, err
  111. }
  112. query.Set("platform", strings.ToLower(options.Platform))
  113. }
  114. if options.BuildID != "" {
  115. query.Set("buildid", options.BuildID)
  116. }
  117. query.Set("version", string(options.Version))
  118. if options.Outputs != nil {
  119. outputsJSON, err := json.Marshal(options.Outputs)
  120. if err != nil {
  121. return query, err
  122. }
  123. query.Set("outputs", string(outputsJSON))
  124. }
  125. return query, nil
  126. }