2021-02-15 09:13:41 +01:00
|
|
|
/*
|
|
|
|
Copyright 2020 Docker Compose CLI authors
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package compose
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2023-08-30 10:15:35 +02:00
|
|
|
"github.com/docker/cli/cli/command"
|
2021-08-31 18:53:24 +02:00
|
|
|
"github.com/docker/compose/v2/pkg/api"
|
2021-02-15 09:13:41 +01:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type removeOptions struct {
|
2022-12-19 16:38:36 +00:00
|
|
|
*ProjectOptions
|
2021-02-15 09:13:41 +01:00
|
|
|
force bool
|
|
|
|
stop bool
|
|
|
|
volumes bool
|
|
|
|
}
|
|
|
|
|
2023-08-30 10:15:35 +02:00
|
|
|
func removeCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command {
|
2021-02-15 09:13:41 +01:00
|
|
|
opts := removeOptions{
|
2022-12-19 16:38:36 +00:00
|
|
|
ProjectOptions: p,
|
2021-02-15 09:13:41 +01:00
|
|
|
}
|
|
|
|
cmd := &cobra.Command{
|
2022-08-02 13:11:57 +02:00
|
|
|
Use: "rm [OPTIONS] [SERVICE...]",
|
2021-02-15 09:13:41 +01:00
|
|
|
Short: "Removes stopped service containers",
|
|
|
|
Long: `Removes stopped service containers
|
|
|
|
|
|
|
|
By default, anonymous volumes attached to containers will not be removed. You
|
|
|
|
can override this with -v. To list all volumes, use "docker volume ls".
|
|
|
|
|
|
|
|
Any data which is not in a volume will be lost.`,
|
2021-04-15 12:43:18 +02:00
|
|
|
RunE: Adapt(func(ctx context.Context, args []string) error {
|
2023-08-30 10:15:35 +02:00
|
|
|
return runRemove(ctx, dockerCli, backend, opts, args)
|
2021-04-15 12:43:18 +02:00
|
|
|
}),
|
2023-08-30 10:15:35 +02:00
|
|
|
ValidArgsFunction: completeServiceNames(dockerCli, p),
|
2021-02-15 09:13:41 +01:00
|
|
|
}
|
|
|
|
f := cmd.Flags()
|
|
|
|
f.BoolVarP(&opts.force, "force", "f", false, "Don't ask to confirm removal")
|
|
|
|
f.BoolVarP(&opts.stop, "stop", "s", false, "Stop the containers, if required, before removing")
|
|
|
|
f.BoolVarP(&opts.volumes, "volumes", "v", false, "Remove any anonymous volumes attached to containers")
|
2021-04-28 14:36:17 +02:00
|
|
|
f.BoolP("all", "a", false, "Deprecated - no effect")
|
|
|
|
f.MarkHidden("all") //nolint:errcheck
|
|
|
|
|
2021-02-15 09:13:41 +01:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2023-08-30 10:15:35 +02:00
|
|
|
func runRemove(ctx context.Context, dockerCli command.Cli, backend api.Service, opts removeOptions, services []string) error {
|
2024-02-05 17:38:15 -05:00
|
|
|
project, name, err := opts.projectOrName(ctx, dockerCli, services...)
|
2021-02-15 09:13:41 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-11 10:58:20 +02:00
|
|
|
return backend.Remove(ctx, name, api.RemoveOptions{
|
2021-04-20 15:56:48 +02:00
|
|
|
Services: services,
|
2021-06-25 13:46:34 +02:00
|
|
|
Force: opts.force,
|
|
|
|
Volumes: opts.volumes,
|
2022-04-11 10:58:20 +02:00
|
|
|
Project: project,
|
2023-02-08 22:40:07 +01:00
|
|
|
Stop: opts.stop,
|
2021-02-15 10:46:56 +01:00
|
|
|
})
|
2021-02-15 09:13:41 +01:00
|
|
|
}
|