A log file filled the disk and took the database with it
On a small container host with default settings, one container's log can grow until the disk is full, and a database on the same disk stops with it. How the failure runs, how to check for it today and the settings that prevent it.
If you run containers on a small host and have never set a size limit on their logs, go and look at how big those logs are. With Docker's default settings they grow until the disk is full. When that happens, a database on the same disk is usually the first thing to stop.
This is an ordinary way for a small server to fall over, and the symptom shows up a long way from the cause.
How the failure runs
It starts with a default. Docker's documentation says: "As a default, Docker uses the json-file logging driver, which caches container logs as JSON internally." Everything a container writes to standard output or standard error is captured and appended to a file on the host, one file per container.
The same page carries a warning that is easy to read past: "By default, no log-rotation is performed." The driver's reference page gives the setting behind it. The max-size option is the size at which a log is rolled, and the page gives its default as "-1 (unlimited)."
So every container has a file that only grows. For a quiet container that may never matter. For a chatty one it will. An application left at debug level, a job that logs every item it processes or a loop that logs each failed retry will produce gigabytes without anyone noticing.
On a small host that file shares a filesystem with everything else: the container images, the volumes and the directory where the database keeps its data.
Then the disk reaches 100 percent. Programs that only read carry on, which is part of why the host looks alive. Anything that needs to write gets an error.
A database needs to write constantly. Postgres records every change in its write-ahead log before it applies the change, and it cannot skip that step. The Postgres documentation is direct about what comes next: "If the disk holding the WAL files grows full, database server panic and consequent shutdown might occur."
If the database container has a restart policy, Docker starts it again. Postgres begins crash recovery, which the documentation describes as replaying the log from the latest checkpoint. Recovery also has to write, the disk is still full, and the server stops again. The container can cycle like that for as long as the disk stays full.
Why it is hard to diagnose
Everything you see points at the database. The application reports that it cannot connect. The database container shows as restarting or unhealthy. The database log says there is no space left on the device.
So you look at the database, and the database is small. A few hundred megabytes of data on a disk of many gigabytes does not look like the reason the disk is full, and it is not. The space went to a file belonging to a different container, in a directory most people never open, written by an application that is still running and still appears healthy.
It is the neighbor's bathtub coming through your ceiling.
How to check today
Three checks, in order.
Check the filesystem with df -h. If the filesystem that holds Docker's data directory is above about 80 percent, keep going.
Check each container's logging configuration:
docker inspect -f '{{.HostConfig.LogConfig}}' <container>
If the driver is json-file and the options are empty, that container has no limit.
Find the file and look at its size:
docker inspect -f '{{.LogPath}}' <container>
Run ls -lh on the path it prints.
While you are there, look at the other things that grow without being asked: the system journal and old container images that nothing uses anymore.
The settings that prevent it
Set a limit for the whole host in Docker's daemon configuration file:
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}
With those values each container keeps at most three files of ten megabytes. Note that max-file does nothing by itself. The reference page says it is only "effective when max-size is also set."
Docker's own recommendation goes a step further. The documentation suggests the local logging driver, "as it performs log-rotation by default." If nothing you run depends on the JSON file format, that is the simpler choice.
There is a catch that undoes the setting if you miss it. The documentation says that changing the daemon configuration "only affects containers that are created after the configuration is changed." Restarting Docker is not enough, and restarting the container is not enough either. An existing container keeps the logging settings it was created with. You have to remove and recreate it. With Compose, that means bringing the service up again with the option that forces recreation.
After that, run the inspect command again on every container and confirm the options are there. I would not trust the configuration file alone, for the reason just given.
You can also set the limit for each service in the Compose file, or for each container with --log-opt on the command line. I would do both: the daemon default to catch whatever I forget, and an explicit setting on the services I know to be noisy.
Without rotation the log fills the free space in about 100 days. With the two settings it stops at 30 MB however long the container runs.
The capped line uses the settings from this post: max-size 10m and max-file 3, which stop the log at 30 MB.
Getting out once it has happened
Free space first. Do not start by deleting anything in the database's directory.
The safest space to reclaim is whatever is unrelated to the database: unused images, old journal entries and the oversized log itself. Docker's documentation warns against working on its log files with outside tools, so I would leave the file alone and get rid of it by recreating its container with limits set. If the disk sits on a volume that can be grown, growing it is a reasonable way to get breathing room, as long as you then fix the cause.
Then start the database and read its log while it recovers. The news here is better than it feels at the time. The Postgres documentation says that a filled data disk "will not result in data corruption." The server stopped because it refused to continue without its log, which is the behavior you want. Once it can write again, it replays what it needs and comes up.
Confirm that with your own eyes: connect, list the databases and check a table you know. A clean start in the log is good evidence, and a query that returns the rows you expect is better.
Keeping it from coming back
Put the database's data on its own filesystem or volume. Then another container's log cannot fill it, whatever that log does.
Alert on disk usage well before full. The Postgres documentation adds a practical reason: "Some file systems perform badly when they are almost full, so do not wait until the disk is completely full to take action."
Review what each container logs. A rotation limit keeps the disk safe, but an application writing gigabytes of logs is usually telling you something else is wrong with it.
On a shared host, the ownership is worth stating out loud. Whoever runs the container platform owns the daemon defaults, including log limits. Whoever owns each application owns how much it logs. Whoever owns the database owns where its data lives and the alert on that volume. One caveat: on a small host those are often the same person, and in my lab all three are me, which means none of them can assume one of the others has it covered.
Sources
Views are my own.