Mad World #Save_GAZA: Free Palestine  

Search

Docker Logs Tail and Follow: How to Use docker logs -f --tail

Docker Logs Tail and Follow: How to Use docker logs -f --tail

docker logs shows what a container writes to its standard output (stdout) and standard error (stderr). The command you will use most is docker logs -f --tail 100 <container>: it prints the last 100 lines of the log, then keeps running and prints each new line as the container writes it. Press Ctrl+C to stop following. The container keeps running.

This guide shows --tail, -f, --since and --until, timestamps, grep (and why it can miss your errors), saving logs to a file, docker compose logs, where Docker keeps the log file, how to clear it and how to stop it from filling your disk. We ran every command on 14 November 2026 on Docker Engine 29.2.1 and Docker Compose v2.29.7 with a test container. The outputs below are the real ones. docker logs is the short form of docker container logs: both run the same command.

docker logs cheat sheet

What you wantCommand
Show the whole logdocker logs demo-app
Show the last 100 linesdocker logs --tail 100 demo-app
Follow new lines livedocker logs -f demo-app
Last 100 lines, then followdocker logs -f --tail 100 demo-app
Lines from the last 10 minutesdocker logs --since 10m demo-app
Lines older than 1 hourdocker logs --until 1h demo-app
Add Docker's timestampsdocker logs -t demo-app
Search for errorsdocker logs demo-app 2>&1 | grep -i error
Save the log to a filedocker logs demo-app > demo-app.log 2>&1
All services of a Compose projectdocker compose logs -f --tail 50
Path of the log filedocker inspect --format '{{.LogPath}}' demo-app

Replace demo-app with the name or ID of your container. docker ps lists running containers, and docker ps -a also lists stopped ones.

The test container

To get real output, we started a small BusyBox container named demo-app. Every 2 seconds it writes an INFO line to stdout. Every fourth request it also writes an ERROR line to stderr, like a web app that logs its failed requests:

Terminal
docker run -d --name demo-app busybox sh -c '
  i=0
  while true; do
    i=$((i+1))
    echo "$(date -u +%FT%TZ) INFO request $i GET /api/items 200"
    if [ $((i % 4)) -eq 0 ]; then
      echo "$(date -u +%FT%TZ) ERROR request $i upstream timeout after 3000 ms" >&2
    fi
    sleep 2
  done'

Without options, docker logs prints the whole log, from the moment the container was created, and exits. After 26 seconds our container had written 17 lines:

Terminal
docker logs demo-app
Code
2026-11-14T13:22:12Z INFO request 1 GET /api/items 200
2026-11-14T13:22:14Z INFO request 2 GET /api/items 200
2026-11-14T13:22:16Z INFO request 3 GET /api/items 200
2026-11-14T13:22:18Z INFO request 4 GET /api/items 200
2026-11-14T13:22:18Z ERROR request 4 upstream timeout after 3000 ms
2026-11-14T13:22:20Z INFO request 5 GET /api/items 200
2026-11-14T13:22:22Z INFO request 6 GET /api/items 200
2026-11-14T13:22:24Z INFO request 7 GET /api/items 200
2026-11-14T13:22:26Z INFO request 8 GET /api/items 200
2026-11-14T13:22:26Z ERROR request 8 upstream timeout after 3000 ms
2026-11-14T13:22:28Z INFO request 9 GET /api/items 200
2026-11-14T13:22:30Z INFO request 10 GET /api/items 200
2026-11-14T13:22:32Z INFO request 11 GET /api/items 200
2026-11-14T13:22:34Z INFO request 12 GET /api/items 200
2026-11-14T13:22:34Z ERROR request 12 upstream timeout after 3000 ms
2026-11-14T13:22:36Z INFO request 13 GET /api/items 200
2026-11-14T13:22:38Z INFO request 14 GET /api/items 200

On a container that has run for weeks, the whole log can be millions of lines. That is why you almost always add --tail.

What docker logs shows, and what it does not

A container's log is what its main process writes to stdout and stderr. Docker's logging driver stores those lines, and docker logs reads them back. The default driver, json-file, writes them to a JSON file on the host.

So docker logs does not show files that an app writes inside the container, such as /var/log/app.log. Some official images send their log files to stdout. As Docker's page on container logs explains, the official nginx image links /var/log/nginx/access.log to /dev/stdout and error.log to /dev/stderr, so nginx requests appear in docker logs. If your app only writes to a file, read that file inside the container:

Terminal
docker exec demo-app tail -n 50 /path/to/app.log

docker logs --tail: show the last lines

--tail N prints only the last N lines. Its short form is -n N, and the default is all. The count includes stdout and stderr lines, in the order the container wrote them:

Terminal
docker logs --tail 3 demo-app
Code
2026-11-14T13:22:34Z ERROR request 12 upstream timeout after 3000 ms
2026-11-14T13:22:36Z INFO request 13 GET /api/items 200
2026-11-14T13:22:38Z INFO request 14 GET /api/items 200

-n 1 works the same way:

Terminal
docker logs -n 1 demo-app
Code
2026-11-14T13:22:44Z INFO request 17 GET /api/items 200

docker logs -f: follow the log live

-f (long form --follow) keeps the command running and prints each new line as the container writes it, like tail -f on a file. Combine it with --tail, or Docker first prints the whole log before it starts to follow:

Terminal
docker logs -f --tail 1 demo-app

We let it run for 7 seconds. It printed the last line, then each new line as it came, the ERROR line included:

Code
2026-11-14T13:22:38Z INFO request 14 GET /api/items 200
2026-11-14T13:22:40Z INFO request 15 GET /api/items 200
2026-11-14T13:22:42Z INFO request 16 GET /api/items 200
2026-11-14T13:22:42Z ERROR request 16 upstream timeout after 3000 ms
2026-11-14T13:22:44Z INFO request 17 GET /api/items 200

Press Ctrl+C to stop following. It stops docker logs, not the container. Here are both commands in a terminal:

Terminal: docker logs --tail 3 prints three lines and exits, then docker logs -f --tail 2 prints new log lines until Ctrl+C
Real terminal session, 14 November 2026. (1) --tail 3 prints the last three lines and returns to the prompt. (2) -f --tail 2 prints the last two lines, then one new line every 2 seconds, until Ctrl+C (the ^C on the last line).

docker logs --since and --until: filter by time

--since shows the lines written after a point in time, and --until the lines written before it. Both take a relative time, such as 30s, 10m or 2h, or a date such as 2026-11-14 or 2026-11-14T13:00:00Z. The docker logs reference says a date without Z or an offset such as +01:00 is read in your computer's local time zone.

Terminal
docker logs --since 6s demo-app
Code
2026-11-14T13:22:32Z INFO request 11 GET /api/items 200
2026-11-14T13:22:34Z INFO request 12 GET /api/items 200
2026-11-14T13:22:34Z ERROR request 12 upstream timeout after 3000 ms
2026-11-14T13:22:36Z INFO request 13 GET /api/items 200
2026-11-14T13:22:38Z INFO request 14 GET /api/items 200

--until 20s means "until 20 seconds ago". Here we kept only the last two of those lines:

Terminal
docker logs --until 20s demo-app | tail -n 2
Code
2026-11-14T13:22:14Z INFO request 2 GET /api/items 200
2026-11-14T13:22:16Z INFO request 3 GET /api/items 200

You can combine both, and add -f or --tail. For example, the lines of a 30-minute window in UTC:

Terminal
docker logs --since 2026-11-14T13:00:00Z --until 2026-11-14T13:30:00Z demo-app

docker logs -t: add timestamps

-t (long form --timestamps) puts the time Docker received each line in front of it, in UTC with nanoseconds. Our test app already writes its own time, so each line has two:

Terminal
docker logs -t --tail 2 demo-app
Code
2026-11-14T13:22:36.154206979Z 2026-11-14T13:22:36Z INFO request 13 GET /api/items 200
2026-11-14T13:22:38.156159589Z 2026-11-14T13:22:38Z INFO request 14 GET /api/items 200

Use -t when your app writes no time of its own, or to compare the app's clock with Docker's.

Search Docker logs with grep: the 2>&1 trap

docker logs sends the container's stdout to your terminal's stdout, and the container's stderr to your terminal's stderr. A pipe (|) carries only stdout. So when an app writes its errors to stderr, a plain grep misses them:

Terminal
docker logs demo-app | grep ERROR | wc -l
Code
2026-11-14T13:22:18Z ERROR request 4 upstream timeout after 3000 ms
2026-11-14T13:22:26Z ERROR request 8 upstream timeout after 3000 ms
2026-11-14T13:22:34Z ERROR request 12 upstream timeout after 3000 ms
0

The three ERROR lines still appear on the screen, because stderr goes straight to the terminal, past grep and wc. And wc counts 0, because none of them went through the pipe. Add 2>&1 to send stderr into the pipe too:

Terminal
docker logs demo-app 2>&1 | grep ERROR
Code
2026-11-14T13:22:18Z ERROR request 4 upstream timeout after 3000 ms
2026-11-14T13:22:26Z ERROR request 8 upstream timeout after 3000 ms
2026-11-14T13:22:34Z ERROR request 12 upstream timeout after 3000 ms
Terminal: docker logs piped to grep -c ERROR counts 0 while the ERROR lines print anyway; with 2>&1, grep -c counts 5
The same trap in a real terminal. (1) Without 2>&1, grep -c counts 0, while the five ERROR lines bypass the pipe and print anyway. (2) With 2>&1, grep -c counts all 5.

Useful grep options: -i ignores case, -c counts the matching lines, -A 5 also prints the 5 lines after each match (often a stack trace), and -v hides the lines that match. For example, the errors of the last hour with 3 lines of context:

Terminal
docker logs --since 1h demo-app 2>&1 | grep -i -A 3 error

Save Docker logs to a file

Redirect both streams to one file:

Terminal
docker logs demo-app > demo-app.log 2>&1

Or keep stdout and stderr in two files:

Terminal
docker logs demo-app > stdout.log 2> stderr.log

To count each stream, send the other one to /dev/null. Our container had written 14 lines to stdout and 3 to stderr:

Terminal
docker logs demo-app 2>/dev/null | wc -l
docker logs demo-app 2>&1 >/dev/null | wc -l
Code
14
3

docker compose logs: every service of a project

docker compose logs shows the logs of all the services of a Compose project. Each line starts with the name of its container, and the command takes the same --tail, -f, -t, --since and --until options. Our test project, named logsdemo, has two services: app, the same BusyBox loop, and web, an nginx container. In the project folder, you can leave out -p logsdemo: Compose finds the project from the folder.

Terminal
docker compose -p logsdemo logs --tail 2
Code
app-1  | 2026-11-14T13:23:07Z INFO request 6 GET /api/items 200
app-1  | 2026-11-14T13:23:09Z INFO request 7 GET /api/items 200
web-1  | 2026/11/14 13:22:57 [notice] 1#1: start worker process 36
web-1  | 2026/11/14 13:22:57 [notice] 1#1: start worker process 37

Note that --tail 2 gives two lines per container, not two in total. Name a service to see only its lines:

Terminal
docker compose -p logsdemo logs --tail 3 app
Code
app-1  | 2026-11-14T13:23:05Z INFO request 5 GET /api/items 200
app-1  | 2026-11-14T13:23:07Z INFO request 6 GET /api/items 200
app-1  | 2026-11-14T13:23:09Z INFO request 7 GET /api/items 200

-f follows the service live. We stopped it after 5 seconds, and Compose printed canceled when it stopped:

Terminal
docker compose -p logsdemo logs -f --tail 1 app
Code
app-1  | 2026-11-14T13:23:09Z INFO request 7 GET /api/items 200
app-1  | 2026-11-14T13:23:11Z INFO request 8 GET /api/items 200
app-1  | 2026-11-14T13:23:11Z ERROR request 8 upstream timeout after 3000 ms
app-1  | 2026-11-14T13:23:13Z INFO request 9 GET /api/items 200
canceled

--no-log-prefix removes the app-1 | prefix, which is useful before grep or when you save the log to a file:

Terminal
docker compose -p logsdemo logs --no-log-prefix --tail 2 app
Code
2026-11-14T13:23:11Z ERROR request 8 upstream timeout after 3000 ms
2026-11-14T13:23:13Z INFO request 9 GET /api/items 200

New to Compose? Read what Docker Compose is and how docker compose up starts a project.

Where Docker stores container logs

With the default json-file driver, each container has its own log file on the host, in /var/lib/docker/containers/<container-id>/<container-id>-json.log. docker inspect prints the exact path:

Terminal
docker inspect --format '{{.LogPath}}' demo-app
Code
/var/lib/docker/containers/e0dbb175305bd308e6f6cd30e9d9b7545d3cf3a2d9309ccacd1781a44e0f6125/e0dbb175305bd308e6f6cd30e9d9b7545d3cf3a2d9309ccacd1781a44e0f6125-json.log

The file belongs to root (-rw-r-----), so you need sudo to read it. Each line is a JSON object with the text, the stream it came from and Docker's timestamp. These are the first three lines of our container's file, read as root:

Terminal
sudo head -n 3 "$(docker inspect --format '{{.LogPath}}' demo-app)"
JSON
{"log":"2026-11-14T13:22:12Z INFO request 1 GET /api/items 200\n","stream":"stdout","time":"2026-11-14T13:22:12.108631586Z"}
{"log":"2026-11-14T13:22:14Z INFO request 2 GET /api/items 200\n","stream":"stdout","time":"2026-11-14T13:22:14.111895768Z"}
{"log":"2026-11-14T13:22:16Z INFO request 3 GET /api/items 200\n","stream":"stdout","time":"2026-11-14T13:22:16.115289724Z"}

On Docker Desktop for Windows or macOS, this path is inside Docker's Linux virtual machine, not on your own disk. There, use docker logs. On a Synology NAS, the path and the format are different: see the Synology section below.

Limit the size of Docker logs (log rotation)

By default, the json-file driver keeps every line and never deletes anything. Our test container had no limit at all:

Terminal
docker inspect --format '{{json .HostConfig.LogConfig}}' demo-app
JSON
{"Type":"json-file","Config":{}}

The json-file driver's documentation gives the reason: max-size defaults to -1, which means unlimited. A container that logs a lot can fill your disk. Two options fix it. max-size rotates the file when it reaches a size, and max-file sets how many files Docker keeps (it works only together with max-size).

We tested it with a tiny limit, 1 KB and 3 files, on a container that writes 400 lines at once:

Terminal
docker run -d --name demo-rotate --log-opt max-size=1k --log-opt max-file=3 busybox sh -c 'i=0; while [ $i -lt 400 ]; do i=$((i+1)); echo "line $i of a chatty container"; done; sleep 600'
docker inspect --format '{{json .HostConfig.LogConfig}}' demo-rotate
JSON
{"Type":"json-file","Config":{"max-file":"3","max-size":"1k"}}

Docker kept three files, none over 1 KB, and deleted the older lines. docker logs now returns only the last 22 lines, 379 to 400:

Code
-rw-------    1 root     root          2696 Nov 14 13:22 config.v2.json
-rw-r-----    1 root     root           202 Nov 14 13:22 <id>-json.log
-rw-r-----    1 root     root          1010 Nov 14 13:22 <id>-json.log.1
-rw-r-----    1 root     root          1008 Nov 14 13:22 <id>-json.log.2
-rw-------    1 root     root          1509 Nov 14 13:22 hostconfig.json
Terminal
docker logs demo-rotate | wc -l
docker logs demo-rotate | head -n 2
Code
22
line 379 of a chatty container
line 380 of a chatty container

Use a real size, such as 10 MB and 3 files. You can set it in three places:

  1. One container: docker run --log-opt max-size=10m --log-opt max-file=3 ...
  2. A Compose service, in compose.yaml. This is the block of our test project:
YAML
services:
  app:
    image: busybox
    command: ["sh", "-c", "i=0; while true; do i=$$((i+1)); echo \"$$(date -u +%FT%TZ) INFO request $$i GET /api/items 200\"; if [ $$((i % 4)) -eq 0 ]; then echo \"$$(date -u +%FT%TZ) ERROR request $$i upstream timeout after 3000 ms\" >&2; fi; sleep 2; done"]
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"
  1. The default for new containers, in /etc/docker/daemon.json, then restart Docker with sudo systemctl restart docker. The values must be strings, in quotes:
JSON
{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  }
}

The daemon.json default applies only to containers created after the restart. Existing containers keep their old settings until you recreate them, for example with docker compose up -d --force-recreate. Docker's logging documentation also recommends the local logging driver, which rotates its files by default and uses a more compact format.

How to clear Docker logs

There is no docker logs --clear. You have two ways:

  1. Recreate the container, for example with docker compose up -d --force-recreate. A new container starts with an empty log.
  2. Empty the file in place, as root, while the container keeps running:
Terminal
sudo truncate -s 0 "$(docker inspect --format '{{.LogPath}}' demo-app)"

We emptied our container's file this way. Right after, docker logs demo-app | wc -l printed:

Code
0

Use truncate, not rm. Docker keeps the file open while the container runs, and on Linux a deleted file that is still open keeps its disk space. Clearing logs by hand is a one-off fix; rotation, above, is the lasting one.

Logs of a stopped container

docker logs also works on a stopped container, as long as the container exists. We stopped ours (-t 1 makes Docker kill it after 1 second, hence the code 137) and read its log:

Terminal
docker stop -t 1 demo-app
docker ps -a --filter name=demo-app --format '{{.Names}} {{.Status}}'
docker logs --tail 2 demo-app
Code
demo-app Exited (137) Less than a second ago
2026-11-14T13:22:48Z INFO request 19 GET /api/items 200

It printed one line, not two, because we had emptied the log a few seconds before. docker rm deletes the container and its log together, so save the log first if you still need it.

Common docker logs errors

Error response from daemon: No such container

The name or ID is wrong. We typed demo-ap instead of demo-app:

Code
Error response from daemon: No such container: demo-ap

Check the exact name with docker ps -a. In a Compose project, the container name is usually the project and service names plus a number, such as myproject-app-1. docker compose logs app uses the service name instead.

configured logging driver does not support reading

We got this with a container started with --log-driver none, which keeps no logs at all:

Code
Error response from daemon: configured logging driver does not support reading

Check the driver with docker inspect --format '{{.HostConfig.LogConfig.Type}}' <container>. With none, there is nothing to read: recreate the container with another driver. With a remote driver such as syslog or Splunk, Docker keeps a local copy for docker logs, called dual logging, unless cache-disabled is set to true.

docker logs shows nothing

Either the app writes to a file inside the container (see above), or it buffers its output. Python is a common case: start it with python -u, or set the environment variable PYTHONUNBUFFERED=1, and the lines appear as they are written.

Permission denied on the Docker socket

Your user may not run Docker. Put sudo in front of the command, or add your user to the docker group, which gives root-level access to the host.

Docker logs on a Synology NAS (Container Manager)

Container Manager shows a container's log in its details window. For --tail, -f and grep, use the command line: enable SSH and sign in (see our Synology SSH guide), then run the same commands with sudo:

Terminal
sudo docker logs -f --tail 50 my-container

We checked Container Manager on our own NAS with read-only commands (Docker Engine 24.0.2). Synology does not use json-file. Its default logging driver is db, set in /var/packages/ContainerManager/etc/dockerd.json, and each container's log is a small database file:

Terminal
sudo docker info --format '{{.LoggingDriver}}'
sudo docker inspect --format '{{.LogPath}}' my-container
Code
db
/volume1/@docker/containers/<id>/log.db

docker logs reads it like any other log: --tail, -t and -f all worked on our containers. Because the file is a database, do not empty it with truncate. Recreate the container instead, for example with sudo docker compose up -d --force-recreate in the project folder. To get a message when a container stops, see Docker container notifications on Telegram.

Frequently asked questions

How do I see the last 100 lines of a Docker container's log?

Run docker logs --tail 100 <container> or docker logs -n 100 <container>. Add -f to keep following new lines.

How do I stop docker logs -f without stopping the container?

Press Ctrl+C. It stops only the docker logs command. The container keeps running.

Where are Docker container logs stored?

With the default json-file driver, in /var/lib/docker/containers/<id>/<id>-json.log. docker inspect --format '{{.LogPath}}' <container> prints the exact path. On a Synology NAS, they are in /volume1/@docker/containers/<id>/log.db.

Why does docker logs | grep not find my errors?

The app writes its errors to stderr, which does not go through the pipe. Use docker logs <container> 2>&1 | grep -i error.

How do I limit the size of Docker logs?

Set max-size and max-file for the json-file driver: per container with --log-opt, per service in compose.yaml, or for all new containers in /etc/docker/daemon.json. Or use the local driver, which rotates its files by default.

Does docker logs work on a stopped container?

Yes, until you remove it. docker rm deletes the log with the container.

Yassine

Yassine

Yassine founded IGProd to educate the visitors with technological tutorials. These helpful guides are available for all sorts of professionals and novices, whether they work in technology or not!

Surf freely 💗

Leave a comment

Your email address will not be published. Required fields are marked *

Your experience on this site will be improved by allowing cookies Cookie Policy