# 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.

URL: https://levelrail.com/learn/deploy-operate/read-container-logs/
Updated: 2026-11-27

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

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| `Error response from daemon: No such container: web` | Wrong name, or the container was removed | Run `docker ps -a` and use the exact name |
| Empty output from a running container | App writes to a file, not stdout or stderr | Log to stdout, or read the file with `docker exec` |
| `docker logs` prints an error about the logging driver | Driver does not support reading logs back | Read logs from the driver's destination, or switch to `json-file` |
| Old lines are gone | Rotation deleted them, or the container was recreated | Expected; raise `max-file` if you need more history |
| Disk is full and `/var/lib/docker` is big | No `max-size` on `json-file` | Set limits, recreate containers, see [Limit Docker log size](https://levelrail.com/learn/deploy-operate/limit-docker-log-size/)) |
| `docker compose logs` shows nothing | Wrong folder or project name | `cd` to the project, or pass `-p` and `-f` for the right project |
| Lines arrive late with `-f` | App output is buffered | Disable buffering in the app runtime |
| `docker compose logs` is slow and huge | No `--tail` on a long-running service | Add `--tail 200` |
| Timestamps look wrong by hours | `-t` shows UTC, the app logs local time | Compare 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

| Goal | Command |
| --- | --- |
| Last 100 lines | `docker logs --tail 100 NAME` |
| Last 10 minutes | `docker logs --since 10m NAME` |
| Follow live with times | `docker logs -f -t NAME` |
| One Compose service | `docker compose logs -f --tail 100 web` |
| Include errors in grep | `docker logs NAME 2>&1 \| grep -i error` |
| Show log settings | `docker inspect --format '{{.HostConfig.LogConfig}}' NAME` |

## Related lessons

- [Limit Docker log size](https://levelrail.com/learn/deploy-operate/limit-docker-log-size/))
- [Fix a 502 Bad Gateway](https://levelrail.com/learn/deploy-operate/fix-502-bad-gateway/))
- [Read logs with journalctl](https://levelrail.com/learn/server-basics/read-logs-with-journalctl/))

## Next

Automate how changes reach your server: [What is CI/CD?](https://levelrail.com/learn/deploy-operate/what-is-ci-cd/)).

## 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

- [Docker docs, docker container logs](https://docs.docker.com/reference/cli/docker/container/logs/)
- [Docker docs, json-file logging driver](https://docs.docker.com/engine/logging/drivers/json-file/)
