image_build.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package lib
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "net/http"
  6. "net/url"
  7. "strconv"
  8. "github.com/docker/docker/api/types"
  9. "github.com/docker/docker/pkg/httputils"
  10. "github.com/docker/docker/pkg/units"
  11. "github.com/docker/docker/runconfig"
  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(options types.ImageBuildOptions) (types.ImageBuildResponse, error) {
  17. query, err := 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("/build", query, options.Context, headers)
  29. if err != nil {
  30. return types.ImageBuildResponse{}, err
  31. }
  32. var osType string
  33. if h, err := httputils.ParseServerHeader(serverResp.header.Get("Server")); err == nil {
  34. osType = h.OS
  35. }
  36. return types.ImageBuildResponse{
  37. Body: serverResp.body,
  38. OSType: osType,
  39. }, nil
  40. }
  41. func imageBuildOptionsToQuery(options types.ImageBuildOptions) (url.Values, error) {
  42. query := url.Values{
  43. "t": options.Tags,
  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 !runconfig.IsolationLevel.IsDefault(runconfig.IsolationLevel(options.Isolation)) {
  66. query.Set("isolation", options.Isolation)
  67. }
  68. query.Set("cpusetcpus", options.CPUSetCPUs)
  69. query.Set("cpusetmems", options.CPUSetMems)
  70. query.Set("cpushares", strconv.FormatInt(options.CPUShares, 10))
  71. query.Set("cpuquota", strconv.FormatInt(options.CPUQuota, 10))
  72. query.Set("cpuperiod", strconv.FormatInt(options.CPUPeriod, 10))
  73. query.Set("memory", strconv.FormatInt(options.Memory, 10))
  74. query.Set("memswap", strconv.FormatInt(options.MemorySwap, 10))
  75. query.Set("cgroupparent", options.CgroupParent)
  76. if options.ShmSize != "" {
  77. parsedShmSize, err := units.RAMInBytes(options.ShmSize)
  78. if err != nil {
  79. return query, err
  80. }
  81. query.Set("shmsize", strconv.FormatInt(parsedShmSize, 10))
  82. }
  83. query.Set("dockerfile", options.Dockerfile)
  84. ulimitsJSON, err := json.Marshal(options.Ulimits)
  85. if err != nil {
  86. return query, err
  87. }
  88. query.Set("ulimits", string(ulimitsJSON))
  89. buildArgs := runconfig.ConvertKVStringsToMap(options.BuildArgs)
  90. buildArgsJSON, err := json.Marshal(buildArgs)
  91. if err != nil {
  92. return query, err
  93. }
  94. query.Set("buildargs", string(buildArgsJSON))
  95. return query, nil
  96. }