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.
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 serviceWithout --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
| Option | What it does | Example |
|---|---|---|
--tail N, -n N | Only the last N lines; the default is all | docker logs -n 50 web |
-f, --follow | Keep printing new lines as they arrive | docker logs -f web |
--since | Only lines after a time or age | docker logs --since 1h web |
--until | Only lines before a time or age | docker logs --until 10m web |
-t, --timestamps | Prefix each line with the time Docker recorded it | docker logs -t --tail 20 web |
--details | Add extra attributes set with --log-opt, such as labels or env | docker 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 shorter2026-11-09T08:00:00,2026-11-09T08:00and2026-11-09. - A Unix timestamp in seconds, optionally with a fraction.
docker logs --since 2026-11-09T08:00:00 --until 2026-11-09T08:15:00 web
docker logs --since 2h --until 1h webA 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:
2026-11-09T08:01:12.042817300Z GET /health 200
2026-11-09T08:01:13.500000000Z GET / 200Search 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:
docker logs --since 1h web 2>&1 | grep -i error
docker logs web > web.log 2>&1
docker logs web 2>&1 | wc -lA 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
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 onlydocker 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:
docker inspect --format '{{.LogPath}}' web
docker inspect --format '{{.HostConfig.LogConfig.Type}}' web
docker info --format '{{.LoggingDriver}}' # default for new containersThe 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
| Cause | Check | Fix |
|---|---|---|
| The app writes to a log file inside the container | docker exec web ls -l /var/log/app | Log to stdout and stderr, or symlink the log file to /dev/stdout |
| Python buffers stdout when it is not a terminal | Output appears late or in bursts | PYTHONUNBUFFERED=1 or python -u |
| A logging driver that cannot be read back, with the cache off | docker inspect --format '{{.HostConfig.LogConfig.Type}}' web | Read the logs where the driver sends them |
| The container was recreated | docker ps -a | The 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:
docker run -d --name web \
--log-opt max-size=10m --log-opt max-file=3 \
nginxservices:
web:
image: nginx
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"max-fileonly works together withmax-size; the oldest file is deleted when there are too many. Addcompress: "true"to gzip the rotated files.- The
localdriver 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 keepsjson-fileas 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-recreatefor Compose); a restart is not enough. Values indaemon.jsonmust 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.