Container logs and logging drivers

Intermediate · 17 min

Docker captures what PID 1 writes to stdout and stderr. Reading it with docker logs, picking a logging driver, and stopping json-file from filling the disk.

Before thisInteractive, detached and exec

Better Stack · 21 min · from 4:23

Container logs and logging drivers

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Better StackReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Docker captures only what the container's main process writes to stdout and stderr. Logs written to files inside the container are invisible to it, which is why the nginx image links its log files to /dev/stdout and /dev/stderr.
  • docker logs reads back what was captured, with -f to follow, -n for the last lines, --since and --until for a time window and -t for timestamps.
  • The default json-file driver never rotates unless you set max-size and max-file, so a chatty container can fill the disk.
  • The local driver rotates by default, keeping five 20 MB files per container with compression, and Docker's docs recommend it outside Kubernetes.
  • Remote drivers such as journald, syslog, fluentd, gelf and awslogs ship logs elsewhere. Dual logging keeps a local cache so docker logs still works.
  • Logging settings in daemon.json apply only to containers created after the daemon restarts. Existing containers keep the driver they were created with.

Where did the logs go?

On a normal server, an app writes to /var/log/myapp.log and you tail it. In a container that file lives in a writable layer you can’t see from the host and that disappears with the container. Containers turn logging around: the app writes to its standard output, and the engine catches it and stores it or sends it on. Docker’s docs put it this way: logging drivers capture “the container’s stdout/stderr”. It’s the same deal systemd’s journal offers services, from the journald lesson.

Log to stdout

Docker sees only what PID 1 (and its children) write to file descriptors 1 and 2. Anything written to a file inside the container is invisible to docker logs. The official nginx image shows the standard workaround. I pulled its 34 MB package layer and listed the log directory:

$ tar -tzvf nginx-l1.tgz | grep var/log/nginx
drwxr-xr-x  0 0  0  0 Nov 24 09:47 var/log/nginx/
lrwxrwxrwx  0 0  0  0 Nov 24 09:47 var/log/nginx/access.log -> /dev/stdout
lrwxrwxrwx  0 0  0  0 Nov 24 09:47 var/log/nginx/error.log -> /dev/stderr

nginx thinks it’s writing files. The symlinks send the lines to the container’s stdout and stderr. For your own apps, the better fix is configuration: log to stdout, one event per line, ideally as structured JSON.

Reading logs

docker logs api                       # everything captured so far
docker logs -f -n 100 api             # last 100 lines, then follow
docker logs --since 15m -t api        # last 15 minutes, with timestamps
docker logs --since 2026-11-28T09:00:00Z --until 2026-11-28T09:05:00Z api

Two habits help. Prefer logs -f over attach, because it can’t stop the container. And remember that a container started with -t has one merged stream with carriage returns, as the interactive lesson showed.

Logging drivers

Each container has a logging driver, set by --log-driver or the daemon’s default. Docker ships json-file (the default), local, journald, syslog, fluentd, gelf, awslogs, splunk, gcplogs, etwlogs (Windows) and none, and plugins can add more.

{
  "log-driver": "local",
  "log-opts": { "max-size": "10m" }
}

That goes in /etc/docker/daemon.json. Then restart Docker. Per the docs, the change applies to newly created containers only. Existing containers keep the driver they were created with, and docker inspect -f '{{.HostConfig.LogConfig.Type}}' api shows which one. A single container can override the default:

docker run -d --log-driver local --log-opt max-size=10m --log-opt max-file=3 notes-api:1.4.2

The json-file trap

json-file writes each line as a JSON record under /var/lib/docker/containers/<id>/<id>-json.log. Its max-size option defaults to -1, unlimited, and max-file only takes effect when max-size is set. By default there is no rotation. Docker keeps it that way for backward compatibility and for Kubernetes, and its docs now say outright that this can exhaust the disk and recommend the local driver instead.

The format also costs space. I built one record the way json-file stores it, for a typical 89-byte nginx access log line:

{"log":"172.17.0.1 - - [28/Nov/2026:10:02:11 +0000] \"GET /health HTTP/1.1\" 200 2 \"-\" \"curl/8.11\"\n","stream":"stdout","time":"2026-11-28T10:02:11.123456789Z"}

165 bytes on disk for 89 bytes of log, nearly double. A health check every 10 seconds alone adds about 1.4 MB a day, forever, unless something rotates it.

The local driver fixes both problems. It keeps 100 MB per container by default, as five 20 MB files, compresses rotated files and uses a more compact format. If you need the json-file format, for example because a log shipper reads those files, set max-size and max-file on it.

Shipping logs elsewhere

On a fleet you want logs leaving the host. You can do that two ways. A remote driver (journald, syslog, fluentd, gelf, awslogs, gcplogs, splunk) has the daemon send every line itself. Or keep a local driver and run a collector, such as Fluent Bit, Vector or the OpenTelemetry Collector, that tails the files and forwards them. The log aggregation lesson compares the designs.

Remote drivers used to break docker logs. Now dual logging keeps a local cache (the local format, five 20 MB files) for any driver that can’t be read back, so docker logs still works. cache-disabled turns it off.

One setting deserves thought on busy services: the delivery mode. The default, blocking, means that if the driver can’t keep up, the app’s next write to stdout waits. A slow log endpoint can stall the app. --log-opt mode=non-blocking puts a ring buffer in between, 1 MB unless you set max-buffer-size, and drops new messages when it’s full. You’re choosing between losing logs and stalling the service.

About the video

The embed is Better Stack’s “Docker Logging - “docker logs” Command | Log Drivers | Logging Strategies”, January 2024, from 4:23 to the end, 21 minutes. The clip skips the intro and the nginx pull. Its chapters are viewing nginx logs with docker logs, the command’s options, log drivers, configuring log options in daemon.json, and logging strategies. It doesn’t show the json-file record format or dual logging’s cache, which the lesson adds. English was judged from the channel and its English caption track. Sematext’s short video in deeper focuses on rotation settings.

What was run: the nginx image’s package layer was pulled from Docker Hub’s registry API and listed with tar, and the json-file record was built and measured with Python’s json module, the same compact form Docker writes. Nothing ran in Docker. Driver defaults, delivery modes and dual logging come from docs.docker.com, checked 2026-11-28.

Where this fits

Logs are one view of a container. Next, the rest of the instrument panel: inspect, top, stats and events.

Check yourself

  1. 01Your app writes to /app/logs/app.log and docker logs shows nothing. Why, and what are two fixes?

    Docker only captures PID 1's stdout and stderr, not files. Configure the app to log to stdout, or symlink the log file to /dev/stdout in the image as the nginx image does.

  2. 02A host's disk is full and /var/lib/docker/containers holds a 40 GB *-json.log file. What happened, and how do you prevent it?

    The default json-file driver does no rotation, so one container's log grew without limit. Set log-opts max-size and max-file, or switch to the local driver, in daemon.json and recreate the containers.

  3. 03You set "log-driver": "local" in daemon.json and restarted Docker, but docker inspect shows an old container still using json-file. Is the setting broken?

    No. The logging driver is fixed when a container is created. Only new containers use the new default, so recreate the old ones.

  4. 04What's the risk of the default blocking delivery mode, and what does non-blocking trade for it?

    If the driver can't keep up, writes to stdout block and the app stalls. Non-blocking mode buffers messages in memory (1 MB by default) and drops new ones when the buffer is full.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark