Delete unnecessary containers of Docker at once

Recently, I've been studying with Docker on AWS EC2 (Amazon Linux2) and playing around with it. Create a container for centos → Go to the end, create a container for mysql → Go to the end → Repeat the process, and occasionally check the operating status of the container with the docker ps command, but at one point docker When I ran ps -a ** What the hell! !! **have become. So, "Don't hate Docker when the Docker container is over!" ◯ I'm not sure if Atsuko Ta said it, but at least I learned that ** the Docker container is over but the Docker container remains **.

The useless introduction has become long, but to delete the garbage Docker container that has accumulated in this pool A memorandum of command.

Deleting unnecessary containers using awk

State before deletion

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                    PORTS                    NAMES
9ec50bb2ac79        mysql               "docker-entrypoint.s…"   4 days ago          Up 2 days                 3306/tcp, 33060/tcp      MYSQL
20cabc9db638        centos              "/bin/bash"              4 days ago          Up 2 days                 0.0.0.0:9989->8080/tcp   centos_container
149e47fc7182        mysql               "docker-entrypoint.s…"   4 days ago          Exited (0) 4 days ago                              silly_haibt
6d0b42666db1        mysql               "docker-entrypoint.s…"   4 days ago          Exited (0) 4 days ago                              stoic_panini
cc75432e283b        mysql               "docker-entrypoint.s…"   4 days ago          Exited (127) 4 days ago                            serene_merkle
e75ea014088a        mysql               "docker-entrypoint.s…"   4 days ago          Exited (0) 4 days ago                              laughing_allen
7f6d0144717d        centos              "/bin/bash"              4 days ago          Exited (0) 4 days ago                              zealous_haibt
b78d2f0c9b93        centos              "/bin/bash"              4 days ago          Exited (0) 4 days ago                              centos_1
a1adb31a4719        mysql               "docker-entrypoint.s…"   4 days ago          Exited (0) 4 days ago                              mysql_container
b977c75c9dd7        mysql               "docker-entrypoint.s…"   4 days ago          Exited (1) 4 days ago                              bold_nobel
6c590752cab8        mysql               "docker-entrypoint.s…"   4 days ago          Exited (1) 4 days ago                              gifted_nightingale

Of the above, delete all but the top two in operation.

docker ps -f status=exited | awk '{print $1}' | xargs docker rm

Execution result

149e47fc7182
6d0b42666db1
cc75432e283b
e75ea014088a
7f6d0144717d
b78d2f0c9b93
a1adb31a4719
b977c75c9dd7
6c590752cab8
Error: No such container: CONTAINER
[ ~]$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
9ec50bb2ac79        mysql               "docker-entrypoint.s…"   4 days ago          Up 2 days           3306/tcp, 33060/tcp      MYSQL
20cabc9db638        centos              "/bin/bash"              4 days ago          Up 2 days           0.0.0.0:9989->8080/tcp   centos_container

The unnecessary container disappeared safely, but I got an error when trying to docker rm with" CONTAINER "in the header line. .. (Well, Ikka)

Docker official document method (-_-) * We recommend this!

After creating the above command, the Docker official manual describes how to delete a container that is not running. .. You might be told to put it before the awk method, which doesn't have the above ...

Pre-execution state (I increased the production of unnecessary containers again)

docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                    NAMES
109839ada027        centos              "/bin/bash"              35 seconds ago      Exited (0) 34 seconds ago                            vibrant_bohr
cf2e8c41b2ee        centos              "/bin/bash"              36 seconds ago      Exited (0) 36 seconds ago                            brave_mestorf
fcfdaf35235d        centos              "/bin/bash"              37 seconds ago      Exited (0) 37 seconds ago                            gallant_faraday
bf7b9fa96291        centos              "/bin/bash"              38 seconds ago      Exited (0) 38 seconds ago                            zealous_morse
9ec50bb2ac79        mysql               "docker-entrypoint.s…"   4 days ago          Up 2 days                   3306/tcp, 33060/tcp      MYSQL
20cabc9db638        centos              "/bin/bash"              4 days ago          Up 2 days                   0.0.0.0:9989->8080/tcp   centos_container

Delete command

Command explanation (from official manual)

This command deletes all stopped containers. The command docker ps -a -q passes all finished container IDs to the rm command and deletes them all. The running container is not deleted.

docker rm $(docker ps -a -q)

Execution result

109839ada027
cf2e8c41b2ee
fcfdaf35235d
bf7b9fa96291
Error response from daemon: You cannot remove a running container 9ec50bb2ac797f3cfdbe827aa2793ba135f6f29e9fd2c1b179afa09d6be4b5ba. Stop the container before attempting removal or force remove
Error response from daemon: You cannot remove a running container 20cabc9db6380304c3c0c3b8a14f04e0bd60fd58c810ee4c02d6539fd5b4f3f3. Stop the container before attempting removal or force remove

Attempting to delete a running container will result in an error. I haven't tried it, but if you do docker rm -f $ (docker ps -a -q), you'll be sentenced to kill all the containers! And, as most of you have read so far, you may have noticed that the above awk method was used. The contents can also be realized with the following command ...

docker rm $(docker ps -f status=exited -q)

Well then, it seems that you are all right. .. .. Ten

Recommended Posts

Delete unnecessary containers of Docker at once
Get out of multiple loops at once
Get a lot of Twitter tweets at once
python at docker
Delete all Docker Volumes