clear the logs properly for a Docker container

DevOps – Truncate Docker Logs Regularly to Free Up Server Disk Storage

My services have been unavailable because there is “no space left on the device”. This has caused me to be disturbed. I was forced to use df-h/and du-h/ commands every time.

I encountered the same problem again today and resolved it. I set out to discover what was taking up my disk space.

$ history
55  du -sh /*
56  du -sh /var
57  du -sh /var/*
58  du -sh /var/lib/*
59  du -sh /var/lib/docker/*

(be sure to sudo su first if you want to use du utility)

Run below mentione code and got an interesting outcome:

du -ch /var/lib/docker/containers/*/*-json.
$ sudo sh -c "du -ch /var/lib/docker/containers/*/*-json.log"
13G    /var/lib/docker/containers/../..-json.log

Holy crap. This log file is 13 gigabytes. Google was the next company I called to learn how to clear Docker logs. There’s no official implementation. This is the fastest and easiest way.

sudo sh -c 'truncate -s 0 /var/lib/docker/containers/*/*-json.log'

Problem solved. Next, how can I prevent this from happening again? As per the linked Stackoverflow response, here’s a quick solution to my next question: Rotate your log.

Create /etc/logrotate.d/docker-logs, and add the following to the file:

/var/lib/docker/containers/*/*.log {
 rotate 7

And if everything is fine, logrotate.d will do the jobs.

Clear logs in native Docker on Mac

I need to get rid large container log files from my docker environment.

When I run native Docker on a Mac, I have trouble finding them. I’m not using the docker-machine virtual box thing. My docker version for docker is 1.13.1.

What do I do?

docker inspect <container-name>

And see here

"LogPath": "/var/lib/docker/containers/<container-id>/<container-id>-json.log
But there is not even directory /var/lib/docker on my mac (host).

I have also looked in

However, I couldn’t find any container-specific loggings.

Tail could be useful, but it’s not always convenient for me.

The question is: How can I clear the logs of my containers in my Docker Mac environment?

How to clear the logs properly for a Docker container?

This question has a one-liner you can use:

echo "" > $(docker inspect --format='.LogPath' ) 

Instead of echo, there is the simpler:

: > $(docker inspect --format='.LogPath' ) 

Or you can use the truncate command

truncate -s 0 $(docker inspect --format='.LogPath' ) 

Both of these can modify Docker files and I don’t like them. External log deletion can occur while docker writes json-formatted data to the file. This could cause a partial line and prevent you from being able to read logs from the logs.

Docker can instead rotate your logs automatically for you. If you’re using the default JSON-logging driver, this is done by adding flags to Dockerd:

dockerd... --logopt max-size=10m 

This can be set as part of your Daemon.json instead of changing your startup scripts.

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

These options must be set up with root access. To have these settings applied, make sure you run a reload docker after you’ve modified the file. This setting will be used as the default for all containers created. To receive the new log limits, containers must be deleted and rebuilt.

Similar log options can be passed to individual containers to override these defaults, allowing you to save more or fewer logs on individual containers. From docker run this looks like:

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

Or in a compose folder:

version: '3.7' services: app: image: ... logging: options: max-size: "10m" max-file: "3" 

For additional space savings, you can switch from the json log driver to the “local” log driver. It takes the same max-size and max-file options, but instead of storing in json it uses a binary syntax that is faster and smaller. This allows you to store more logs in the same sized file. The daemon.json entry for that looks like:

 "log-driver": "local", "log-opts": "max-size": "10m", "max-file": "3"  

External log forwarders and parsers that depend on direct access will not be able to use the local driver. If you use file beat to send Elastic or Splunk’s universal forever, I would avoid the “local driver”.

Leave a Reply

Your email address will not be published. Required fields are marked *