build.go 3.2 KB

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