Docker containers generate logs as part of their normal operation — useful for debugging, monitoring, or auditing. However, these logs can accumulate quickly, especially in long-running containers, consuming valuable disk space.
In this post, you’ll learn how Docker logging works, why log files can grow large, and how to clear container logs safely and correctly.
🧠 Understanding Docker Logs
By default, Docker uses the json-file logging driver. This means all logs from your containers are stored in JSON format, usually located at:
/var/lib/docker/containers/<container-id>/<container-id>-json.log
Over time, these files can become huge — particularly if:
- The container is verbose in its logging
- There’s no log rotation set up
- The container runs continuously for long periods
⚠️ Important Consideration
Docker doesn’t offer a native docker logs --clear command. So, clearing logs requires manual steps or proper log rotation configuration.
Let’s go through both.
🧹 Option 1: Clear Logs by Truncating the Log File
You can truncate the log file without stopping the container:
- Find the container ID:
docker ps
- Clear the logs using
truncate:
truncate -s 0 /var/lib/docker/containers/<container-id>/<container-id>-json.log
Replace <container-id> with your actual container ID.
This method clears the log content but keeps the file and doesn’t interrupt the container.
✅ Works for Linux hosts with the default
json-filedriver
⛔ Option 2: Remove the Container and Recreate It
If truncation isn’t possible (e.g., on non-Linux systems), you can remove and recreate the container:
docker rm -f my-container
docker run --name my-container your-image
Note: This will clear all logs, but also resets the container.
⚙️ Option 3: Set Up Log Rotation
The better long-term solution is to prevent logs from growing uncontrollably by using log rotation.
Add these options when starting a container:
docker run \
--log-driver json-file \
--log-opt max-size=10m \
--log-opt max-file=3 \
your-image
max-size: Max size of a log file before it’s rotatedmax-file: Number of log files to retain
🔁 This keeps logs under control automatically.
📦 For Docker Compose
Add log rotation in your docker-compose.yml:
services:
app:
image: your-image
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"
Then run:
docker-compose up -d
✅ Best Practices
- Use log rotation on all production containers
- Monitor disk usage on
/var/lib/docker/ - Avoid aggressive logging in development containers
- Set up alerts for large log growth if using monitoring tools
🧭 Conclusion
Clearing Docker logs isn’t as simple as a single command, but it’s manageable with the right approach. Use truncation for quick fixes, and always set up log rotation to prevent future log bloat.
Pro Tip: Add truncate or log-cleaning commands to a cron job for periodic maintenance on development servers.