build.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. "fmt"
  17. "os"
  18. "strings"
  19. "github.com/compose-spec/compose-go/v2/cli"
  20. "github.com/compose-spec/compose-go/v2/types"
  21. "github.com/docker/cli/cli/command"
  22. cliopts "github.com/docker/cli/opts"
  23. ui "github.com/docker/compose/v2/pkg/progress"
  24. "github.com/docker/compose/v2/pkg/utils"
  25. buildkit "github.com/moby/buildkit/util/progress/progressui"
  26. "github.com/spf13/cobra"
  27. "github.com/docker/compose/v2/pkg/api"
  28. )
  29. type buildOptions struct {
  30. *ProjectOptions
  31. quiet bool
  32. pull bool
  33. push bool
  34. args []string
  35. noCache bool
  36. memory cliopts.MemBytes
  37. ssh string
  38. builder string
  39. deps bool
  40. print bool
  41. }
  42. func (opts buildOptions) toAPIBuildOptions(services []string) (api.BuildOptions, error) {
  43. var SSHKeys []types.SSHKey
  44. if opts.ssh != "" {
  45. id, path, found := strings.Cut(opts.ssh, "=")
  46. if !found && id != "default" {
  47. return api.BuildOptions{}, fmt.Errorf("invalid ssh key %q", opts.ssh)
  48. }
  49. SSHKeys = append(SSHKeys, types.SSHKey{
  50. ID: id,
  51. Path: path,
  52. })
  53. }
  54. builderName := opts.builder
  55. if builderName == "" {
  56. builderName = os.Getenv("BUILDX_BUILDER")
  57. }
  58. uiMode := ui.Mode
  59. if uiMode == ui.ModeJSON {
  60. uiMode = "rawjson"
  61. }
  62. return api.BuildOptions{
  63. Pull: opts.pull,
  64. Push: opts.push,
  65. Progress: uiMode,
  66. Args: types.NewMappingWithEquals(opts.args),
  67. NoCache: opts.noCache,
  68. Quiet: opts.quiet,
  69. Services: services,
  70. Deps: opts.deps,
  71. Memory: int64(opts.memory),
  72. Print: opts.print,
  73. SSHs: SSHKeys,
  74. Builder: builderName,
  75. }, nil
  76. }
  77. func buildCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command {
  78. opts := buildOptions{
  79. ProjectOptions: p,
  80. }
  81. cmd := &cobra.Command{
  82. Use: "build [OPTIONS] [SERVICE...]",
  83. Short: "Build or rebuild services",
  84. PreRunE: Adapt(func(ctx context.Context, args []string) error {
  85. if opts.quiet {
  86. ui.Mode = ui.ModeQuiet
  87. devnull, err := os.Open(os.DevNull)
  88. if err != nil {
  89. return err
  90. }
  91. os.Stdout = devnull
  92. }
  93. return nil
  94. }),
  95. RunE: AdaptCmd(func(ctx context.Context, cmd *cobra.Command, args []string) error {
  96. if cmd.Flags().Changed("ssh") && opts.ssh == "" {
  97. opts.ssh = "default"
  98. }
  99. if cmd.Flags().Changed("progress") && opts.ssh == "" {
  100. fmt.Fprint(os.Stderr, "--progress is a global compose flag, better use `docker compose --progress xx build ...\n")
  101. }
  102. return runBuild(ctx, dockerCli, backend, opts, args)
  103. }),
  104. ValidArgsFunction: completeServiceNames(dockerCli, p),
  105. }
  106. flags := cmd.Flags()
  107. flags.BoolVar(&opts.push, "push", false, "Push service images")
  108. flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Don't print anything to STDOUT")
  109. flags.BoolVar(&opts.pull, "pull", false, "Always attempt to pull a newer version of the image")
  110. flags.StringArrayVar(&opts.args, "build-arg", []string{}, "Set build-time variables for services")
  111. flags.StringVar(&opts.ssh, "ssh", "", "Set SSH authentications used when building service images. (use 'default' for using your default SSH Agent)")
  112. flags.StringVar(&opts.builder, "builder", "", "Set builder to use")
  113. flags.BoolVar(&opts.deps, "with-dependencies", false, "Also build dependencies (transitively)")
  114. flags.Bool("parallel", true, "Build images in parallel. DEPRECATED")
  115. flags.MarkHidden("parallel") //nolint:errcheck
  116. flags.Bool("compress", true, "Compress the build context using gzip. DEPRECATED")
  117. flags.MarkHidden("compress") //nolint:errcheck
  118. flags.Bool("force-rm", true, "Always remove intermediate containers. DEPRECATED")
  119. flags.MarkHidden("force-rm") //nolint:errcheck
  120. flags.BoolVar(&opts.noCache, "no-cache", false, "Do not use cache when building the image")
  121. flags.Bool("no-rm", false, "Do not remove intermediate containers after a successful build. DEPRECATED")
  122. flags.MarkHidden("no-rm") //nolint:errcheck
  123. flags.VarP(&opts.memory, "memory", "m", "Set memory limit for the build container. Not supported by BuildKit.")
  124. flags.StringVar(&p.Progress, "progress", string(buildkit.AutoMode), fmt.Sprintf(`Set type of ui output (%s)`, strings.Join(printerModes, ", ")))
  125. flags.MarkHidden("progress") //nolint:errcheck
  126. flags.BoolVar(&opts.print, "print", false, "Print equivalent bake file")
  127. return cmd
  128. }
  129. func runBuild(ctx context.Context, dockerCli command.Cli, backend api.Service, opts buildOptions, services []string) error {
  130. project, _, err := opts.ToProject(ctx, dockerCli, services, cli.WithResolvedPaths(true), cli.WithoutEnvironmentResolution)
  131. if err != nil {
  132. return err
  133. }
  134. services = addBuildDependencies(services, project)
  135. if err := applyPlatforms(project, false); err != nil {
  136. return err
  137. }
  138. apiBuildOptions, err := opts.toAPIBuildOptions(services)
  139. if err != nil {
  140. return err
  141. }
  142. return backend.Build(ctx, project, apiBuildOptions)
  143. }
  144. func addBuildDependencies(services []string, project *types.Project) []string {
  145. servicesWithDependencies := utils.NewSet(services...)
  146. for _, service := range services {
  147. build := project.Services[service].Build
  148. if build != nil {
  149. for _, target := range build.AdditionalContexts {
  150. if s, found := strings.CutPrefix(target, types.ServicePrefix); found {
  151. servicesWithDependencies.Add(s)
  152. }
  153. }
  154. }
  155. }
  156. if len(servicesWithDependencies) > len(services) {
  157. return addBuildDependencies(servicesWithDependencies.Elements(), project)
  158. }
  159. return servicesWithDependencies.Elements()
  160. }