build_classic.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "encoding/json"
  17. "fmt"
  18. "io"
  19. "os"
  20. "path/filepath"
  21. "runtime"
  22. "strings"
  23. "github.com/compose-spec/compose-go/types"
  24. buildx "github.com/docker/buildx/build"
  25. "github.com/docker/cli/cli/command/image/build"
  26. dockertypes "github.com/docker/docker/api/types"
  27. "github.com/docker/docker/cli"
  28. "github.com/docker/docker/pkg/archive"
  29. "github.com/docker/docker/pkg/idtools"
  30. "github.com/docker/docker/pkg/jsonmessage"
  31. "github.com/docker/docker/pkg/progress"
  32. "github.com/docker/docker/pkg/streamformatter"
  33. "github.com/docker/docker/pkg/urlutil"
  34. "github.com/hashicorp/go-multierror"
  35. "github.com/pkg/errors"
  36. "github.com/docker/compose/v2/pkg/api"
  37. )
  38. func (s *composeService) doBuildClassic(ctx context.Context, project *types.Project, opts map[string]buildx.Options) (map[string]string, error) {
  39. var nameDigests = make(map[string]string)
  40. var errs error
  41. err := project.WithServices(nil, func(service types.ServiceConfig) error {
  42. imageName := api.GetImageNameOrDefault(service, project.Name)
  43. o, ok := opts[imageName]
  44. if !ok {
  45. return nil
  46. }
  47. digest, err := s.doBuildClassicSimpleImage(ctx, o)
  48. if err != nil {
  49. errs = multierror.Append(errs, err).ErrorOrNil()
  50. }
  51. nameDigests[imageName] = digest
  52. return nil
  53. })
  54. if err != nil {
  55. return nil, err
  56. }
  57. return nameDigests, errs
  58. }
  59. //nolint:gocyclo
  60. func (s *composeService) doBuildClassicSimpleImage(ctx context.Context, options buildx.Options) (string, error) {
  61. var (
  62. buildCtx io.ReadCloser
  63. dockerfileCtx io.ReadCloser
  64. contextDir string
  65. tempDir string
  66. relDockerfile string
  67. err error
  68. )
  69. dockerfileName := options.Inputs.DockerfilePath
  70. specifiedContext := options.Inputs.ContextPath
  71. progBuff := s.stdout()
  72. buildBuff := s.stdout()
  73. if options.ImageIDFile != "" {
  74. // Avoid leaving a stale file if we eventually fail
  75. if err := os.Remove(options.ImageIDFile); err != nil && !os.IsNotExist(err) {
  76. return "", errors.Wrap(err, "removing image ID file")
  77. }
  78. }
  79. if len(options.Platforms) > 1 {
  80. return "", errors.Errorf("this builder doesn't support multi-arch build, set DOCKER_BUILDKIT=1 to use multi-arch builder")
  81. }
  82. switch {
  83. case isLocalDir(specifiedContext):
  84. contextDir, relDockerfile, err = build.GetContextFromLocalDir(specifiedContext, dockerfileName)
  85. if err == nil && strings.HasPrefix(relDockerfile, ".."+string(filepath.Separator)) {
  86. // Dockerfile is outside of build-context; read the Dockerfile and pass it as dockerfileCtx
  87. dockerfileCtx, err = os.Open(dockerfileName)
  88. if err != nil {
  89. return "", errors.Errorf("unable to open Dockerfile: %v", err)
  90. }
  91. defer dockerfileCtx.Close() //nolint:errcheck
  92. }
  93. case urlutil.IsGitURL(specifiedContext):
  94. tempDir, relDockerfile, err = build.GetContextFromGitURL(specifiedContext, dockerfileName)
  95. case urlutil.IsURL(specifiedContext):
  96. buildCtx, relDockerfile, err = build.GetContextFromURL(progBuff, specifiedContext, dockerfileName)
  97. default:
  98. return "", errors.Errorf("unable to prepare context: path %q not found", specifiedContext)
  99. }
  100. if err != nil {
  101. return "", errors.Errorf("unable to prepare context: %s", err)
  102. }
  103. if tempDir != "" {
  104. defer os.RemoveAll(tempDir) //nolint:errcheck
  105. contextDir = tempDir
  106. }
  107. // read from a directory into tar archive
  108. if buildCtx == nil {
  109. excludes, err := build.ReadDockerignore(contextDir)
  110. if err != nil {
  111. return "", err
  112. }
  113. if err := build.ValidateContextDirectory(contextDir, excludes); err != nil {
  114. return "", errors.Wrap(err, "checking context")
  115. }
  116. // And canonicalize dockerfile name to a platform-independent one
  117. relDockerfile = archive.CanonicalTarNameForPath(relDockerfile)
  118. excludes = build.TrimBuildFilesFromExcludes(excludes, relDockerfile, false)
  119. buildCtx, err = archive.TarWithOptions(contextDir, &archive.TarOptions{
  120. ExcludePatterns: excludes,
  121. ChownOpts: &idtools.Identity{},
  122. })
  123. if err != nil {
  124. return "", err
  125. }
  126. }
  127. // replace Dockerfile if it was added from stdin or a file outside the build-context, and there is archive context
  128. if dockerfileCtx != nil && buildCtx != nil {
  129. buildCtx, relDockerfile, err = build.AddDockerfileToBuildContext(dockerfileCtx, buildCtx)
  130. if err != nil {
  131. return "", err
  132. }
  133. }
  134. buildCtx, err = build.Compress(buildCtx)
  135. if err != nil {
  136. return "", err
  137. }
  138. progressOutput := streamformatter.NewProgressOutput(progBuff)
  139. body := progress.NewProgressReader(buildCtx, progressOutput, 0, "", "Sending build context to Docker daemon")
  140. configFile := s.configFile()
  141. creds, err := configFile.GetAllCredentials()
  142. if err != nil {
  143. return "", err
  144. }
  145. authConfigs := make(map[string]dockertypes.AuthConfig, len(creds))
  146. for k, auth := range creds {
  147. authConfigs[k] = dockertypes.AuthConfig(auth)
  148. }
  149. buildOptions := imageBuildOptions(options)
  150. buildOptions.Version = dockertypes.BuilderV1
  151. buildOptions.Dockerfile = relDockerfile
  152. buildOptions.AuthConfigs = authConfigs
  153. ctx, cancel := context.WithCancel(ctx)
  154. defer cancel()
  155. response, err := s.apiClient().ImageBuild(ctx, body, buildOptions)
  156. if err != nil {
  157. return "", err
  158. }
  159. defer response.Body.Close() //nolint:errcheck
  160. imageID := ""
  161. aux := func(msg jsonmessage.JSONMessage) {
  162. var result dockertypes.BuildResult
  163. if err := json.Unmarshal(*msg.Aux, &result); err != nil {
  164. fmt.Fprintf(s.stderr(), "Failed to parse aux message: %s", err)
  165. } else {
  166. imageID = result.ID
  167. }
  168. }
  169. err = jsonmessage.DisplayJSONMessagesStream(response.Body, buildBuff, progBuff.FD(), true, aux)
  170. if err != nil {
  171. if jerr, ok := err.(*jsonmessage.JSONError); ok {
  172. // If no error code is set, default to 1
  173. if jerr.Code == 0 {
  174. jerr.Code = 1
  175. }
  176. return "", cli.StatusError{Status: jerr.Message, StatusCode: jerr.Code}
  177. }
  178. return "", err
  179. }
  180. // Windows: show error message about modified file permissions if the
  181. // daemon isn't running Windows.
  182. if response.OSType != "windows" && runtime.GOOS == "windows" {
  183. // if response.OSType != "windows" && runtime.GOOS == "windows" && !options.quiet {
  184. fmt.Fprintln(s.stdout(), "SECURITY WARNING: You are building a Docker "+
  185. "image from Windows against a non-Windows Docker host. All files and "+
  186. "directories added to build context will have '-rwxr-xr-x' permissions. "+
  187. "It is recommended to double check and reset permissions for sensitive "+
  188. "files and directories.")
  189. }
  190. if options.ImageIDFile != "" {
  191. if imageID == "" {
  192. return "", errors.Errorf("Server did not provide an image ID. Cannot write %s", options.ImageIDFile)
  193. }
  194. if err := os.WriteFile(options.ImageIDFile, []byte(imageID), 0o666); err != nil {
  195. return "", err
  196. }
  197. }
  198. return imageID, nil
  199. }
  200. func isLocalDir(c string) bool {
  201. _, err := os.Stat(c)
  202. return err == nil
  203. }
  204. func imageBuildOptions(options buildx.Options) dockertypes.ImageBuildOptions {
  205. return dockertypes.ImageBuildOptions{
  206. Tags: options.Tags,
  207. NoCache: options.NoCache,
  208. Remove: true,
  209. PullParent: options.Pull,
  210. BuildArgs: toMapStringStringPtr(options.BuildArgs),
  211. Labels: options.Labels,
  212. NetworkMode: options.NetworkMode,
  213. ExtraHosts: options.ExtraHosts,
  214. Target: options.Target,
  215. }
  216. }
  217. func toMapStringStringPtr(source map[string]string) map[string]*string {
  218. dest := make(map[string]*string)
  219. for k, v := range source {
  220. v := v
  221. dest[k] = &v
  222. }
  223. return dest
  224. }