jjbskir
jjbskir

Reputation: 10617

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.

Upvotes: 346

Views: 574534

Answers (16)

Prasad tonds
Prasad tonds

Reputation: 13

win11

docker rm $(docker ps -aq)

mac

docker ps -aq | xargs docker rm

Upvotes: 0

Bing
Bing

Reputation: 159

You may try this: sudo docker stop $(sudo docker ps -a -q).

Upvotes: 1

balu k
balu k

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

Victor Ude
Victor Ude

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

  • -a, --all[=false] Show all containers (default shows just running)
  • -q, --quiet[=false] Only display container IDs

$() 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

  • -i[replace-str], --replace[=replace-str] This option is a synonym for -Ireplace-str if replace-str is specified. If the replace-str argument is missing, the effect is the same as -I{}. This option is deprecated; use -I instead.

sh -c dash — command interpreter (shell) (dash is the standard command interpreter for the system.)

  • -c Read commands from the command_string operand instead of from the standard input. Special parameter 0 will be set from the command_name operand and the positional parameters ($1, $2, etc.) set from the remaining argument operands.

'docker stop {}; docker rm {};' the shell command to execute. {} represents each container ID output from the earlier docker ps -a -q command.

Upvotes: 0

Andriy Tolstoy
Andriy Tolstoy

Reputation: 6090

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

or

docker ps -aq | xargs docker rm -f

docker ps -aq

  1. docker ps -a or docker ps --all returns all containers and not only running containers
  2. docker ps -q or docker ps --quiet returns container ids

docker rm -f or docker rm --force removes also running containers

Upvotes: 47

Yannis P.
Yannis P.

Reputation: 2765

An answer on the spirit of Pankaj Rastogi's comment, in one of the answers.

  1. create a list of all containers and store them in a text file

docker ps > containers.txt

  1. pass the CONTAINER IDs 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:

  • the above works also for udocker.
  • these commands, remove all images. If you want to inly remove some images, you can perhaps use a grep step, a bit before xargs, in order to filter out some of them.

Upvotes: 0

Willie Cheng
Willie Cheng

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

vidya
vidya

Reputation: 31

To remove all Docker images:

docker rmi $(docker images -aq)

Upvotes: 3

Eugen Mayer
Eugen Mayer

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

Rashid Iqbal
Rashid Iqbal

Reputation: 1261

stop containers:

docker stop container1_id container2_id containerz_id 

delete all image:

docker system prune --all

Upvotes: 7

Lucas Basquerotto
Lucas Basquerotto

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

Upvotes: 4

Alex Nolasco
Alex Nolasco

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

Pritesh Gohil
Pritesh Gohil

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

Travis Reeder
Travis Reeder

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

Aipo
Aipo

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

John Kuriakose
John Kuriakose

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

Related Questions