It's almost the end of the year, so it's a story of cleaning up to use Docker comfortably at the beginning of the year

This article is the 25th day article of "Yurutto Advent Calendar 2020"

at first

2020 is almost over. I want to spend the end of the year and the beginning of the year comfortably. I aim to clean Docker, which is (probably) the most frequently used in my daily work, and lead a Docker life that feels good at the beginning of the year.

--Container --Image --Network --Volume --Cache

Delete all containers

First, delete all containers by following the steps below.

  1. Check the container list
  2. Stop all containers
  3. Delete all containers

1. Check the container list

If you want to see the list of containers, you can see it with docker ps -a


$ docker ps -a
CONTAINER ID        IMAGE                                                                       COMMAND                  CREATED             STATUS                     PORTS                    NAMES
8e628c6d89be        lab-test                                                                    "bash"                   10 days ago         Exited (0) 10 days ago                              beautiful_hermann
79832a249067        lab-test                                                                    "bash"                   10 days ago         Exited (1) 10 days ago                              intelligent_kirch
db62543bae14        7f4591b869f3                                                                "bash"                   11 days ago         Exited (0) 11 days ago                              wonderful_nash
dc7eab95300d        7f4591b869f3                                                                "bash"                   11 days ago         Exited (0) 11 days ago                              kind_proskuriakova
68691ef5b776        681b31775932                                                                "bash"                   11 days ago         Exited (1) 11 days ago                              adoring_snyder
386bbaa3699a        681b31775932                                                                "bash"                   11 days ago         Exited (129) 11 days ago                            vigorous_margulis
.
.
.

By the way

If you do docekr ps -aq, only the container ID will be output.

$ docekr ps -aq
f26dd4661c42
8e628c6d89be
79832a249067
79d9e52e5e76
.
.
.

2. Stop all containers

Now that we can see all the containers, then stop all the containers once The command is docker stop $ (docker ps -aq)

$ docker stop $(docker ps -aq)

Next, enter the following command and check if there is a container running

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

3. Delete all containers

Since we were able to stop all the containers, the next step is to delete all the containers with the following command.

$ docker rm $(docker ps -aq)

Check if it could be deleted. You have now closed all containers, whether they are running or not.

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

Delete all images

Since we were able to delete all the containers earlier, the next step is to delete the image.

  1. Check the image list
  2. Delete all images

1. Check the image list

First, check how many images you have. By the way, I recently learned the meaning of the -a option of docker images.

It seems to display an intermediate image. I knew it for the first time after reading the contents of docker images -h.

$ docker images -aq
056a32e64258
bfdf7441bdfc
24136c61804b
.
.
.

2. Delete all images

Since I was able to confirm the list of images, delete them all with the following command. You can force the image to be deleted by adding the -f option.

$ docker rmi -f $(docker images -aq)

After deleting, check the image list just in case.

$ docker images -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

This completes the image deletion.

Delete the network

Follow the steps below to delete the network created by docker.

  1. Check the network list
  2. Delete network

1. Check the network list

You can check the list of networks with the following command. By the way, bridge, host, none are the networks that exist by default.

$ docker network ls 
NETWORK ID          NAME                DRIVER              SCOPE
20e1d4898d67        bridge              bridge              local
2a419890b777        host                host                local
a275a9c25a0c        influxdb_default    bridge              local
6521ba142a08        labs_default        bridge              local
ab08971ace6d        mongo_default       bridge              local
4087108a7f37        none                null                local

2. Delete network

Previously, the list of IDs was passed to the delete command and deleted, but for network and volume, the prune option is used. There is also a rm option, but I haven't tried it because I don't understand the following two points. Should I try it next time?

--Can you pass the ID? --I don't know what the default network will be

$ docker network prune -f
Deleted Networks:
mongo_default
labs_default
influxdb_default

You have now deleted any networks other than the one created by default. I will check it just in case. It seems to be okay because all but the default has disappeared.

$ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
20e1d4898d67        bridge              bridge              local
2a419890b777        host                host                local
4087108a7f37        none                null                local

Delete volume

Delete the volume created by docker in the same way as the network.

  1. Check the volume list
  2. Delete volume

1. Check the volume list

You can check the list of volumes with the following command.

$ docker volume ls
DRIVER              VOLUME NAME
local               1a2ff7bbf277fb6f134ef03f63604cf9d79a7bb3c13dd76ee5ee43370979266a
local               4b4ffeb6c0ea586148eb1e140c4a2bb9c8b062ccccd3c1916c0c8b1fe7362c9f
local               4d53cbc45378083f9ebbf798a90442b6d1cf0778ea28a24328dcfda0682dadb4
local               6cd26eb1a2f0f175b13d3cfdb0106b8dc88fdf8a7ce08ca5c6209f22e21e3f28
.
.
.

2. Delete volume

Delete the volume with the following command

Total reclaimed space: 2.991GB

By the way, I have about 2.991GB free in my environment. I'm going to do it often, but it will soon accumulate

docker volume prune -f
Deleted Volumes:
local               1a2ff7bbf277fb6f134ef03f63604cf9d79a7bb3c13dd76ee5ee43370979266a
local               4b4ffeb6c0ea586148eb1e140c4a2bb9c8b062ccccd3c1916c0c8b1fe7362c9f
local               4d53cbc45378083f9ebbf798a90442b6d1cf0778ea28a24328dcfda0682dadb4
local               6cd26eb1a2f0f175b13d3cfdb0106b8dc88fdf8a7ce08ca5c6209f22e21e3f28
.
.
.
Total reclaimed space: 2.991GB

Check it just in case.

$ docker volume ls
DRIVER              VOLUME NAM

You have now deleted all the volumes.

Delete cache

Delete the cache created when docker build is done. (Actually, I have little knowledge about this cache) It seems that there was no cache in my environment, so it is 0B

$ docker builder prune -f
Total reclaimed space: 0B

Check if it has been deleted

Make sure it's cleaned last (I should have entered this command first, but it's a later festival)

$ docker system df 
TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
Images              0                   0                   0B                  0B
Containers          0                   0                   0B                  0B
Local Volumes       0                   0                   0B                  0B
Build Cache         0                   0                   0B                  0B

$ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
20e1d4898d67        bridge              bridge              local
2a419890b777        host                host                local
4087108a7f37        none                null                local

Finally, impressions

This time, I cleaned up Docker. With this, I think you can start using it comfortably at the beginning of the year. I think the help for Doker commands is pretty straightforward. If you have any problems, you may want to try -h for the time being.

I will investigate at another time whether it is okay to delete the network created by Docker by default.

reference

Recommended Posts

It's almost the end of the year, so it's a story of cleaning up to use Docker comfortably at the beginning of the year
A solution to the problem of blank spaces at the beginning of textarea
Up to the point of launching a Docker container built using RedHat Quarkus
The story of pushing a Docker container to GitHub Package Registry and Docker Hub with GitHub Actions
The story of migrating a stray batch without an owner from EC2 to a Docker environment
[Docker] Is it good enough to call it a multi-stage build? → The story that became so good
Why put a line break at the end of the file
docker (200) Misra Example Suite at docker The road to completion of compilation
[Java beginner] I got a little deeper understanding of "It's time to use new", so make a note
The story of forgetting to close a file in Java and failing
How to change the value of a variable at a breakpoint in intelliJ