build.go 4.8 KB

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