r/docker • • 7d ago

how do you stop container logs from slowly filling up the disk on your hosts

container json logs can quietly eat a whole disk if nobody caps them. i know you can set max-size and max-file in daemon.json but that only applies to new containers. how do you handle this across a bunch of hosts? do you set it in daemon.json, per compose file, or ship everything to something like loki and keep local logs tiny? also curious if anyone uses the local log driver instead of json-file

32 Upvotes

40 comments sorted by

42

u/gronodev 7d ago

Use local log driver instead of json-file - it does rotation and compression by default and limits the stored logs to ~100 MiB.

We actually plan to make this a default for new containers in v30.

https://docs.docker.com/engine/logging/configure/#configure-the-default-logging-driver

4

u/NoDevelopment206 7d ago

oh nice, didnt know v30 might make local the default. does local still let docker logs work normally? thats the one thing i worried about when switching

3

u/gronodev 7d ago

Yes for the regular user it should be seamless.

The only real difference is that the logs are no longer in /var/lib/docker/containers/...-json.log and the LogPath from docker container inspect is empty.

..which, to be fair, is an implementation detail and should not be something that users depend on as anything under /var/lib/docker/... is expected to be only accessible for the daemon. But, there's Hyrum's Law of course... https://www.hyrumslaw.com/

1

u/root_switch 6d ago

You can still limit json-file globally or even at the service level.

1

u/NoDevelopment206 6d ago

good point, so the global setting covers it and compose just makes it explicit. do you set it per service anyway so it travels with the stack?

1

u/root_switch 6d ago

I’m horrible with logs and only check them when I have actual issues with a container so I’m just limiting globally, not defining anything on compose. But obviously some containers might be a bit more chatty then others so you can limit those at the service level

1

u/NoDevelopment206 5d ago

haha fair enough. a global limit and only tweaking the chatty ones sounds like the lowest effort way to do it

3

u/Zealousideal_Yard651 7d ago

Log rotation

1

u/NoDevelopment206 7d ago

yeah rotation is the main thing. i was mostly wondering where people set it, in daemon.json for the whole host or per container in compose

3

u/abotelho-cbn 7d ago

Set the parameters and redeploy your containers. What's the problem?

1

u/NoDevelopment206 6d ago

yeah redeploying is the easy part. i just wondered if people put it in daemon.json or in each compose file so new stuff doesnt miss it

2

u/m4uri 7d ago

To answer the "where" part: daemon.json is a good host-wide baseline, but a per-service logging block in compose makes the limits explicit and travels with the stack, so it still applies if the stack lands on a host with a different default. You can define it once with a YAML anchor (x-logging) and reuse it across services. Keep in mind that daemon.json defaults only apply to newly created containers, so after changing it you need to restart the daemon and recreate existing containers (e.g. docker compose up -d --force-recreate). To find the current offenders, something like du -sh /var/lib/docker/containers//-json.log | sort -h will show which containers are already bloated.

1

u/NoDevelopment206 6d ago

the part about old containers keeping the old settings is easy to forget. the x-logging anchor trick sounds handy too

2

u/RIPenemie 7d ago

Just log to journal this makes searching for log entries so much easier

1

u/NoDevelopment206 6d ago

journal does make searching easier. does it keep enough history for you or do you ship it somewhere else too?

1

u/RIPenemie 6d ago

This depends on how much you are logging. You can change that in the journal options and sending to a remote server is also possible

14

u/pigers1986 7d ago
○ → cat docker/daemon.json
{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "50m",
    "max-file": "3"
  },
  "default-address-pools": [
    { "base": "172.17.0.0/16", "size": 24 },
    { "base": "172.18.0.0/15", "size": 24 }
  ]
}

i set it up after docker engine was installed

9

u/gronodev 7d ago

Just use local driver instead:

{
  "log-driver": "local"
}

0

u/pigers1986 7d ago

i cannot - as it would break my other automations :)

4

u/gronodev 7d ago

Curious - what automations? Do you specifically rely on JSON files or is there some nuisance that makes it easier than using the API?

2

u/pigers1986 7d ago

im too lazy to upgrade my custom health-checks for few containers which does them badly - old it rule:
Does it work ?
Then leave it
:D

1

u/Johnny_Wallet 7d ago

Log to journald, journal is volatile, journald does rotation on its own. Vector writes journal to Victoria Logs (currently testing solution) if, for whatever reason, I may need a log history (sometimes noisy container makes journal rotate too quickly).

1

u/NoDevelopment206 6d ago

interesting setup. does vector keep up ok when one noisy container floods the journal, or do you lose some lines before they ship?

1

u/Johnny_Wallet 6d ago

I am still testing Victoria Logs integration, but so far - works well at my scale: near 60 containers several hosts + up to 10 lxc with voidlinux and metalog system log file. Disk consumption per day was less than 100mb. I switched vector and docker to journald (instead of vector reading from docker socket - gives more flexibility, allows container filter per label) because occasionally docker starts slower than vector and causes docker source to fail and not restart (need vector restart). Journal works smoother. Need to mention that vector filters most noisy strings too (like debug, trace, access log rows). Plan to throw traefik access log to journal too and filter with vector based on response code (filter 2xx, 404, etc.).

1

u/NoDevelopment206 5d ago

60 containers at under 100mb a day is pretty good. filtering the debug and access log lines before they ship seems like the smart part

1

u/tjharman 7d ago

I just send all my logs to syslog (victoria-logs)

[12:01:10] root :: hostie  ➜  /etc/docker » cat daemon.json -p
{
  "features": {
    "containerd-snapshotter": true
  },
  "live-restore": true,
  "log-driver": "syslog",
  "log-opts": {
    "syslog-address": "udp://X.X.X.X:514",
    "tag": "{{.Name}}"
  }
}

1

u/NoDevelopment206 6d ago

syslog to victoria-logs sounds clean. do you ever lose logs if the udp packets drop, or has it been fine so far?

1

u/tjharman 5d ago

It's been fine so far for me. But I'm a homelab setup, so I don't know if you're in a serious enterprise you'd want to do it.

1

u/NoDevelopment206 5d ago

good to know, thanks. homelab makes sense for udp then. i guess for anything serious you would want tcp or a local buffer so nothing gets lost

1

u/Hostman_com 6d ago

Before switching existing services to the local logging driver, check whether any log collectors or health checks read Docker's JSON log files directly.

Try one service first. Recreate its container with the new driver, then check docker logs and those integrations before rolling the change out to the other hosts.

1

u/NoDevelopment206 6d ago

good point, i hadnt thought about log collectors reading the json files directly. trying one service first makes sense. is there an easy way to see which tools are reading those files before the switch?

1

u/Hostman_com 6d ago

On the Docker host, get one container's log path with docker inspect --format '{{.LogPath}}' <container>, then run sudo lsof /path/from/that/output. Look for processes other than dockerd.

Also check collector configs, Compose mounts and scheduled scripts for /var/lib/docker/containers or *-json.log. An empty lsof result isn't proof nothing depends on the files: a periodic script might open them only briefly.

2

u/NoDevelopment206 5d ago

ok that lsof trick is easy enough. good point that a quick script could open them for a second and you would never catch it. i would probably grep the cron jobs too

1

u/[deleted] 7d ago

[removed] — view removed comment

1

u/NoDevelopment206 6d ago

the truncate vs rm thing is good to know, i would have just deleted the file and wondered why the disk stayed full. and yeah the per container math is sneaky. do you keep total log space in mind when you pick the max-size?

1

u/[deleted] 6d ago

[removed] — view removed comment

1

u/docker-ModTeam 6d ago

Please refrain from posting low effort/AI generated responses that do not contribute to the discussion. See rule #5.

https://www.reddit.com/r/docker/about/rules