Chapter 4 · Lesson 7 of 18
Deploy and operate

Read container logs

Container logs are the text a containerized app writes to standard output and standard error, which Docker stores and lets you read.

By the end you will

Read, filter and follow container logs with docker logs and docker compose logs.

Time
15 min
Level
Beginner
Check
4 questions
You need
  • A running container or Compose app

Your site returns errors and the dashboard says nothing useful. The answer is almost always a line the app printed a few seconds ago. Knowing how to read, narrow and follow logs turns a vague outage into a specific error message.

How it works#

Inside a container, the main process writes to two streams: standard output (stdout) and standard error (stderr). Docker’s logging driver captures both. With the default json-file driver, Docker writes each line to a JSON file on the host, under the Docker data directory, with a timestamp and the stream name. docker logs reads that file back for you.

Three consequences follow. First, only what goes to stdout or stderr is captured, so an app that writes to /var/log/app.log inside the container shows nothing. Second, the log belongs to the container: remove the container and its log is gone. Third, json-file has no size limit by default, so a chatty app can fill the disk over weeks.

Other drivers (such as syslog, journald or none) send logs elsewhere. Depending on the driver, docker logs may not work, and you read the logs where they were sent.

Step 1: Read the logs#

bash
docker logs my-container

Find the container name with docker ps.

What you should see: the full log of that container from its start, which can be long. Use the next step to narrow it.

Step 2: Narrow and follow#

bash
docker logs --tail 100 my-container
docker logs --since 10m my-container
docker logs -t my-container
docker logs -f my-container
  • --tail 100 shows the last 100 lines.
  • --since 10m shows only the last ten minutes. It also accepts a timestamp.
  • -t adds a timestamp to each line.
  • -f follows new lines live. Press Ctrl+C to stop.

You can combine them: docker logs -f --tail 50 -t my-container.

What you should see: only the requested lines. With -f the command stays running and prints each new line as it arrives.

Step 3: Use Compose#

bash
docker compose logs
docker compose logs -f --tail 100 web

Naming a service (web) limits output to it. Without a name you see every service, each line prefixed by the service name. Run it from the folder that holds compose.yaml.

What you should see: lines like web-1 | GET /health 200, with the prefix showing which container wrote them.

Step 4: Limit log size#

Rotation stops logs from growing forever. In compose.yaml:

yaml
services:
  web:
    image: myapp:latest
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"

This keeps up to 3 files of 10 MB each per container. Recreate the container for it to apply: docker compose up -d.

What you should see: Compose prints that the service is recreated. The first-time Recreated or Started line confirms the new settings are in use.

Check it worked#

  • docker logs --tail 5 my-container prints recent lines.
  • docker inspect --format '{{.HostConfig.LogConfig}}' my-container shows the driver and your options, for example {json-file map[max-file:3 max-size:10m]}.

Edge cases and gotchas#

  • Output can be mixed. docker logs replays stdout and stderr on the matching streams, so docker logs my-container | grep error misses stderr lines. Add 2>&1: docker logs my-container 2>&1 | grep error.
  • Recreating a container deletes its logs. docker compose up -d after an image update replaces the container. Read the logs of a failing release before you redeploy, or ship them elsewhere.
  • A restart loop hides the real error. If a container restarts every few seconds, docker logs --tail 50 shows the latest start only. The first crash line may be just above it.
  • Apps that buffer output look silent. Some runtimes buffer stdout when not attached to a terminal, so lines appear late. Look for an unbuffered or line-buffered setting in your language runtime.
  • Time zones differ. Docker timestamps are in UTC. Your app may log in local time. Mind this when you compare lines with a user report.
  • Limits do not apply retroactively. A changed max-size affects only containers created afterwards.
  • Large logs are slow. docker logs on a huge file with no --tail can take a long time and flood the terminal.

Troubleshooting#

SymptomLikely causeFix
Error response from daemon: No such container: webWrong name, or the container was removedRun docker ps -a and use the exact name
Empty output from a running containerApp writes to a file, not stdout or stderrLog to stdout, or read the file with docker exec
docker logs prints an error about the logging driverDriver does not support reading logs backRead logs from the driver’s destination, or switch to json-file
Old lines are goneRotation deleted them, or the container was recreatedExpected; raise max-file if you need more history
Disk is full and /var/lib/docker is bigNo max-size on json-fileSet limits, recreate containers, see Limit Docker log size
docker compose logs shows nothingWrong folder or project namecd to the project, or pass -p and -f for the right project
Lines arrive late with -fApp output is bufferedDisable buffering in the app runtime
docker compose logs is slow and hugeNo --tail on a long-running serviceAdd --tail 200
Timestamps look wrong by hours-t shows UTC, the app logs local timeCompare in one time zone

Real-world scenario#

A customer reports that saving a profile fails. You do not know which service handles it, so you start with the whole stack for the last few minutes:

bash
docker compose logs --since 10m -t 2>&1 | grep -i error

One line stands out: api-1 | error: connection to database refused. You follow only that service while you reproduce the problem:

bash
docker compose logs -f --tail 20 api

The log shows the connection attempts failing. docker compose ps then shows the database container restarting. You read its logs, find that it ran out of disk space, free the space, and the API reconnects by itself. Total time: a few minutes, and no guessing, because the logs named the failing dependency.

Quick reference#

GoalCommand
Last 100 linesdocker logs --tail 100 NAME
Last 10 minutesdocker logs --since 10m NAME
Follow live with timesdocker logs -f -t NAME
One Compose servicedocker compose logs -f --tail 100 web
Include errors in grepdocker logs NAME 2>&1 | grep -i error
Show log settingsdocker inspect --format '{{.HostConfig.LogConfig}}' NAME

Next#

Automate how changes reach your server: What is CI/CD?.

Try it: Read the logs of a container

Find the container, read recent lines, follow them and check the rotation settings.

practice, nothing runs on your machine

Want a real machine? Free browser labs: Killercoda

Check what you learned4 questions
1Your docker logs output is empty, but the app writes to a file inside the container. Why?
2Which command shows only the last ten minutes and keeps following new lines?
3What does max-size: 10m with max-file: 3 keep per container?
4You change max-size in compose.yaml but the old container still grows. What fixes it?

Frequently asked questions

Why are my logs empty?

Docker only captures what the app writes to stdout and stderr. If the app writes to a file inside the container, it will not show in docker logs.

Can logs fill my disk?

Yes. With the default json-file driver there is no size limit unless you set one, so configure max-size and max-file.

Why did my logs disappear after I recreated the container?

Logs belong to the container. When you remove and recreate it, for example with docker compose up after an image change, the old log goes with the old container.

Why does docker logs say nothing is available?

Some logging drivers send logs elsewhere and docker logs cannot read them back. Check the driver with docker inspect and read the logs from where they were sent.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios