down.go 2.7 KB

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