Docker Maintenance CheatSheet and Tips

Dec 21, 2023

Related to: Docker

Tips

  • Check the size of the container folders. We should not have big containers since the images are responsible for having the OS and so on…
  • If the folders are big, it’s probably the log file. We should limit it Docker Logging
  • Run daily or weekly prune docker system prune -f. This should be in a cron job.
  • Be careful with other types of logs, too - always set a max size and a max retention period.

Disk Usage Check

The system df gives us the size of our Docker items separated by type.

df is a mention of the Linux df command

Think of disk free to remember it.

docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          80        40        24.08GB   8.086GB (33%)
Containers      77        59        794.6MB   347.1kB (0%)
Local Volumes   36        36        75.07GB   0B (0%)
Build Cache     0         0         0B        0B

The -v or --verbose gives us a lot more detailed information.

docker system df -v
Images space usage:
 
REPOSITORY                                TAG                      IMAGE ID       CREATED         SIZE      SHARED SIZE   UNIQUE SIZE   CONTAINERS
mysql                                     8                        73246731c4b0   2 days ago      618.8MB   0B            618.8MB       1
fluentd                                   latest                   b0035f156c01   2 months ago    256.3MB   206.8MB       49.53MB       1
 
Containers space usage:
 
CONTAINER ID   IMAGE                                                  COMMAND                  LOCAL VOLUMES   SIZE      CREATED             STATUS                       NAMES
d04f1c9bbea1   mysql:8                                                "docker-entrypoint.s…"   1               6B        37 minutes ago      Up 37 minutes                mysql-server
e753f696afb5   saerp-fluentd:latest                                   "tini -- /bin/entryp…"   1               0B        12 hours ago        Up 12 hours                  fluentd_fluentd.1.tg7nel4rnjwwh7om39gqoibdq
 
Local Volumes space usage:
 
VOLUME NAME                                                        LINKS     SIZE
fluentd_kibanadata                                                 1         36B
mysql-data                                                         1         16.49GB
 
Build cache usage: 0B
 
CACHE ID   CACHE TYPE   SIZE      CREATED   LAST USED   USAGE     SHARED

Besides this, you can check the good old du -h --max-depth=1 | sort -h. Just keep in mind that it can be quite slow, depending on the number of folders and files.

System Info

Use the following command to get the overall config status of your docker installation.

docker system info

A useful output is the Docker Root Dir, which shows where in your local system docker has its files.

As with most docker commands, we can get the JSON output using --format "{{ json . }}"

docker system info --format "{{ json . }}"

Clear up the Clutter

Use the system prune command to clear the “unused” stuff in your docker installation.

This will remove unused images and build cache, networks, and containers.

docker system prune -f

-f or --force is added to avoid prompt for confirmation

The best approach is adding a cron job to run the command weekly or daily.

Volume Mount Point

Getting the local folder where a docker volume is located is useful.

It’s the same result as the Docker Root Dir: on the previous command with the /volumes/volumeName added. But it’s good for a quick check.

docker volume ls 
docker volume inspect --format '{{ .Mountpoint }}' volumeName

References

Graph View