Stop and remove all docker containers

Viewed 124217

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.

12 Answers

Stop all the containers

docker stop $(docker ps -a -q)

Remove all the containers

docker rm $(docker ps -a -q)

Find more command here

docker ps -aq | xargs docker stop | xargs docker rm

or

docker ps -aq | xargs docker rm -f

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

One command line for cleaning all containers

docker system prune -f ; docker volume prune -f ;docker rm -f -v $(docker ps -q -a)

The one liner:

docker rm -f $(docker ps -a -q)

If you only want to do the running ones, remove -a.

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 listarray (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 stopfirst and then rm is advisable (stop sends SIGTERM, unless it times out, in which case it sends SIGKILL).

stop containers:

docker stop container1_id container2_id containerz_id 

delete all image:

docker system prune --all

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

To remove all Docker images:

docker rmi $(docker images -aq)

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.

Related