0

I want to automatically clear Docker container logs based on a specific timestamp, such as removing logs older than 30 minutes. However, I am having trouble configuring the compose file to achieve this. Currently, I am only able to control the log file size using the max-size: "" attribute, limiting it to 4KB. Can I accomplish this through a Docker Compose file? If not, how can I achieve this goal? Here's the Docker Compose file I've been using:

version: "3"
services:
  azure-function-app:
    build:
      context: .
      dockerfile: FunctionApp01/Dockerfile
    image: functionapp-nolimit
    container_name: function_app-nolimit
    restart: unless-stopped
    tty: true
    ports:
      - "8081:80"
    logging:
      options:
        max-size: "4k" 

I attempted to clean logs based on their size limit. However, when the size limit is exceeded, all previous logs are deleted, which does not meet my objective. My goal is to delete logs that are older than a specific timestamp.

SiddheshDesai
  • 3,668
  • 1
  • 2
  • 11
  • AFAIK, Docker compose does not provide a built in way to clear all the container logs. But you can refer the answers from this SO thread- https://stackoverflow.com/questions/42510002/docker-how-to-clear-the-logs-properly-for-a-docker-container to clear your container logs. One more alternative is to use a third party tool like logrotate and include it in your compose file like below:- /var/lib/docker/containers/*/*.log { rotate 7 daily compress size=50M missingok delaycompress copytruncate } according to the answer by AlexPnt in the above SO thread – SiddheshDesai Jul 07 '23 at 08:58

1 Answers1

0

Unfortunately, Docker compose does not provide a built in way to clear all the container logs. In order to clear logs you can make use of docker cli commands or third party dependency like logrotate that automatically create, rotate and delete the log files depending upon a CRON schedule.

I am running one sample Azure Functions Docker image and I ran the below command to clear the logs referred form this Blog and this SO thread answer:-

Function app running inside a docker:-

docker ps

enter image description here

Accessing log file:-

docker inspect --format='{{.LogPath}}' ac98

enter image description here

Clearing the logs:-

Echo an empty string so the logs file will be cleared like below:-

sudo sh -c 'echo "" > $(docker inspect --format="{{.LogPath}}" ac98)'

Another option is to use a tail command and limit the logs to specific lines, This method will omit other lines from the logs.

docker logs ac98 --tail 1
docker logs ac98 --tail 100 --since 1h
docker logs ac98 --tail 100 --since 1h

enter image description here

One more alternative is to use third party dependency in your docker compose using logrotate like below:-

/path/to/your/log/file {
    daily
    rotate 7
    compress
    delaycompress
    missingok
    notifempty
    dateext
    dateformat -%Y-%m-%d-%s
    maxage 30m
}

This configuration file will erase logs older than 30 minutes, keep up to 7 rotated logs, and rotate the log file once each day. To automatically delete your Docker container logs, you can store this configuration file in a location like /etc/logrotate.d/ and execute logrotate as a cron job.

SiddheshDesai
  • 3,668
  • 1
  • 2
  • 11