Free Up Disk Space on a Docker Host Without Downtime
It is the first day of a big sale. The whole shop runs on a single server as a handful of Docker containers. Around noon, customers start reporting that they cannot log in or add items to their carts. The developers tell you that every write to Redis fails with this error:
MISCONF Redis is configured to save RDB snapshots, but it's currently unable to persist to disk.
Commands that may modify the data set are disabled, because this instance is configured to report
errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option).
Please check the Redis logs for details about the RDB error.
Nobody deployed anything today. You are on call, and you have just logged in to the server.
Because of the sale, nothing can be stopped, restarted, or removed. Every container must keep running while you fix the problem.
Start with the diagnosis. Redis reports the error, but the cause is somewhere else on the server. Find out which container filled up the root filesystem and enter its name or ID below. If an answer is wrong, click the task to see a hint.
Hint 1: Where to start looking
First, find out which filesystem is full.
Then walk down its directory tree to see where the space went.
The df and du commands help with both steps.
Hint 2: From a directory to a container
Docker keeps a directory for every container under /var/lib/docker/containers.
Each of these directories is named after the full ID of a container.
Now bring the shop back. Free up the space on the disk without stopping, restarting, or removing any container.
Hint 3: Where the output goes
Every line that a container prints ends up in a log file on the host.
You can get the path to this file from docker inspect.
Hint 4: Keeping the container alive
Removing the container would also remove its log, but the container has to keep running. Look for a way to make the file smaller without deleting it.
Hint 5: Why deleting the file does not help
The Docker daemon keeps the log file of a running container open.
On Linux, the rm command removes only the name of a file.
The data stays on the disk until the last process that holds the file open closes it.
Emptying the file in place frees its blocks right away, and the daemon keeps appending new lines to the same file.
Hint 6: Emptying a file in place
The truncate command changes the size of a file, and a size of zero empties it.
A shell redirection that writes nothing into the file empties it, too.
The log file belongs to root.
Keep in mind that sudo does not apply to a redirection, because your own shell opens the file before sudo starts.
Hint: The concepts behind it
The tutorial Why Deleting a File Doesn't Free the Disk explains inodes, file descriptors, and what the rm and truncate commands change, with exercises in a playground.
Once the disk has room again, Redis should be able to save its data. Make sure that it accepts writes again.
The shop is back, but the container that caused the incident still writes a new log line five times per second. Nothing limits the size of its log, so the disk will fill up again. A lasting fix means recreating the container with a log size limit, and that has to wait until the sale is over.