build.go 4.7 KB

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