image_build.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package lib
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "net/http"
  6. "net/url"
  7. "regexp"
  8. "strconv"
  9. "github.com/docker/docker/api/types"
  10. "github.com/docker/docker/runconfig"
  11. "github.com/docker/go-units"
  12. )
  13. var headerRegexp = regexp.MustCompile(`\ADocker/.+\s\((.+)\)\z`)
  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(options types.ImageBuildOptions) (types.ImageBuildResponse, error) {
  18. query, err := 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/tar")
  29. serverResp, err := cli.postRaw("/build", query, options.Context, 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 imageBuildOptionsToQuery(options types.ImageBuildOptions) (url.Values, error) {
  40. query := url.Values{
  41. "t": options.Tags,
  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 !runconfig.IsolationLevel.IsDefault(runconfig.IsolationLevel(options.Isolation)) {
  64. query.Set("isolation", options.Isolation)
  65. }
  66. query.Set("cpusetcpus", options.CPUSetCPUs)
  67. query.Set("cpusetmems", options.CPUSetMems)
  68. query.Set("cpushares", strconv.FormatInt(options.CPUShares, 10))
  69. query.Set("cpuquota", strconv.FormatInt(options.CPUQuota, 10))
  70. query.Set("cpuperiod", strconv.FormatInt(options.CPUPeriod, 10))
  71. query.Set("memory", strconv.FormatInt(options.Memory, 10))
  72. query.Set("memswap", strconv.FormatInt(options.MemorySwap, 10))
  73. query.Set("cgroupparent", options.CgroupParent)
  74. if options.ShmSize != "" {
  75. parsedShmSize, err := units.RAMInBytes(options.ShmSize)
  76. if err != nil {
  77. return query, err
  78. }
  79. query.Set("shmsize", strconv.FormatInt(parsedShmSize, 10))
  80. }
  81. query.Set("dockerfile", options.Dockerfile)
  82. ulimitsJSON, err := json.Marshal(options.Ulimits)
  83. if err != nil {
  84. return query, err
  85. }
  86. query.Set("ulimits", string(ulimitsJSON))
  87. buildArgs := runconfig.ConvertKVStringsToMap(options.BuildArgs)
  88. buildArgsJSON, err := json.Marshal(buildArgs)
  89. if err != nil {
  90. return query, err
  91. }
  92. query.Set("buildargs", string(buildArgsJSON))
  93. return query, nil
  94. }
  95. func getDockerOS(serverHeader string) string {
  96. var osType string
  97. matches := headerRegexp.FindStringSubmatch(serverHeader)
  98. if len(matches) > 0 {
  99. osType = matches[1]
  100. }
  101. return osType
  102. }