down.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/docker/cli/cli/command"
  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, dockerCli command.Cli, backend api.Service) *cobra.Command {
  35. opts := downOptions{
  36. ProjectOptions: p,
  37. }
  38. downCmd := &cobra.Command{
  39. Use: "down [OPTIONS] [SERVICES]",
  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, dockerCli, backend, opts, args)
  52. }),
  53. ValidArgsFunction: noCompletion(),
  54. }
  55. flags := downCmd.Flags()
  56. removeOrphans := utils.StringToBool(os.Getenv(ComposeRemoveOrphans))
  57. flags.BoolVar(&opts.removeOrphans, "remove-orphans", removeOrphans, "Remove containers for services not defined in the Compose file")
  58. flags.IntVarP(&opts.timeout, "timeout", "t", 0, "Specify a shutdown timeout in seconds")
  59. 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`)
  60. flags.StringVar(&opts.images, "rmi", "", `Remove images used by services. "local" remove only images that don't have a custom tag ("local"|"all")`)
  61. flags.SetNormalizeFunc(func(f *pflag.FlagSet, name string) pflag.NormalizedName {
  62. if name == "volume" {
  63. name = "volumes"
  64. logrus.Warn("--volume is deprecated, please use --volumes")
  65. }
  66. return pflag.NormalizedName(name)
  67. })
  68. return downCmd
  69. }
  70. func runDown(ctx context.Context, dockerCli command.Cli, backend api.Service, opts downOptions, services []string) error {
  71. project, name, err := opts.projectOrName(ctx, dockerCli, services...)
  72. if err != nil {
  73. return err
  74. }
  75. var timeout *time.Duration
  76. if opts.timeChanged {
  77. timeoutValue := time.Duration(opts.timeout) * time.Second
  78. timeout = &timeoutValue
  79. }
  80. return backend.Down(ctx, name, api.DownOptions{
  81. RemoveOrphans: opts.removeOrphans,
  82. Project: project,
  83. Timeout: timeout,
  84. Images: opts.images,
  85. Volumes: opts.volumes,
  86. Services: services,
  87. })
  88. }