Long-running Docker containers can rapidly accumulate a large number of log lines. These consume storage capacity and reduce performance when accessing and filtering the data. While Docker includes integrated tools for viewing the logs, there’s no built-in mechanism for cleaning them up.
In this article you’ll learn how to clear up the logs of running Docker containers, without restarting or replacing them. You’ll also see some techniques for more efficiently handling large logs while retaining old data.
Understanding the Problem
Docker collects logs from the standard output and error streams of container foreground processes. The docker logs
command is used to retrieve these logs but it doesn’t let you delete old lines.
Docker supports many different logging drivers so it’s not possible to offer a standardized clean-up mechanism. This article focuses on the standard json-file
logging driver, where log lines are stored in a JSON file on your container host’s filesystem. Refer to your storage driver’s documentation if you need to clean up logs streamed to a remote host.
Log files created by the json-file
driver are stored under the /var/lib/docker/containers
directory. Each container gets its own log file which is used throughout its lifetime. There’s no log rotation configured by default.
Simply deleting the log file isn’t an effective solution. Docker expects the file to be available continually and won’t recreate it automatically if it’s removed. It’s safer to clear the existing log’s contents to avoid affecting any current writes.
Discovering the Log File’s Path
First find the path to your target container’s log file. You can retrieve the log file path for a container called my-app
by running the following command:
$ docker inspect --format='{{.LogPath}}' my-app /var/lib/containers/1380d936...-json.log
Clearing the Log File
You can clear out the contents of the log without deleting it by echoing an empty string into its content. The file will be owned by root
so you’ll need to perform this operation in a root shell. The following command will empty the log file for you:
$ sudo sh -c 'echo "" > $(docker inspect --format="{{.LogPath}}" my-app)'
Shell interpolation is being used to dynamically retrieve the log file path for the my-app
container. You could manually substitute in the path retrieved earlier instead.
When you run docker logs my-app
, you’ll now see empty output unless the container has resumed writing lines in the interim.
Limiting Log Output
The docker logs
command accepts a few arguments that can be used to limit the output from noisy logs. These help mitiga