Write logs to stdout and stderr, not to files inside the container.
Why this works:
- Docker captures both streams.
docker logsreads them.- The platform ships them to a central log system.
This follows the Twelve-Factor idea: treat logs as a stream of events. Write structured JSON lines, because they are easier to search.
What happens by default? Docker uses the json-file logging driver. It stores logs on the host and does not rotate them (rotate means delete or archive old logs). So a container that logs a lot can fill the disk.
Fixes:
- Set rotation with log options like
max-sizeandmax-file. You can set them per container or in the daemon config. - Or use the
localdriver, which rotates by default.
Logs written to files inside the container are invisible to docker logs. They are also lost when the container is removed.
- Log to stdout and stderr, not to files.
- docker logs reads the output Docker captured.
- The default json-file driver does not rotate logs.
- Set max-size and max-file, or use the local driver.
Read more