build.go 3.3 KB

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