image_build.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/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. }
  43. if options.SuppressOutput {
  44. query.Set("q", "1")
  45. }
  46. if options.RemoteContext != "" {
  47. query.Set("remote", options.RemoteContext)
  48. }
  49. if options.NoCache {
  50. query.Set("nocache", "1")
  51. }
  52. if options.Remove {
  53. query.Set("rm", "1")
  54. } else {
  55. query.Set("rm", "0")
  56. }
  57. if options.ForceRemove {
  58. query.Set("forcerm", "1")
  59. }
  60. if options.PullParent {
  61. query.Set("pull", "1")
  62. }
  63. if options.Squash {
  64. if err := cli.NewVersionError("1.25", "squash"); err != nil {
  65. return query, err
  66. }
  67. query.Set("squash", "1")
  68. }
  69. if !container.Isolation.IsDefault(options.Isolation) {
  70. query.Set("isolation", string(options.Isolation))
  71. }
  72. query.Set("cpusetcpus", options.CPUSetCPUs)
  73. query.Set("networkmode", options.NetworkMode)
  74. query.Set("cpusetmems", options.CPUSetMems)
  75. query.Set("cpushares", strconv.FormatInt(options.CPUShares, 10))
  76. query.Set("cpuquota", strconv.FormatInt(options.CPUQuota, 10))
  77. query.Set("cpuperiod", strconv.FormatInt(options.CPUPeriod, 10))
  78. query.Set("memory", strconv.FormatInt(options.Memory, 10))
  79. query.Set("memswap", strconv.FormatInt(options.MemorySwap, 10))
  80. query.Set("cgroupparent", options.CgroupParent)
  81. query.Set("shmsize", strconv.FormatInt(options.ShmSize, 10))
  82. query.Set("dockerfile", options.Dockerfile)
  83. ulimitsJSON, err := json.Marshal(options.Ulimits)
  84. if err != nil {
  85. return query, err
  86. }
  87. query.Set("ulimits", string(ulimitsJSON))
  88. buildArgsJSON, err := json.Marshal(options.BuildArgs)
  89. if err != nil {
  90. return query, err
  91. }
  92. query.Set("buildargs", string(buildArgsJSON))
  93. labelsJSON, err := json.Marshal(options.Labels)
  94. if err != nil {
  95. return query, err
  96. }
  97. query.Set("labels", string(labelsJSON))
  98. cacheFromJSON, err := json.Marshal(options.CacheFrom)
  99. if err != nil {
  100. return query, err
  101. }
  102. query.Set("cachefrom", string(cacheFromJSON))
  103. return query, nil
  104. }