Guides

docker logs: tail, follow and filter container logs (and why they are empty)

docker logs --tail 100 -f shows the last lines and follows new ones. Filter by time, grep stderr, read Compose logs, find empty logs and set log rotation.

Published ~8 min read
On this page

Short answer: docker logs --tail 100 -f web prints the last 100 lines of the container web and keeps streaming new ones until you press Ctrl+C, which stops only the viewer, not the container. For a Compose service, run docker compose logs -f --tail=100 web in the project folder.

any Docker host
docker logs --tail 100 web            # last 100 lines, then exit
docker logs -f --tail 100 web         # last 100 lines, then follow
docker logs --since 30m -t web        # last 30 minutes, with timestamps
docker compose logs -f --tail=100 web # same for a Compose service

Without --tail, docker logs prints everything Docker still keeps for the container, which on a busy server can be gigabytes. The options and behaviour below come from Docker’s reference for docker container logs and docker compose logs, its logging driver pages and the Docker CLI source, read on 9 November 2026. Docker was not run for this article, so output blocks show the documented format.

docker logs options

OptionWhat it doesExample
--tail N, -n NOnly the last N lines; the default is alldocker logs -n 50 web
-f, --followKeep printing new lines as they arrivedocker logs -f web
--sinceOnly lines after a time or agedocker logs --since 1h web
--untilOnly lines before a time or agedocker logs --until 10m web
-t, --timestampsPrefix each line with the time Docker recorded itdocker logs -t --tail 20 web
--detailsAdd extra attributes set with --log-opt, such as labels or envdocker logs --details web

The container can be named or given by ID; docker ps shows both, and docker ps -a includes stopped containers, whose logs you can still read. With the default driver, logs live with the container, so docker rm deletes them, and a container started with --rm takes its logs with it when it exits. Ways to start containers are in the docker run guide.

Tail and follow: --tail and -f

--tail takes a number of lines counted from the end. A negative number or anything that is not a number counts as all, so a typo prints the whole history instead of an error. --tail 0 -f shows nothing old and only what arrives from now on, the way tail -f on a file behaves; the tail command guide covers that side.

-f keeps the command attached to the log stream. Ctrl+C ends docker logs; the container keeps running. You can combine --since with --tail, -f or both, for example docker logs -f --since 10m --tail 200 web.

Logs from a time range: --since and --until

Both options accept three kinds of value:

  • A duration back from now: 42m, 3h, 1m30s.
  • A date and time: RFC 3339 such as 2026-11-09T08:00:00Z, or the shorter 2026-11-09T08:00:00, 2026-11-09T08:00 and 2026-11-09.
  • A Unix timestamp in seconds, optionally with a fraction.
any Docker host
docker logs --since 2026-11-09T08:00:00 --until 2026-11-09T08:15:00 web
docker logs --since 2h --until 1h web

A time without Z or an offset such as +01:00 is read in the timezone of the machine where you type the command, which on a server is often UTC while your application logs local time. Add -t to see when Docker recorded each line; the timestamp is in RFC 3339 format with nanoseconds, padded with zeros so the columns line up:

documented format · docker logs -t
2026-11-09T08:01:12.042817300Z GET /health 200
2026-11-09T08:01:13.500000000Z GET / 200

Search logs with grep, and save them to a file

docker logs writes what the container sent to stdout to your stdout, and what it sent to stderr to your stderr. A pipe only carries stdout, so error messages, which most programs print to stderr, slip past grep. Redirect stderr into the pipe first:

any Docker host
docker logs --since 1h web 2>&1 | grep -i error
docker logs web > web.log 2>&1
docker logs web 2>&1 | wc -l

A container started with a TTY (-t on docker run, tty: true in Compose) has only one stream, and everything arrives on stdout. The guide to redirecting stdout and stderr explains 2>&1 in detail.

Logs of a Docker Compose service

in the folder with compose.yaml
docker compose logs -f --tail=100 web    # one service
docker compose logs --since 15m          # every service, prefixed with its name
docker compose logs --no-log-prefix web  # without the name column
docker compose logs --index 2 web        # second replica only

docker compose logs takes the same --tail, -f, --since, --until and -t options. Its --tail counts per container, so with three services --tail=100 can print 300 lines. Use the service name from the Compose file, not the container name, and run it in the project folder or name the file with -f before logs.

Where Docker stores container logs

With the default json-file driver, each container has one file under the Docker data root, by default /var/lib/docker/containers/ID/ID-json.log, with one JSON object per line holding the text, the stream and the time. Ask Docker for the exact path and for the driver in use:

any Docker host
docker inspect --format '{{.LogPath}}' web
docker inspect --format '{{.HostConfig.LogConfig.Type}}' web
docker info --format '{{.LoggingDriver}}'      # default for new containers

The local driver keeps its files (rotated ones compressed) in a local-logs folder in the same container directory and leaves LogPath empty. Docker’s documentation says these files are meant for the daemon only and that other tools touching them can interfere with logging, so read logs with docker logs, and do not truncate or delete the files by hand.

Why docker logs shows nothing

CauseCheckFix
The app writes to a log file inside the containerdocker exec web ls -l /var/log/appLog to stdout and stderr, or symlink the log file to /dev/stdout
Python buffers stdout when it is not a terminalOutput appears late or in burstsPYTHONUNBUFFERED=1 or python -u
A logging driver that cannot be read back, with the cache offdocker inspect --format '{{.HostConfig.LogConfig.Type}}' webRead the logs where the driver sends them
The container was recreateddocker ps -aThe old container took its logs with it

The first cause is the common one. Docker only captures what the main process writes to stdout and stderr. The official nginx image therefore links /var/log/nginx/access.log to /dev/stdout and error.log to /dev/stderr, and your own image can do the same. Python writes stdout in blocks when it is not attached to a terminal, so short messages wait in the buffer; PYTHONUNBUFFERED=1 in the environment turns that off.

Drivers such as syslog or gelf send logs elsewhere. Docker keeps a local cache next to them (dual logging, by default 5 files of 20 MB) so docker logs still works. With cache-disabled set, it answers Error response from daemon: configured logging driver does not support reading. With journald, docker logs works and journalctl reads the same entries; the journalctl guide has the filters.

Stop logs from filling the disk: rotation

The json-file driver has no size limit by default (max-size is -1, unlimited), so a chatty container grows its log until the disk is full. The Docker install guide shows the /etc/docker/daemon.json that sets max-size and max-file for every new container. You can also set it per container or per Compose service:

one container
docker run -d --name web \
  --log-opt max-size=10m --log-opt max-file=3 \
  nginx
compose.yaml
services:
  web:
    image: nginx
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"
  • max-file only works together with max-size; the oldest file is deleted when there are too many. Add compress: "true" to gzip the rotated files.
  • The local driver rotates and compresses on its own: 5 files of 20 MB, so about 100 MB per container, unless you set other values. Docker itself recommends it to prevent disk exhaustion and keeps json-file as the default for backward compatibility.
  • Logging settings are fixed when a container is created. After a change, recreate the container (docker compose up -d --force-recreate for Compose); a restart is not enough. Values in daemon.json must be strings in quotes.

Frequently asked

How do I tail the logs of a Docker container?

Run docker logs --tail 100 -f CONTAINER. --tail 100 (or -n 100) prints the last 100 lines and -f keeps printing new ones until you press Ctrl+C, which stops only the viewer, not the container. Use --tail 0 -f to see only lines that arrive from now on.

Why does grep not find errors in docker logs output?

docker logs passes the container's stderr to your stderr, and a pipe only carries stdout, so error lines skip grep. Use docker logs CONTAINER 2>&1 | grep -i error. Containers started with a TTY send everything on stdout, so for them the redirect makes no difference.

Where are Docker container logs stored?

With the default json-file driver, in /var/lib/docker/containers/ID/ID-json.log under the Docker data root; docker inspect --format '{{.LogPath}}' CONTAINER prints the exact path. The local driver keeps its files, rotated ones compressed, in a local-logs folder in the same container directory. Docker's documentation says these files are for the daemon only, so read them with docker logs.

How do I limit the size of Docker logs?

Set max-size and max-file, for example --log-opt max-size=10m --log-opt max-file=3 on docker run, the same keys under logging.options in a Compose file, or log-opts in /etc/docker/daemon.json for all new containers. The local driver rotates by default at 5 files of 20 MB. Existing containers keep their old settings until they are recreated.

Related articles

All guides