CodePark/ questions / Healthchecks, restarts & logs
START PRACTICINGPRACTICE
Backend question · Docker › Healthchecks, restarts & logs

How should a containerised Node service handle logging, and what happens to those logs by default?

Write logs to stdout and stderr, not to files inside the container.

Why this works:

  • Docker captures both streams.
  • docker logs reads 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-size and max-file. You can set them per container or in the daemon config.
  • Or use the local driver, 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.