build.go 4.5 KB

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