build.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/cli"
  20. "github.com/compose-spec/compose-go/types"
  21. buildx "github.com/docker/buildx/util/progress"
  22. "github.com/docker/compose/v2/pkg/utils"
  23. "github.com/spf13/cobra"
  24. "github.com/docker/compose/v2/pkg/api"
  25. )
  26. type buildOptions struct {
  27. *projectOptions
  28. composeOptions
  29. quiet bool
  30. pull bool
  31. progress string
  32. args []string
  33. noCache bool
  34. memory string
  35. }
  36. var printerModes = []string{
  37. buildx.PrinterModeAuto,
  38. buildx.PrinterModeTty,
  39. buildx.PrinterModePlain,
  40. buildx.PrinterModeQuiet,
  41. }
  42. func buildCommand(p *projectOptions, backend api.Service) *cobra.Command {
  43. opts := buildOptions{
  44. projectOptions: p,
  45. }
  46. cmd := &cobra.Command{
  47. Use: "build [SERVICE...]",
  48. Short: "Build or rebuild services",
  49. PreRunE: Adapt(func(ctx context.Context, args []string) error {
  50. if opts.memory != "" {
  51. fmt.Println("WARNING --memory is ignored as not supported in buildkit.")
  52. }
  53. if opts.quiet {
  54. opts.progress = buildx.PrinterModeQuiet
  55. devnull, err := os.Open(os.DevNull)
  56. if err != nil {
  57. return err
  58. }
  59. os.Stdout = devnull
  60. }
  61. if !utils.StringContains(printerModes, opts.progress) {
  62. return fmt.Errorf("unsupported --progress value %q", opts.progress)
  63. }
  64. return nil
  65. }),
  66. RunE: Adapt(func(ctx context.Context, args []string) error {
  67. return runBuild(ctx, backend, opts, args)
  68. }),
  69. ValidArgsFunction: serviceCompletion(p),
  70. }
  71. cmd.Flags().BoolVarP(&opts.quiet, "quiet", "q", false, "Don't print anything to STDOUT")
  72. cmd.Flags().BoolVar(&opts.pull, "pull", false, "Always attempt to pull a newer version of the image.")
  73. cmd.Flags().StringVar(&opts.progress, "progress", buildx.PrinterModeAuto, fmt.Sprintf(`Set type of progress output (%s)`, strings.Join(printerModes, ", ")))
  74. cmd.Flags().StringArrayVar(&opts.args, "build-arg", []string{}, "Set build-time variables for services.")
  75. cmd.Flags().Bool("parallel", true, "Build images in parallel. DEPRECATED")
  76. cmd.Flags().MarkHidden("parallel") //nolint:errcheck
  77. cmd.Flags().Bool("compress", true, "Compress the build context using gzip. DEPRECATED")
  78. cmd.Flags().MarkHidden("compress") //nolint:errcheck
  79. cmd.Flags().Bool("force-rm", true, "Always remove intermediate containers. DEPRECATED")
  80. cmd.Flags().MarkHidden("force-rm") //nolint:errcheck
  81. cmd.Flags().BoolVar(&opts.noCache, "no-cache", false, "Do not use cache when building the image")
  82. cmd.Flags().Bool("no-rm", false, "Do not remove intermediate containers after a successful build. DEPRECATED")
  83. cmd.Flags().MarkHidden("no-rm") //nolint:errcheck
  84. cmd.Flags().StringVarP(&opts.memory, "memory", "m", "", "Set memory limit for the build container. Not supported on buildkit yet.")
  85. cmd.Flags().MarkHidden("memory") //nolint:errcheck
  86. return cmd
  87. }
  88. func runBuild(ctx context.Context, backend api.Service, opts buildOptions, services []string) error {
  89. project, err := opts.toProject(services, cli.WithResolvedPaths(true))
  90. if err != nil {
  91. return err
  92. }
  93. return backend.Build(ctx, project, api.BuildOptions{
  94. Pull: opts.pull,
  95. Progress: opts.progress,
  96. Args: types.NewMappingWithEquals(opts.args),
  97. NoCache: opts.noCache,
  98. Quiet: opts.quiet,
  99. Services: services,
  100. })
  101. }