Stop and remove all docker containers

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.

3

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

3

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 
2
docker ps -aq | xargs docker stop | xargs docker rm 

or

docker ps -aq | xargs docker rm -f 
1

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

Here's a good gist I use for this kind of thing: From this link that people seem to not like ()

delete volumes

$ docker volume rm $(docker volume ls -qf dangling=true) $ docker volume ls -qf dangling=true | xargs -r docker volume rm 

delete networks

$ docker network ls $ docker network ls | grep "bridge" $ docker network rm $(docker network ls | grep "bridge" | awk '/ / { print $1 }') 

remove docker images

// see:

$ docker images $ docker rmi $(docker images --filter "dangling=true" -q --no-trunc) $ docker images | grep "none" $ docker rmi $(docker images | grep "none" | awk '/ / { print $3 }') 

remove docker containers

// see:

$ docker ps $ docker ps -a $ docker rm $(docker ps -qa --no-trunc --filter "status=exited") 

Essentially you want to kill all your running containers, remove every image, uninstall docker, reinstall the version you want and that should be about as clean a slate as it gets.

5

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).

3

One command line for cleaning all containers

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

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.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like