As of today Docker is the most popular tool for running and managing containers. However, if you don’t manage the logs generated by Docker containers, they can grow large and take up too much disk space. Automatic log rotation is the solution to keep log files under control. This tutorial will help you to setup automatic log rotation in different scenarios using simple, clear instructions.
What Is Log Rotation?
Log rotation means automatically limiting the size and number of log files generated by the Docker containers. Instead of one large log file, it splits logs into smaller chunks or keeps only the recent ones, deleting older ones when necessary.
1. Log Rotation for a Single Container
You can set up log rotation when running a specific container. Use the folloiwng command to run the container with log options:
docker run
--log-driver json-file
--log-opt max-size=100m
--log-opt max-file=3
my-container
Explanation:
max-size=100m
: Each log file will be limited to 100 MB.max-file=3
: Only three log files will be kept. Old files will be deleted.
2. Log Rotation in Docker-Compose
When managing multiple containers with Docker Compose, you can set log rotation options directly in the docker-compose.yml
file.
-
Edit the
docker-compose.yml
:services: app: image: my-container logging: driver: "json-file" options: max-size: "10m" max-file: "3"
-
Start your services:
docker-compose up
3. Global Log Rotation Settings
You can also configuration log rotation globally. So the configuration will automatically apply to all container running on your system. To apply log rotation to all containers on a Docker host, configure the global settings in Docker’s daemon.json
file.
-
Find the
daemon.json
file: The file is usually located at/etc/docker/daemon.json
. If it doesn’t exist, create it. -
Edit the file to add log options:
{ "log-driver": "json-file", "log-opts": { "max-size": "10m", "max-file": "3" } }
-
Restart the Docker daemon:
sudo systemctl restart docker
Conclusion
As a System Administrator or Devops, we knows the importance of free disk space on production servers. So taking care of Docker logs is very important part to keep your system running smoothly. By setting up log rotation, you can stop logs from becoming too big and taking up too much space. You can follow these simple steps for single containers, Docker Compose, or adjust it in global docker configuration file . With log rotation in place, your system will stay clean and work better without running into space problems. You should start using these tips today to avoid trouble and keep things easy to manage!
The post How to Automatically Rotate Docker Logs appeared first on TecAdmin.
Source: Read More