Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between "docker container prune" vs "docker rm $(docker container ls -aq)"

Tags:

docker

I'm reading through the Docker documentation and I don't understand the difference between:

docker container prune

and

docker rm $(docker container ls -aq)

Note that in the link, the second command I've listed is docker rm $(docker ps -a -q), but there is no difference between that and what I've written. container ls is just the newer version of the ps command.

It seems that both of these commands remove all stopped containers. Is there more to it than that, or are these just synonyms?

like image 806
marcman Avatar asked Aug 31 '20 13:08

marcman


1 Answers

I don't think there is substantial difference. This -a though means list all containers and as a result docker rm ... will also try to remove running containers. This gives the error that you see below:

Error response from daemon: You cannot remove a running container [...] Stop the container before attempting removal or force remove

example:

$ docker container run --rm -itd alpine:latest 
0ec4d7459d35749ecc24cc5c6fd748f4254b0782f73f1ede76cf49b1fc53b2d4

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
0ec4d7459d35        alpine:latest       "/bin/sh"           4 seconds ago       Up 1 second                             jovial_ritchie

$ docker rm $(docker container ls -aq)
Error response from daemon: You cannot remove a running container 0ec4d7459d35749ecc24cc5c6fd748f4254b0782f73f1ede76cf49b1fc53b2d4. Stop the container before attempting removal or force remove

$ docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B

But... difference when --force, -f is used:

In this case, the commands do 2 different things:

  • docker rm -f ... Forces the removal of a running container (uses SIGKILL) which means that it will remove running containers.

    $ docker rm -f $(docker container ls -aq)
    0ec4d7459d35
    
  • docker container prune -f will remove all stopped containers without asking for confirmation (no [y/N] prompt will be printed).

    $ docker container prune -f
    Total reclaimed space: 0B
    
like image 199
tgogos Avatar answered Oct 13 '22 00:10

tgogos