Reputation: 10617
How can I stop and remove all docker containers to create a clean slate with my Docker containers? Lots of times I feel it is easier to start from scratch, but I have a bunch of containers that I am not sure what their states are, then when I run docker rm
it won't let me because the docker container could still be in use.
Upvotes: 346
Views: 574534
Reputation: 13
win11
docker rm $(docker ps -aq)
mac
docker ps -aq | xargs docker rm
Upvotes: 0
Reputation: 5018
This will kill all running docker containers:
docker kill $(docker ps -q)
This will clean up unused Docker resources:
docker system prune -a
Upvotes: 16
Reputation: 433
I arrived at a combination of docker ps
and xargs
that cleanly stops and removes all containers. Bonus: it is configurable so you can do other things if you want to.
echo $(docker ps -a -q)|xargs -i sh -c 'docker stop {}; docker rm {};'
Explanation:
docker ps -a -q
List containers
$()
The command substitution expands to the output of commands. These commands are executed in a subshell, and their stdout data is what the substitution syntax expands to.
echo $(docker ps -a -q)
print the substituted output from docker ps
. Without this the shell will fail by attempting to execute the ID of the first container as a shell command. (e.g. "<container-id>: command not found")
|
creates a pipe, a unidirectional data channel that can be used for interprocess communication.
xargs -i
build and execute command lines from standard input
sh -c
dash — command interpreter (shell) (dash is the standard command interpreter for the system.)
'docker stop {}; docker rm {};'
the shell command to execute. {}
represents each container ID output from the earlier docker ps -a -q
command.
Upvotes: 0
Reputation: 6090
docker ps -aq | xargs docker stop | xargs docker rm
or
docker ps -aq | xargs docker rm -f
docker ps -aq
docker ps -a
or docker ps --all
returns all containers and not only running containersdocker ps -q
or docker ps --quiet
returns container idsdocker rm -f
or docker rm --force
removes also running containers
Upvotes: 47
Reputation: 2765
An answer on the spirit of Pankaj Rastogi's comment, in one of the answers.
docker ps > containers.txt
CONTAINER ID
s to docker rm
one by one.cat containers.txt | tail -n +2 | cut -d ' ' -f 1 | xargs -I{} docker rm {}
.
The latter lists all container entries, which start with a container id. Entries are separated by space and therefore, tail -n +2
skips the first line which is headers, cut -d ' ' -f 1
, splits every line by space and takes the first line entry, which is the container id and xargs -I{} docker rm {}
removes the container with the specific id.
NOTES:
udocker
.grep
step, a bit before xargs
, in order to filter out some of them.Upvotes: 0
Reputation: 8253
One command line for cleaning all containers
docker system prune -f ; docker volume prune -f ;docker rm -f -v $(docker ps -q -a)
Upvotes: 21
Reputation: 9906
Docker introduced new namespaces and commands which everyone should finally learn and not stick to the old habits. Here is the documentation, and here are some examples:
Deleting no longer needed containers (stopped)
docker container prune
Deleting no longer needed images
which means, that it only deletes images, which are not tagged and are not pointed on by "latest" - so no real images you can regularly use are deleted
docker image prune
Delete all volumes, which are not used by any existing container
( even stopped containers do claim volumes ). This usually cleans up dangling anon-volumes of containers have been deleted long time ago. It should never delete named volumes since the containers of those should exists / be running. Be careful, ensure your stack at least is running before going with this one
docker volume prune
Same for unused networks
docker network prune
And finally, if you want to get rid if all the trash - to ensure nothing happens to your production, be sure all stacks are running and then run
docker system prune
Upvotes: 122
Reputation: 1261
stop containers:
docker stop container1_id container2_id containerz_id
delete all image:
docker system prune --all
Upvotes: 7
Reputation: 8063
If you are concerned about shellcheck SC2046 (in which case you would receive a warning for the command docker stop $(docker ps -a -q)
) and you are using Bash4+ (to be able to use the mapfile
builtin command), you can run the following to stop all containers:
mapfile -t list < <(docker ps -q)
[[ ${#list[@]} -gt 0 ]] && docker container stop "${list[@]}"
The reasoning:
docker ps -q
returns all active containers ids
mapfile
stores the resulting container ids in the list
array
[[ ${#list[@]} -gt 0 ]]
tests if the list
array has 1 or more elements to execute the next command
docker container stop "${list[@]}"
stops all containers whose ids are stored in the list
array (will only run if the array has items)
Similarly, to remove all stopped containers:
mapfile -t list < <(docker ps -aq)
[[ ${#list[@]} -gt 0 ]] && docker container rm "${list[@]}"
(docker ps -aq
returns all container ids, even from stopped containers)
If you want to stop and remove all containers, you can run the above commands sequentially (first stop
, then rm
).
Alternatively, you can run only the the commands to remove the containers with rm --force
, but keep in mind that this will send SIGKILL, so running stop
first and then rm
is advisable (stop
sends SIGTERM, unless it times out, in which case it sends SIGKILL).
Upvotes: 4
Reputation: 19456
We can achieve this with one liner (also removes running containers)
docker container rm $(docker container ls -aq) -f
What it does
docker container ls -aq
lists container's ids only
docker container rm $(..) -f
forcibly removes all container's ids
Upvotes: 9
Reputation: 476
then when I run docker rm it won't let me because the docker container could still be in use.
The steps I would suggest are in following order,
1. Try to remove
docker rm <container-id>
2. rm doesn't work, use stop
docker stop <container-id>
3. stop doesn't work? try kill
docker kill <container-id>
4. stop worked but still container is there? try prune to remove all the stopped container forcefully
docker container prune -f
Upvotes: 0
Reputation: 41103
The one liner:
docker rm -f $(docker ps -a -q)
If you only want to do the running ones, remove -a
.
Upvotes: 8
Reputation: 1985
In ubuntu I just killed all processes when other solutions didn't work.
$ ps aux | grep docker
$ sudo kill {enter process id here}
I do not recommend doing this way.I was on reinstalling your OS, when these command saved some time for fixing my issues with containers.
Upvotes: -1
Reputation: 7111
Stop all the containers
docker stop $(docker ps -a -q)
Remove all the containers
docker rm $(docker ps -a -q)
Find more command here
Upvotes: 646