Merge pull request #3002 from thaJeztah/20.10_backport_remove_all_example

[20.10 backport] docs: improve example for "remove all stopped containers"
This commit is contained in:
Silvin Lubecki 2021-03-09 21:07:18 +01:00 committed by GitHub
commit 9c8a91d22b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 5 deletions

View File

@ -58,13 +58,33 @@ The main process inside the container referenced under the link `redis` will rec
### Remove all stopped containers
```bash
$ docker rm $(docker ps -a -q)
Use the [`docker container prune`](container_prune.md) command to remove all
stopped containers, or refer to the [`docker system prune`](system_prune.md)
command to remove unused containers in addition to other Docker resources, such
as (unused) images and networks.
Alternatively, you can use the `docker ps` with the `-q` / `--quiet` option to
generate a list of container IDs to remove, and use that list as argument for
the `docker rm` command.
Combining commands can be more flexible, but is less portable as it depends
on features provided by the shell, and the exact syntax may differ depending on
what shell is used. To use this approach on Windows, consider using PowerShell
or Bash.
The example below uses `docker ps -q` to print the IDs of all containers that
have exited (`--filter status=exited`), and removes those containers with
the `docker rm` command:
```console
$ docker rm $(docker ps --filter status=exited -q)
```
This command deletes all stopped containers. The command
`docker ps -a -q` above returns all existing container IDs and passes them to
the `rm` command which deletes them. Running containers are not deleted.
Or, using the `xargs` Linux utility;
```console
$ docker ps --filter status=exited -q | xargs docker rm
```
### Remove a container and its volumes