7

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?

marcman
  • 3,233
  • 4
  • 36
  • 71
  • Why do you expect to do different things? I mean one is a command and the other is two commands being piped. There's no difference `prune` is a shorthand of the old `docker rm $(docker ps -a -q)` and as you noted the `docker container ls` is just a new api. I'm guessing `ps` was too atached to UNIX OS while the other is more clear. But that is just a guess. – nax Aug 31 '20 at 14:19
  • @nax: I figured documentation typically avoids being redundant if possible. That both had different, unlinked dedicated sections on the same documentation struck me as odd if they were purely synonymous – marcman Aug 31 '20 at 14:53

3 Answers3

3

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?

Neo Anderson
  • 5,957
  • 2
  • 12
  • 29
2

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
    
tgogos
  • 23,218
  • 20
  • 96
  • 128
  • 1
    You are right on this. The `prune` command is just the evolution of what everyone did when the command didn't exist in fact i'm so used to the old command that I never use prune. but it's the same. – nax Aug 31 '20 at 14:17
  • @nax, do you have any idea when was the `prune` command introduced? Just for curiosity I dug into the oldest docker version that I could find in my old sources, which dates since 5 years ago and the prune command was present. I remember using `prune` since the first days I started using docker. – Neo Anderson Aug 31 '20 at 18:44
  • 2
    The Docker Engine release notes found here: [1.13.1 (2017-02-08)](https://docs.docker.com/engine/release-notes/prior-releases/) point to this discussion: https://github.com/moby/moby/pull/26108 so it must be end of 2016 / start of 2017. – tgogos Aug 31 '20 at 18:51
  • 2
    @NeoAnderson in the docs you can see it requires docker 1.25+ so at that point was oficially released but as tgogos pointed it was on the wild on 1.31.1 I'm getting old ... – nax Sep 01 '20 at 09:30
1

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

gishak
  • 11
  • 1