down.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. "time"
  19. "github.com/compose-spec/compose-go/types"
  20. "github.com/docker/compose/v2/pkg/utils"
  21. "github.com/sirupsen/logrus"
  22. "github.com/spf13/cobra"
  23. "github.com/spf13/pflag"
  24. "github.com/docker/compose/v2/pkg/api"
  25. )
  26. type downOptions struct {
  27. *projectOptions
  28. removeOrphans bool
  29. timeChanged bool
  30. timeout int
  31. volumes bool
  32. images string
  33. }
  34. func downCommand(p *projectOptions, backend api.Service) *cobra.Command {
  35. opts := downOptions{
  36. projectOptions: p,
  37. }
  38. downCmd := &cobra.Command{
  39. Use: "down",
  40. Short: "Stop and remove containers, networks",
  41. PreRunE: AdaptCmd(func(ctx context.Context, cmd *cobra.Command, args []string) error {
  42. opts.timeChanged = cmd.Flags().Changed("timeout")
  43. if opts.images != "" {
  44. if opts.images != "all" && opts.images != "local" {
  45. return fmt.Errorf("invalid value for --rmi: %q", opts.images)
  46. }
  47. }
  48. return nil
  49. }),
  50. RunE: Adapt(func(ctx context.Context, args []string) error {
  51. return runDown(ctx, backend, opts)
  52. }),
  53. Args: cobra.NoArgs,
  54. ValidArgsFunction: noCompletion(),
  55. }
  56. flags := downCmd.Flags()
  57. removeOrphans := utils.StringToBool(os.Getenv("COMPOSE_REMOVE_ORPHANS "))
  58. flags.BoolVar(&opts.removeOrphans, "remove-orphans", removeOrphans, "Remove containers for services not defined in the Compose file.")
  59. flags.IntVarP(&opts.timeout, "timeout", "t", 10, "Specify a shutdown timeout in seconds")
  60. flags.BoolVarP(&opts.volumes, "volumes", "v", false, " Remove named volumes declared in the `volumes` section of the Compose file and anonymous volumes attached to containers.")
  61. flags.StringVar(&opts.images, "rmi", "", `Remove images used by services. "local" remove only images that don't have a custom tag ("local"|"all")`)
  62. flags.SetNormalizeFunc(func(f *pflag.FlagSet, name string) pflag.NormalizedName {
  63. switch name {
  64. case "volume":
  65. name = "volumes"
  66. logrus.Warn("--volume is deprecated, please use --volumes")
  67. }
  68. return pflag.NormalizedName(name)
  69. })
  70. return downCmd
  71. }
  72. func runDown(ctx context.Context, backend api.Service, opts downOptions) error {
  73. name := opts.ProjectName
  74. var project *types.Project
  75. if opts.ProjectName == "" {
  76. p, err := opts.toProject(nil)
  77. if err != nil {
  78. return err
  79. }
  80. project = p
  81. name = p.Name
  82. }
  83. var timeout *time.Duration
  84. if opts.timeChanged {
  85. timeoutValue := time.Duration(opts.timeout) * time.Second
  86. timeout = &timeoutValue
  87. }
  88. return backend.Down(ctx, name, api.DownOptions{
  89. RemoveOrphans: opts.removeOrphans,
  90. Project: project,
  91. Timeout: timeout,
  92. Images: opts.images,
  93. Volumes: opts.volumes,
  94. })
  95. }