How to limit or rotate Docker container logs?

Hi,

I don’t seem to have any limits set on the container logs and over time run out of space.

I am running 3 containers.

CONTAINER ID   IMAGE                          
3b51b531cc4d   nuclias/nuclias_connect_web    nuclias_connect_web
417c480a20a0   nuclias/nuclias_connect_core   nuclias_connect_core
1b5885ef9127   mongo:3.6.11                   mongo

Server: Docker Engine - Community
 Engine:
  Version:          20.10.2
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       8891c58
  Built:            Mon Dec 28 16:15:19 2020
  OS/Arch:          linux/amd64
  Experimental:     false

docker-compose version 1.25.4, build 8d51620a
docker-py version: 4.1.0
CPython version: 3.7.5
OpenSSL version: OpenSSL 1.1.0l  10 Sep 2019

What are my options to limit or rotate the json logs for all of the containers ?
under /var/lib/docker/containers/…

Logging Driver: json-file

The documentation states to update daemon.json file, but i don’t have it under the directory listed below

To use the json-file driver as the default logging driver, set the log-driver and log-opts keys to appropriate values in the daemon.json file, which is located in /etc/docker/ on Linux hosts

Thank you so much!

You are on the right path. The file does not exist by default. Just create it and declare you configuration. Then make sure to restart the docker service to apply the new configuration.

Thank you meyay!

Would I need to create a daemon.json file under each container or under /etc/docker ?

Just /etc/docker/daemon.json.

Afair, It should be a general setting, applied to every container that is created after the setting was applied and the docker service restarted.

can this configuration be set to specific containers?

Hi,

Does not look like my logs are rotating. Is docker-compose using the same config file or there is a different approach to limit logs for docker compose ?

cat daemon.json
{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "250m",
    "max-file": "3"
  }
}

No, Docker Compose does not have different configuration but it does not affect already created containers.
The other problem could be that your docker does not read that configuration file. On a linux host I could change log driver options using that config file but I could not on Windows in a WSL machine.

Thank you @rimelek

I am on a ubuntu box, but container is already created.
What are my options for an existing container to limit the logs or enable rotation ?

I don’t think you can change the limit for a running container but in case of emergency you can delete the content of the log file manually. You can automate that process with a cron job or other tools if you want until you can recreate the container. You can create a backup before you delete the logs.

On linux this command can delete the content of the log file:

sudo echo "" > $(docker container inspect YOURCONTAINERNAME --format "{{ .LogPath }}")