Common Methods and Problems of Docker Cleaning
- 2021-10-13 09:09:22
- OfStack
If you use docker for large-scale development, but have no cleanup strategy, your disk will be filled up immediately, and when you really have to deliver something immediately because of the popularity of the product, you can't deliver it.
When we run a process in our computer, everything will be destroyed once the process is completed. Containers are the infrastructure that many of us now operate. 1 Cut everything runs on 1 container, aiming at 1 process per container. When the process completes, the container exits. But it won't clean itself up.
What Docker accumulates
You need to pay attention to these
Containers that have been stopped
Disk volume
Mirror image
Network
If you have enough space, you may not care much about disk space, but the network is also important. By default, Docker uses the bridge network, and its limit is 31 networks. When you reach the limit, you will see the following message:
could not find an available, non-overlapping IPv4 address pool among the defaults to assign to the network
This can happen if you are a heavy user of docker-compose who creates one network for each project. You can customize by setting 1
--subnet
Subnet to solve problems, such as:
docker network create dada --subnet 192.167.11.0/24
However, the focus of this article is cleaning up.
Cleanup with docker
Cleaning up Stopped Containers
docker rm -v $(docker ps --all --quiet --filter 'status=exited')
This will find all containers in the exited (exited) state and output their ID one by one, so that we can provide it to other shell instructions.
We use
docker rm -v
To delete any anonymous volumes (volumes without explicit names).
The above command should delete the volume associated with the container. If you create volumes manually and want to delete any unused volumes:
docker volume rm $(docker volume ls --quiet --filter 'dangling=true')
Clean up the image
It is usually safe to delete all Docker images. We can get it on demand when we need it. Typically, after 1 image is cleaned up, the build time is longer because the docker daemon takes time to download the image again
docker rm --force $(docker images --quiet)
Here, we use
--force
To force the deletion of the image, even if 1 container is using that image. We can get this image later.
It's very simple. We can delete any network, and it will be rebuilt as needed later.
docker network rm $(docker network ls --quiet)
Cleanup with docker-compose
If you use the docker-compose startup container, we have a simple way to clean up the resources associated with a specific compose file.
docker-compose down --volumes --rmi all --remove-orphans
Unfortunately, this command will not delete anonymous volumes, so you must deal with these anonymous volumes.
1 command to solve all problems
docker is short-lived, we can always re-capture the image, re-create our database for development, or this is just a continuous integration system, we can delete 1 cut.
docker system prune --all --force --volumes