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

Viewed 3491

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?

3 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
    

The effects of the two commands are indeed similar, but there are some nuances to consider:

  • docker container prune can be used with the --filter option.
  • docker container prune has a synchronous protection that blocks concurrent prune executions on the daemon.
  • docker container prune attempts to remove only the containers that are not running, instead of trying to delete all containers and relying on the daemon to throw an exception for those that are not stopped, therefore is quicker and does not generate unnecessary error logs in case someone is tracking the daemon logs.
  • docker container prune builds a report at the end of its execution, providing the reclaimed space. The report is added in daemon.EventsService and implicitly displayed on the screen.
  • docker container prune is shorter

In the end of this answer I have a question: Why would someone type 15 additional characters to get the same result or worse?

docker system prune -f : to remove all the stopped containers (docker do not touch the running containers)

docker system prune -a : to remove all the stopped containers (docker do not touch the running containers) + unused images

docker rm <container_id> : remove a specific container, it should be stopped before (docker stop <container_id>)

Related