How to clean Docker container logs?

2019-03-25 09:01发布

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
5条回答
地球回转人心会变
2楼-- · 2019-03-25 09:07

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.

查看更多
再贱就再见
3楼-- · 2019-03-25 09:11

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.

查看更多
倾城 Initia
4楼-- · 2019-03-25 09:17

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.

查看更多
戒情不戒烟
5楼-- · 2019-03-25 09:29

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

查看更多
干净又极端
6楼-- · 2019-03-25 09:32

Solution for a docker swarm service:

   logging:
     options:
       max-size: "10m"
       max-file: "10"
查看更多
登录 后发表回答