问题
I use docker logs [container-name]
to see the logs of a specific container.
Is there an elegant way to clear these logs?
回答1:
From this question there's a one-liner that you can run:
echo "" > $(docker inspect --format='{{.LogPath}}' <container_name_or_id>)
I'm not a big fan of modifying Docker's files directly with this, a truncate command, or log rotate, since each are external to docker. The external log deletion could happen while docker is writing json formatted data to the file, resulting in a partial line, and breaking the ability to read any logs from the docker logs
cli.
Instead, you can have Docker automatically rotate the logs for you. This is done with additional flags to dockerd if you are using the default JSON logging driver:
dockerd ... --log-opt max-size=10m --log-opt max-file=3
You can also set this as part of your daemon.json file instead of modifying your startup scripts:
{
"log-driver": "json-file",
"log-opts": {"max-size": "10m", "max-file": "3"}
}
Make sure to run a systemctl reload docker
after changing this file to have the settings applied. This setting will then be the default for any newly created containers. Existing containers need to be deleted and recreated to receive the new log limits. And similar log options can be passed to individual containers to override these defaults, allowing you to save more or fewer logs on individual containers.
回答2:
Use:
truncate -s 0 /var/lib/docker/containers/*/*-json.log
You may need sudo
sudo sh -c "truncate -s 0 /var/lib/docker/containers/*/*-json.log"
ref. Jeff S. How to clear the logs properly for a Docker container?
Reference: Truncating a file while it's being used (Linux)
回答3:
sudo sh -c "truncate -s 0 /var/lib/docker/containers/*/*-json.log"
回答4:
You can set up logrotate to clear the logs periodically.
Example file in /etc/logrotate.d/docker-logs
/var/lib/docker/containers/*/*.log {
rotate 7
daily
compress
size=50M
missingok
delaycompress
copytruncate
}
回答5:
On Docker for Windows and Mac, and probably others too, it is possible to use the tail option. For example:
docker logs -f --tail 100
This way, only the last 100 lines are shown, and you don't have first to scroll through 1M lines...
(And thus, deleting the log is probably unnecessary)
回答6:
Docker4Mac, a 2018 solution:
LOGPATH=$(docker inspect --format='{{.LogPath}}' <container_name_or_id>)
docker run -it --rm --privileged --pid=host alpine:latest nsenter -t 1 -m -u -n -i -- truncate -s0 $LOGPATH
The first line gets the log file path, similar to the accepted answer.
The second line uses nsenter
that allows you to run commands in the xhyve
VM that servers as the host for all the docker containers under Docker4Mac. The command we run is the familiar truncate -s0 $LOGPATH
from non-Mac answers.
If you're using docker-compose
, the first line becomes:
local LOGPATH=$(docker inspect --format='{{.LogPath}}' $(docker-compose ps -q <service>))
and <service>
is the service name from your docker-compose.yml
file.
Thanks to https://github.com/justincormack/nsenter1 for the nsenter
trick.
回答7:
You can't do this directly through a Docker command.
You can either limit the log's size, or use a script to delete logs related to a container. You can find scripts examples here (read from the bottom): Feature: Ability to clear log history #1083
Check out the logging section of the docker-compose file reference, where you can specify options (such as log rotation and log size limit) for some logging drivers.
回答8:
As a root user, try to run the following:
> /var/lib/docker/containers/*/*-json.log
or
cat /dev/null > /var/lib/docker/containers/*/*-json.log
or
echo "" > /var/lib/docker/containers/*/*-json.log
回答9:
On my Ubuntu servers even as sudo I would get Cannot open ‘/var/lib/docker/containers/*/*-json.log’ for writing: No such file or directory
But combing the docker inspect and truncate answers worked :
sudo truncate -s 0 `docker inspect --format='{{.LogPath}}' <container>`
回答10:
I do prefer this one (from solutions above):
truncate -s 0 /var/lib/docker/containers/*/*-json.log
However I'm running several systems (Ubuntu 18.x Bionic for example), where this path does not work as expected. Docker is installed through Snap, so the path to containers is more like:
truncate -s 0 /var/snap/docker/common/var-lib-docker/containers/*/*-json.log
回答11:
Docker for Mac users, here is the solution:
Find log file path by:
$ docker inspect | grep log
SSH into the docker machine( suppose the name is
default
, if not, rundocker-machine ls
to find out):$ docker-machine ssh default
Change to root user(reference):
$ sudo -i
Delete the log file content:
$ echo "" > log_file_path_from_step1
来源:https://stackoverflow.com/questions/42510002/how-to-clear-the-logs-properly-for-a-docker-container