Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to prevent out-of-disk space when using Docker?

In this article, we will learn how to prevent Docker from eating up all your disk space. We often encounter this problem when configuring the server to run with Docker.

Remove TAG images

One of the things that take up the most disk space is Docker Image, but what makes the disk out of space is not the image we use to run the program, the main cause comes from the image with  tag.

Let’s check on your server:

docker images
REPOSITORY                             TAG       IMAGE ID       CREATED         SIZE
postgres 5861c038d674 14 months ago 371MB
k8s.gcr.io/ingress-nginx/controller bf621a764db5 17 months ago 278MB
k8s.gcr.io/ingress-nginx/controller 81d7cdfa4169 2 years ago 280MB

These images with tag is dangling images that are generated during the process of building the image from the Dockerfile. You can remove the image with tag to reduce disk space.

Run the following command to list all tag images:

docker images -f "dangling=true"

Get IMAGE ID:

docker images -f "dangling=true" -q

Run the remove command:

docker rmi $(docker images -f "dangling=true" -q)

For docker 1.13+:

docker imbge prune

We should configure crontab to run once a day to clear all dangling images, for example with Linux:

sudo -s
cat > /etc/cron.daily/clear-tag-none
#!/bin/sh
docker rmi $(docker images -f "dangling=true" -q)
EOF
chmod +x /etc/cron.daily/clear-tag-none

Delete Docker Container Log Files

The next thing that takes up disk space is Docker Container Log Files. By default, Docker captures the standard output (and standard error) of all your containers and writes them in files using the JSON format. If you don’t frequently delete log files, it will go up to a dozen or a hundred GB.

To delete Docker Container Log Files, there are several ways:

  • Manual with crontab
  • Using logrotate
  • Build-in logrotate functionality

Manual deletion with crontab

You can use the following command to delete logs for the logs that are not important:

cat /dev/null > /var/lib/docker/containers/*/*-json.log

Configure crontab:

sudo -s
cat > /etc/cron.daily/clear-container-logs
#!/bin/sh
cat /dev/null > /var/lib/docker/containers/*/*-json.log
EOF
chmod +x /etc/cron.daily/clear-container-logs

Using logrotate

If you want to keep the logs, you can use logrotate to reduce the size of log files. To create a new logrotate config file for your Docker containers simply create a new file in the logrotate folder /etc/logrotate.d/logrotate-container and put the following configuration to it:

/var/lib/docker/containers/*/*.log {
rotate 7
daily
compress
missingok
delaycompress
copytruncate
}

you can test it with the following command:

logrotate -fv /etc/logrotate.d/logrotate-container

By default, the installation of logrotate creates a crontab file inside /etc/cron.daily named logrotate, so we don’t need to configure crontab manually.

Build-in logrotate functionality

For Docker 1.8+, you can use the built-in logrotate functionality of Docker. For example, you can set the logging driver and enable automatic log rotation for a specific container by using the --log-driver and --log-opts flags:

docker run - log-driver json-file - log-opt max-size=10m nginxb

If we need to configure the entire container, set the log-driver and log-opts keys to appropriate values in the daemon.json file, which is located in /etc/docker/ on Linux hosts or C:\ProgramData\docker\config\ on Windows Server.

{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}

Conclusion

The above are some ways to prevent servers run out of disk space when using Docker. I hope it is useful to you.

👋 If you find this helpful, please click the clap 👏 button below a few times to show your support for the author 👇

🚀Join FAUN Developer Community & Get Similar Stories in your Inbox Each Week


How to prevent out-of-disk space when using Docker? was originally published in FAUN Publication on Medium, where people are continuing the conversation by highlighting and responding to this story.

Share the post

How to prevent out-of-disk space when using Docker?

×

Subscribe to Top Digital Transformation Strategies For Business Development: How To Effectively Grow Your Business In The Digital Age

Get updates delivered right to your inbox!

Thank you for your subscription

×