93

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.

pkout
  • 6,430
  • 2
  • 45
  • 55
  • 1
    You could try the "--follow" or the "--since" options? If your log requirements are special perhaps you should consider enabling one of the logging plugins: https://docs.docker.com/engine/admin/logging/overview/ – Mark O'Connor Dec 11 '16 at 22:18
  • Instead of cleaning logs by hand which can lead to failures, you probably just want to activate log rotation and let the docker daemon handel it automatically, see my answer: https://stackoverflow.com/questions/67046393/is-it-possible-to-clear-a-docker-container-log-file-mid-run/75251471#75251471 – Stuck Jan 26 '23 at 20:57

10 Answers10

183

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.

christian
  • 618
  • 8
  • 17
BMitch
  • 231,797
  • 42
  • 475
  • 450
  • 16
    In Ubuntu you can `du -chs /var/lib/docker/containers/*/*json.log` to know how much space your containers logs are using. – hisa_py Jun 05 '18 at 15:03
  • In Ubuntu server 19.04 you need to create the file /etc/default/docker with command: sudo echo 'DOCKER_OPTS="--config-file=/etc/docker/daemon.json"' > /etc/default/docker and edit the file daemon.json. – FChiri Jul 30 '19 at 09:59
  • Check out official documentation for configuring log rotation: https://docs.docker.com/config/containers/logging/configure/#configure-the-default-logging-driver – Gaël J Nov 29 '19 at 17:24
56

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

Richard Barber
  • 5,257
  • 2
  • 15
  • 26
maxisam
  • 21,975
  • 9
  • 75
  • 84
21

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.

Atsushi Sakai
  • 938
  • 10
  • 22
18

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

Sam Arai
  • 48
  • 5
pkout
  • 6,430
  • 2
  • 45
  • 55
5

Run:

 docker inspect {containerId}

Copy LogPath value

truncate -s 0 {LogaPath}
Igor Cherepov
  • 51
  • 1
  • 1
4

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.

user2915097
  • 30,758
  • 6
  • 57
  • 59
3

Solution for a docker swarm service:

   logging:
     options:
       max-size: "10m"
       max-file: "10"
Ahmet Vehbi Olgaç
  • 2,725
  • 1
  • 14
  • 12
2

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.

elifiner
  • 7,347
  • 8
  • 39
  • 48
0

On Windows 10 none of the solutions worked for me, I kept getting 'No such file or directory'

This worked

  • Get container ID (inspect the container)
  • In file explorer open docker-desktop-data (in WSL)
  • Navigate to version-pack-data\community\docker\containers\CONTAINER_ID
  • Stop the container
  • Open the file CONTAINER_ID-json.log file and trim it or just create a blank file with same name

enter image description here

source

Robert Sinclair
  • 4,550
  • 2
  • 44
  • 46
-1

If your logs have rotated like this:

enter image description here

Clear them with

truncate -s 0 /var/lib/docker/containers/*/*-json.log.*
JRichardsz
  • 14,356
  • 6
  • 59
  • 94