متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 25Containers · Docker

How to see the logs of a container

A container crashed, an API returns 500, a job did not finish — the first place to look is the container's log. Docker keeps everything the main process writes to stdout and stderr. This how-to shows how to read and follow logs, filter by time, see the logs of a container that already stopped, find out why it stopped, and keep logs from filling the disk.

  • 5 الدروس
  • 6 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

25.1

The quick answer

docker logs --tail 100 api
docker logs -f --since 10m api
docker logs -t api 2>&1 | grep -i error
docker compose logs -f --tail 50 api

-f follows new lines (Ctrl+C to stop), --tail shows only the last lines, -t adds time stamps.

25.2

Step by step

  1. Find the container — also stopped ones:
docker ps -a --filter name=api
CONTAINER ID   IMAGE                                  STATUS                       NAMES
8d2e4f1a9b3c   registry.example.com/shop/api:1.4.2    Exited (137) 3 minutes ago   api
  1. Read the exit code — it often tells you the reason before the log does:
Exit codeUsual meaning
0The process ended normally (a job finished, or the command was not a long-running server).
1App error — read the log.
137Killed with SIGKILL — very often out of memory (--memory limit) or docker kill.
139Segmentation fault (crash in native code).
143Stopped with SIGTERM (docker stop) — normal.
docker inspect api --format '{{.State.ExitCode}} OOMKilled={{.State.OOMKilled}} {{.State.FinishedAt}}'
137 OOMKilled=true 2026-11-17T09:41:12Z
  1. Filter by time (incidents have a time):
docker logs --since 2026-11-17T09:30:00 --until 2026-11-17T09:45:00 api
docker logs --since 1h api 2>&1 | grep -ciE 'error|exception'

2>&1 is needed for grep, because many apps write errors to stderr.

  1. Logs of a restarting container (crash loop): look at the previous run's last lines and the restart count:
docker inspect api --format '{{.RestartCount}}'
docker logs --tail 30 api
docker events --filter container=api --since 30m
  1. Where are the log files, and how big? (with the default json-file driver):
docker inspect api --format '{{.LogPath}}'
du -h /var/lib/docker/containers/*/*-json.log | sort -h | tail -5

Set rotation for all containers in /etc/docker/daemon.json ("log-opts": {"max-size": "10m", "max-file": "3"}); it applies to new containers. For one container: docker run --log-opt max-size=10m --log-opt max-file=3 ….

  1. The app writes to a file inside the container? Then docker logs is empty. Better: make the app log to stdout. Until then:
docker exec api tail -n 50 /app/logs/app.log

25.3

Check that it worked

You found the reason (log line or exit code). After the fix (more memory, fixed config), the container stays Up and docker inspect … RestartCount does not grow any more.

الأوامر في هذا الدرس

25.4

Common problems

ProblemCauseFix
docker logs shows nothingThe app logs to a file, not to stdout/stderr.Log to stdout; or docker exec … tail the file.
grep finds nothing, but errors are on screenErrors go to stderr.Add 2>&1 before the pipe.
Exit code 137, no error in the logOOM kill — the process had no time to log.docker inspect (OOMKilled); raise --memory or fix the leak.
Logs are goneThe container was removed (docker rm, --rm).Send logs to a central system (Loki, ELK); do not rely on local logs.
Disk full by *-json.logNo log rotation.max-size/max-file in daemon.json, recreate the containers.
configured logging driver does not support readingThe container uses syslog/gelf etc.Read them in that system; with journald: journalctl CONTAINER_NAME=api.

25.5

Go further

1A container stopped with exit code 137 and an empty log. What do you check first?

docker inspect for OOMKilled=true — the memory limit killed it.

2Why does docker logs api | grep error miss lines?

Errors are on stderr; use docker logs api 2>&1 | grep error.

كل الأوامر في هذا الفصل