How to clean Docker container logs?

Docker

Docker Problem Overview


I read my Docker container log output using

docker logs -f <container_name>

I log lots of data to the log in my node.js app via calls to console.log(). I need to clean the log, because it's gotten too long and the docker logs command first runs through the existing lines of the log before getting to the end. How do I clean it to make it short again? I'd like to see a command like:

docker logs clean <container_name>

But it doesn't seem to exist.

Docker Solutions


Solution 1 - Docker

First, if you just need to see less output, you can have docker only show you the more recent lines:

docker logs --since 30s -f <container_name_or_id>

Or you can put a number of lines to limit:

docker logs --tail 20 -f <container_name_or_id>

To delete the logs on a Docker for Linux install, you can run the following for a single container:

echo "" > $(docker inspect --format='{{.LogPath}}' <container_name_or_id>)

Note that this requires root, and I do not recommend this. You could potentially corrupt the logfile if you null the file in the middle of docker writing a log to the same file. Instead you should configure docker to rotate the logs.


Lastly, you can configure docker to automatically rotate logs with the following in an /etc/docker/daemon.json file:

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

That allows docker to keep up to 3 log files per container, with each file limited to 10 megs (so a limit between 20 and 30 megs of logs per container). You will need to run a systemctl reload docker to apply those changes. And these changes are the defaults for any newly created container, they do not apply to already created containers. You will need to remove and recreate any existing containers to have these settings apply.

Solution 2 - Docker

The best script I found is

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

It cleans all logs and you don't need to stop the containers.

Credit goes to https://bytefreaks.net/applications/docker/horrible-solution-how-to-delete-all-docker-logs

Solution 3 - Docker

If you want to remove all log files, not only for a specific container's log, you can use:

docker system prune

But, note that this does not clear logs for running containers.

Solution 4 - Docker

This is not the ideal solution, but until Docker builds in a command to do it, this is a good workaround.

Create a script file docker-clean-logs.sh with this content:

#!/bin/bash

rm $(docker inspect $1 | grep -G '"LogPath": "*"' | sed -e 's/.*"LogPath": "//g' | sed -e 's/",//g');

Grant the execute permission to it:

chmod +x ./docker-clean-logs.sh

Stop the Docker container that you want to clean:

docker stop <container_name>

Then run the above script:

./docker-clean-logs.sh <container_name>

And finally run your container again:

docker start ...

Credit goes to the user sgarbesi on this page: https://github.com/docker/compose/issues/1083

Solution 5 - Docker

You can use logrotate as explained in this article

https://sandro-keil.de/blog/2015/03/11/logrotate-for-docker-container/

This needs to be done before launching the container.

Solution 6 - Docker

In order to do this on OSX, you need to get to the virtual machine the Docker containers are running in.

You can use the walkerlee/nsenter image to run commands inside the VM like so:

docker run --rm -it --privileged --pid=host walkerlee/nsenter -t 1 -m -u -i -n sh

Combining that with a simplified version of the accepted answer you get:

#!/bin/sh
docker run --rm -it --privileged --pid=host walkerlee/nsenter -t 1 -m -u -i -n \
    cp /dev/null $(docker inspect -f '{{.LogPath}}' $1)

Save it, chmod +x it, run it.

As far as I can tell this doesn't require the container to be stopped. Also, it clears out the log file (instead of deleting it) avoiding errors when doing docker logs right after cleanup.

Solution 7 - Docker

Solution for a docker swarm service:

   logging:
     options:
       max-size: "10m"
       max-file: "10"

Solution 8 - Docker

Run:

 docker inspect {containerId}

Copy LogPath value

truncate -s 0 {LogaPath}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpkoutView Question on Stackoverflow
Solution 1 - DockerBMitchView Answer on Stackoverflow
Solution 2 - DockermaxisamView Answer on Stackoverflow
Solution 3 - DockerAtsushi SakaiView Answer on Stackoverflow
Solution 4 - DockerpkoutView Answer on Stackoverflow
Solution 5 - Dockeruser2915097View Answer on Stackoverflow
Solution 6 - DockerelifinerView Answer on Stackoverflow
Solution 7 - DockerAhmet Vehbi OlgaçView Answer on Stackoverflow
Solution 8 - DockerIgor CherepovView Answer on Stackoverflow