QA Interview Hub · Knowledge base · Quizzes · Українська

Logs and observability › Tools and queries

The command line: docker logs, kubectl logs, grep, jq

A container writes its log to stdout and stderr, and docker logs and kubectl logs read this stored stream. So a log written to a file inside the container is not shown. In Kubernetes a restart creates a new container with an empty log, so for CrashLoopBackOff the cause is found with kubectl logs --previous and kubectl describe. JSON logs are filtered with jq.

Logs · Terminal

A pod is in CrashLoopBackOff and kubectl logs is empty. Where is the cause of the crash?

A test environment comes up, the service crashes and restarts, and kubectl logs shows nothing or only the start. Kibana or Grafana is not yet configured for this stand, but cluster access exists. Logs of a local environment, a stand and CI are read in a terminal, and a few commands known by heart save a lot of time. This lesson covers Docker, Kubernetes, grep, jq and PowerShell, and why commands are sometimes silent.

How it works and why. A container writes its log to stdout and stderr, and the runtime (Docker, Kubernetes) captures these streams and stores them. docker logs and kubectl logs read exactly this stored container stream. This is a simple model from which the rest follows: if an application writes its log to a file inside the container, docker logs is empty; if a container restarted, the current instance starts with a clean stream; if a pod is deleted, its logs disappear with it, leaving only what an agent managed to ship to a central store.

Docker:

docker logs -f --tail 100 api            # follow, last 100 lines
docker logs --since 10m api              # for the last 10 minutes
docker compose logs -f --tail=50 api db  # several compose services
docker compose logs --no-color --timestamps > compose.log

Kubernetes:

kubectl logs deploy/checkout --since=15m
kubectl logs -f pod/checkout-7d9f -c app      # a specific container
kubectl logs pod/checkout-7d9f --previous     # the container that crashed
kubectl logs -l app=checkout --tail=100 --prefix   # by label, with pod names

Why docker logs is empty

Wrong

A log into a file inside the container

The runtime sees only stdout and stderr. docker logs is empty, and the file has to be read through docker exec.

Right

A log to stdout

The stream is collected by the runtime and an agent into a central store and read with docker logs and kubectl logs.

Figure 1. Console output in a container is a contract: everything that must be visible from outside goes to stdout.

Example: CrashLoopBackOff. A service starts, crashes within a second, Kubernetes restarts it, the status is CrashLoopBackOff. The current container has just been created and its log is empty or cut off. The cause of the crash is in the previous instance, and --previous shows it:

kubectl get pods -l app=checkout          # status and restart count
kubectl logs pod/checkout-7d9f --previous # the log of the crashed container
kubectl describe pod checkout-7d9f        # events: OOMKilled, probe failed

If --previous shows an exception with a configuration error text, the bug is in environment variables or secrets of the stand. If there is no log at all and describe says OOMKilled, the container was killed for memory before it wrote anything. Two commands thus tell an application error from environment limits. For a deployment with several pods the kubectl logs deploy/… command takes only one pod, so use a label (-l) for the full picture.

Files and text.

tail -f app.log | grep --line-buffered ERROR   # a live error stream
grep -n -C 3 "orderId=8841" app.log            # 3 lines of context
grep -c "ERROR" app.log                        # a count
zgrep "timeout" app.log.2026-10-11.gz          # in a compressed archive

JSON logs with jq.

jq -c 'select(.level=="error")' app.log
jq -r 'select(.traceId=="4bf92f35...") | [.ts,.service,.msg] | @tsv' app.log
jq -s 'group_by(.error) | map({error: .[0].error, n: length})' app.log
# a mixed file: skip non-JSON lines
jq -R 'fromjson? | select(.level=="error")' mixed.log

PowerShell. The analogue of grep is Select-String, of tail -f is Get-Content -Wait -Tail:

Select-String -Path app.log -Pattern "ERROR" -Context 3
Get-Content app.log -Wait -Tail 50
Get-Content app.log | ForEach-Object { $_ | ConvertFrom-Json } |
  Where-Object level -eq 'error'

Limits and alternatives. A terminal is good for quick pinpoint checks but not for history: Kubernetes keeps only what lies on the node, and log rotation trims old data. For several pods at once a terminal is inconvenient, while a central store gives search across all services. Commands with -f and tail -f hold the connection and can hang in a CI script, so scripts use --tail or --since without -f.

Typical mistakes.

  • Looking at the current container’s log after a restart: the cause of the crash is in --previous.
  • grep in a pipeline with tail -f without --line-buffered: output arrives late in batches.
  • jq on a file with mixed lines without -R and fromjson?: a parse error at the first non-JSON line.
  • Pasting the result into a bug report without checking for secrets.

Check yourself

A pod is in CrashLoopBackOff and kubectl logs is empty. Two commands that will show the cause.

Answer

kubectl logs pod/… --previous shows the log of the previous, crashed container. kubectl describe pod/… shows events and the termination reason (OOMKilled, readiness probe failures, ImagePull). Together they tell an application error from an environment limit.

jq -c 'select(.level=="error")' app.log ends with a parse error. The file has lines that are not JSON. How do you fix it?

Answer

Read lines as text and parse only those that are JSON: jq -R 'fromjson? | select(.level=="error")' app.log. The -R flag feeds each line as a string, and fromjson? silently skips those that do not parse.

In an interview. Junior: For Docker I use docker logs with -f, --tail and --since, for Kubernetes kubectl logs, I search with grep and parse JSON with jq. Middle/Senior: I understand the model: the runtime keeps a container’s stdout and stderr, so a log in a file is invisible, and after a restart I look for the cause through --previous and describe. For several pods I use a label, and in CI scripts I do not leave -f. For history and search across all services I switch to a central store.

More articles in this chapter