2025-03-27 19:06:04 +00:00
|
|
|
---
|
|
|
|
tags: [docker, procedural]
|
|
|
|
created: Thursday, March 27, 2025
|
|
|
|
---
|
|
|
|
|
|
|
|
# Docker cleanup
|
|
|
|
|
2025-08-13 16:52:42 +01:00
|
|
|
## Identify _all_ running containers
|
|
|
|
|
|
|
|
```sh
|
|
|
|
docker ps -a
|
|
|
|
```
|
|
|
|
|
2025-04-05 10:32:40 +01:00
|
|
|
## Generic clean-up: free as many bytes as possible
|
|
|
|
|
|
|
|
```sh
|
|
|
|
sudo docker system prune -a
|
|
|
|
```
|
|
|
|
|
2025-03-27 19:06:04 +00:00
|
|
|
## Check Docker storage usage
|
|
|
|
|
|
|
|
```sh
|
|
|
|
sudo docker system df
|
|
|
|
```
|
|
|
|
|
|
|
|
## Unused volumes
|
|
|
|
|
|
|
|
These are the biggest culprit when taking up server space.
|
|
|
|
|
|
|
|
Use this command to clear them out:
|
|
|
|
|
|
|
|
```sh
|
|
|
|
sudo docker volume rm $(sudo docker volume ls -q -f dangling=true)
|
|
|
|
```
|
|
|
|
|
|
|
|
These are typically UUIDs and named anonymously which makes it hard to know what
|
|
|
|
you are deleting.
|
|
|
|
|
|
|
|
To avoid this, the best practice is to name the volumes in the Dockerfile and
|
|
|
|
also label them to make it easier to tell what's doing what:
|
|
|
|
|
|
|
|
```yml
|
|
|
|
volumes:
|
|
|
|
nextcloud_data:
|
|
|
|
name: nextcloud_data
|
|
|
|
labels:
|
|
|
|
app: nextcloud
|
|
|
|
purpose: user_data
|
|
|
|
|
|
|
|
services:
|
|
|
|
nextcloud:
|
|
|
|
volumes:
|
|
|
|
- nextcloud_data:/var/www/html
|
|
|
|
```
|
|
|
|
|
|
|
|
### Locations to check (even after deleting volume)
|
|
|
|
|
|
|
|
```
|
|
|
|
/var/lib/docker/image
|
|
|
|
/var/lib/docker/containers
|
|
|
|
/var/lib/docker/volumes
|
|
|
|
/var/lib/docker/buildkit # build cache
|
|
|
|
```
|
|
|
|
|
|
|
|
## Unused images
|
|
|
|
|
|
|
|
### Identify
|
|
|
|
|
|
|
|
```sh
|
2025-05-04 18:01:46 +01:00
|
|
|
sudo docker images --filter "dangling=true"
|
2025-03-27 19:06:04 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
### Delete
|
|
|
|
|
|
|
|
```sh
|
|
|
|
|
|
|
|
sudo docker rmi mariadb:latest
|
|
|
|
```
|
2025-08-13 16:52:42 +01:00
|
|
|
|
|
|
|
## Delete containers
|
|
|
|
|
|
|
|
```sh
|
|
|
|
docker container rm <uuid>
|
|
|
|
```
|