image_build.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package lib
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "io"
  6. "net/http"
  7. "net/url"
  8. "strconv"
  9. "github.com/docker/docker/cliconfig"
  10. "github.com/docker/docker/pkg/httputils"
  11. "github.com/docker/docker/pkg/ulimit"
  12. "github.com/docker/docker/pkg/units"
  13. "github.com/docker/docker/runconfig"
  14. )
  15. // ImageBuildOptions holds the information
  16. // necessary to build images.
  17. type ImageBuildOptions struct {
  18. Tags []string
  19. SuppressOutput bool
  20. RemoteContext string
  21. NoCache bool
  22. Remove bool
  23. ForceRemove bool
  24. PullParent bool
  25. Isolation string
  26. CPUSetCPUs string
  27. CPUSetMems string
  28. CPUShares int64
  29. CPUQuota int64
  30. CPUPeriod int64
  31. Memory int64
  32. MemorySwap int64
  33. CgroupParent string
  34. ShmSize string
  35. Dockerfile string
  36. Ulimits []*ulimit.Ulimit
  37. BuildArgs []string
  38. AuthConfigs map[string]cliconfig.AuthConfig
  39. Context io.Reader
  40. }
  41. // ImageBuildResponse holds information
  42. // returned by a server after building
  43. // an image.
  44. type ImageBuildResponse struct {
  45. Body io.ReadCloser
  46. OSType string
  47. }
  48. // ImageBuild sends request to the daemon to build images.
  49. // The Body in the response implement an io.ReadCloser and it's up to the caller to
  50. // close it.
  51. func (cli *Client) ImageBuild(options ImageBuildOptions) (ImageBuildResponse, error) {
  52. query, err := imageBuildOptionsToQuery(options)
  53. if err != nil {
  54. return ImageBuildResponse{}, err
  55. }
  56. headers := http.Header(make(map[string][]string))
  57. buf, err := json.Marshal(options.AuthConfigs)
  58. if err != nil {
  59. return ImageBuildResponse{}, err
  60. }
  61. headers.Add("X-Registry-Config", base64.URLEncoding.EncodeToString(buf))
  62. headers.Set("Content-Type", "application/tar")
  63. serverResp, err := cli.POSTRaw("/build", query, options.Context, headers)
  64. if err != nil {
  65. return ImageBuildResponse{}, err
  66. }
  67. var osType string
  68. if h, err := httputils.ParseServerHeader(serverResp.header.Get("Server")); err == nil {
  69. osType = h.OS
  70. }
  71. return ImageBuildResponse{
  72. Body: serverResp.body,
  73. OSType: osType,
  74. }, nil
  75. }
  76. func imageBuildOptionsToQuery(options ImageBuildOptions) (url.Values, error) {
  77. query := url.Values{
  78. "t": options.Tags,
  79. }
  80. if options.SuppressOutput {
  81. query.Set("q", "1")
  82. }
  83. if options.RemoteContext != "" {
  84. query.Set("remote", options.RemoteContext)
  85. }
  86. if options.NoCache {
  87. query.Set("nocache", "1")
  88. }
  89. if options.Remove {
  90. query.Set("rm", "1")
  91. } else {
  92. query.Set("rm", "0")
  93. }
  94. if options.ForceRemove {
  95. query.Set("forcerm", "1")
  96. }
  97. if options.PullParent {
  98. query.Set("pull", "1")
  99. }
  100. if !runconfig.IsolationLevel.IsDefault(runconfig.IsolationLevel(options.Isolation)) {
  101. query.Set("isolation", options.Isolation)
  102. }
  103. query.Set("cpusetcpus", options.CPUSetCPUs)
  104. query.Set("cpusetmems", options.CPUSetMems)
  105. query.Set("cpushares", strconv.FormatInt(options.CPUShares, 10))
  106. query.Set("cpuquota", strconv.FormatInt(options.CPUQuota, 10))
  107. query.Set("cpuperiod", strconv.FormatInt(options.CPUPeriod, 10))
  108. query.Set("memory", strconv.FormatInt(options.Memory, 10))
  109. query.Set("memswap", strconv.FormatInt(options.MemorySwap, 10))
  110. query.Set("cgroupparent", options.CgroupParent)
  111. if options.ShmSize != "" {
  112. parsedShmSize, err := units.RAMInBytes(options.ShmSize)
  113. if err != nil {
  114. return query, err
  115. }
  116. query.Set("shmsize", strconv.FormatInt(parsedShmSize, 10))
  117. }
  118. query.Set("dockerfile", options.Dockerfile)
  119. ulimitsJSON, err := json.Marshal(options.Ulimits)
  120. if err != nil {
  121. return query, err
  122. }
  123. query.Set("ulimits", string(ulimitsJSON))
  124. buildArgs := runconfig.ConvertKVStringsToMap(options.BuildArgs)
  125. buildArgsJSON, err := json.Marshal(buildArgs)
  126. if err != nil {
  127. return query, err
  128. }
  129. query.Set("buildargs", string(buildArgsJSON))
  130. return query, nil
  131. }