15

I use following commands to remove all docker containers:

docker ps -q | xargs docker stop
docker ps -aq --no-trunc -f status=exited | xargs docker rm

But anyway I see containers after:

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
70cb7107d00d        24820714bfc6        "wait-for-it.sh mysqâ¦"   21 minutes ago      Created                                 sql_migration

Then, I executed the command:

docker rm sql_migration

And it removed the container.

Can you please help to correct initial command and explain why it doesn't work.

Also, I would be grateful if you explained how to change container to status like sql_migration

tgogos
  • 23,218
  • 20
  • 96
  • 128
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • This might be helpful http://stackoverflow.com/questions/32723111/how-to-remove-old-and-unused-docker-images – Robin Topper Aug 29 '18 at 08:43
  • Possible duplicate of [What does CREATED container mean in docker?](https://stackoverflow.com/questions/43734412/what-does-created-container-mean-in-docker) – atline Aug 29 '18 at 08:50

5 Answers5

28

1. Remove what can be removed...

To remove all exited and created containers but not the Up (running) ones:

docker container prune -f

-f or --force prevents you from being prompted to answer the following:

WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N]

example: (Notice at the end that one container is not being removed)

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
053ce57276b7        alpine              "/bin/sh"           3 seconds ago       Created                                         eager_meninsky
55f20431a536        alpine              "/bin/sh"           15 seconds ago      Up 14 seconds                                   hardcore_clarke
44cbe2dc81b0        alpine              "/bin/sh"           38 seconds ago      Exited (0) 37 seconds ago                       test
647747afb9a4        alpine              "/bin/sh"           18 hours ago        Exited (137) 18 hours ago                       admiring_visvesvaraya


$ docker container prune -f
Deleted Containers:
053ce57276b7d7008272e95991cf950268b9b32676b1389ec6e8ab6e6b755dc9
44cbe2dc81b0522e0fd0e53f28a4d3871b818b9b17681dd011c8680ab37b51e7
647747afb9a431a2c5040e6aba5119b199b94061c444ff0194aaa809dbf849b8

Total reclaimed space: 0B


$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
55f20431a536        alpine              "/bin/sh"           45 seconds ago      Up 44 seconds                           hardcore_clarke

2. Stop all & remove...

docker container stop $(docker container ls -aq)
docker container prune -f

example:

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
da7ffd8efb62        alpine              "/bin/sh"           5 seconds ago       Created                                         quirky_poitras
31cb71a8899c        alpine              "/bin/sh"           20 seconds ago      Exited (0) 13 seconds ago                       elastic_shaw
becfdc81228c        alpine              "/bin/sh"           25 seconds ago      Up 24 seconds                                   thirsty_murdock


$ docker container stop $(docker container ls -aq)
da7ffd8efb62
31cb71a8899c
becfdc81228c


$ docker container prune -f
Deleted Containers:
da7ffd8efb623677882b1534008e66a1530baa94e0473be537ef5c415c928ba3
31cb71a8899c47472d0ccb5710e34ff08b4ef142599d4e857e3e69740a2e59b5
becfdc81228cdf41519102ea780956eed71a86103e849dff3d9f7cca0a54651f

Total reclaimed space: 5B


$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
Community
  • 1
  • 1
tgogos
  • 23,218
  • 20
  • 96
  • 128
  • Docker is not as straight forward as I think it could be when it comes to rebuilding containers. For me, there was a learning curve, and since building environments is not something I do all the time, I forget the exact syntax of the commands. I will have to remember these commands. – Jed Lynch Oct 07 '21 at 04:00
9

I use the following command to remove all the docker containers:

docker rm $(docker ps -aq)

Riccardo Persiani
  • 702
  • 1
  • 5
  • 25
2

I use the following commands to remove all containers:

docker stop $(docker ps -aq)
docker rm $(docker ps -aq)
SapuSeven
  • 1,473
  • 17
  • 30
1

One liner to stop / remove all of Docker containers:

docker stop $(docker ps -a -q)

docker rm $(docker ps -a -q)

Salman Iftikhar
  • 311
  • 2
  • 9
1

Case 1. In order to remove only Stopped Containers use sudo docker container prune -f

$ docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
4a7f7eebae0f63178aff7eb0aa39cd3f0627a203ab2df258c1a00b456cf20063
f98f9c2aa1eaf727e4ec9c0283bc7d4aa4762fbdba7f26191f26c97f64090360

Total reclaimed space: 212 B

Case 2. In order to remove all existing docker containers on system ( In all states - running / stopped / exited containers ) usesudo docker container ls -aq | xargs sudo docker container rm,

commands explanation:

  • sudo container ls -aq - lists all containers on system by continaer id filter.
  • xargs - iteration over the pipe output and execution of command on each line
  • docker container rm <container-id> - docker container removal
avivamg
  • 12,197
  • 3
  • 67
  • 61