Home DockerDocker Logs: View, Rotate and Manage Container Logs

Docker Logs: View, Rotate and Manage Container Logs

by Knowledge Base Linux
Docker logs streaming from containers into a viewing console

When a container crashes, returns an error, or behaves differently in production than it did on your laptop, docker logs is usually the first command you reach for. It shows everything the container process wrote to standard output and standard error, from startup messages to stack traces, without requiring you to enter the container or install extra tooling. Used well, it turns a vague failure into a specific cause in seconds.

Docker logs rotation cycle with old log files archived and deleted

The challenge is that container logs grow quickly. A busy web server, a verbose application in debug mode, or a worker stuck in a retry loop can generate gigabytes of output, and Docker keeps that output on the host by default. Without rotation, a single noisy container can fill a disk, stop other containers, and eventually break the host itself. This guide covers the full lifecycle: reading logs efficiently, rotating them at the daemon and container level, working with stopped containers, reclaiming disk space safely, and centralizing logs when you outgrow a single host.

Prerequisites

You need Docker installed and running, plus a container you can inspect. The examples use a container named web, but you can substitute any container name or ID from docker ps. You also need sudo or membership in the docker group to run Docker commands, and access to the Docker daemon configuration file if you plan to change global logging defaults. Changing daemon-wide settings restarts the Docker service, so plan that change for a maintenance window if you run production workloads on the same host.

It helps to know whether your containers were started with docker run or with Compose, because rotation settings live in different places for each method. Daemon defaults apply to new containers, per-container flags override the daemon for a single container, and Compose logging options apply when the stack is created. Understanding that precedence prevents the common confusion where a daemon change appears to have no effect on containers that already exist.

Essential docker logs Commands and Flags

The basic command prints the full log history for a container. For a long-running service, that output can be huge, so you will almost always combine it with flags that limit or stream the output.

docker logs web
docker logs --tail 100 web
docker logs --follow web
docker logs --timestamps web

The table below summarises the flags you will use most often. Each flag can be combined with the others, so a typical debugging command streams only recent, timestamped output.

Flag Purpose Example
–follow, -f Stream new log lines as they appear docker logs -f web
–tail, -n Show only the last N lines docker logs –tail 200 web
–since Show logs after a time or relative duration docker logs –since 1h web
–until Show logs before a time or relative duration docker logs –until 30m web
–timestamps, -t Prefix each line with an RFC3339 timestamp docker logs -t web

A few practical combinations cover most debugging sessions. Use docker logs –tail 100 -f web to watch recent output live, docker logs –since 2026-10-28T00:00:00Z web to inspect logs after a deployment, and docker logs -t –tail 500 web when you need to correlate container events with host metrics or an incident timeline. Press Ctrl+C to stop a followed stream; that stops the log viewer, not the container.

When output is long, pipe it through standard tools rather than scrolling in a terminal. For example, docker logs web 2>&1 | grep -i error filters for errors, while redirecting output to a file preserves evidence for an incident report. Remember that docker logs reads from the logging driver, so the exact behaviour depends on the driver configured for that container, which is covered in the next section.

Understanding the Default Json-File Driver

Unless you configure something else, Docker uses the json-file logging driver. Each container log line is stored as a JSON object on the host, under the Docker data directory, in a file named after the container ID. This is why docker logs works immediately with no extra setup, and why logs survive a container restart: restarting a container does not delete its log file.

The important limitation is that json-file has no rotation by default. The log file keeps growing until you configure a maximum size and file count, or until the container is removed. That default is convenient for short-lived development containers, but it is dangerous for production services that run for weeks or months. Always set rotation for long-lived containers, even if disk usage looks fine today.

You can check which driver a container uses with docker inspect. Look for the LogConfig section, which shows the driver type and any options applied at creation time. If the driver is not json-file, some docker logs features may behave differently, and rotation options may not apply in the same way.

docker inspect web --format '{{.HostConfig.LogConfig.Type}}'
docker inspect web --format '{{json .HostConfig.LogConfig.Config}}'

Configuring Log Rotation in daemon.json

The cleanest way to protect a host is to set daemon-wide defaults in /etc/docker/daemon.json. New containers inherit these settings unless they override them explicitly. The example below limits each log file to 10 megabytes and keeps at most three rotated files per container, for a maximum of about 30 megabytes of logs per container.

sudo nano /etc/docker/daemon.json
{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  }
}

After saving the file, validate the JSON syntax and restart the Docker daemon. Be aware that restarting Docker stops running containers unless live-restore is enabled, so schedule this change carefully on a production host.

sudo systemctl restart docker
docker info --format '{{.LoggingDriver}}'

Crucially, daemon defaults only affect containers created after the restart. Existing containers keep the settings they were created with, so you must recreate them to pick up the new limits. This is the most common reason why a daemon.json change appears not to work: the old container is still running with the old configuration.

Per-Container and Compose Rotation

For a single container started with docker run, pass log options directly on the command line. These options override the daemon defaults for that container only, which is useful when one service is noisier than the rest and needs a larger allowance.

docker run -d --name web \
  --log-driver json-file \
  --log-opt max-size=10m \
  --log-opt max-file=3 \
  nginx

For a Compose stack, set the same options under the logging key for each service. The example below applies json-file rotation to a web service, and you should apply similar settings to workers, databases, and any other long-lived service in the stack. For related Compose patterns, see the guide to Docker Compose profiles and stacks, and for container security checks, see the guide to Docker image scanning with Trivy.

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

After changing Compose logging options, recreate the affected services with docker compose up -d so the new containers pick up the settings. Verify with docker inspect, and confirm that the old containers, with their unrotated logs, are gone. Choosing sensible limits depends on your workload: a quiet internal service may be fine with 10 megabytes and three files, while a busy API service may need larger files or more rotations to preserve enough history for debugging.

Using docker logs for Stopped Containers

A stopped container is not a deleted container, and that distinction matters for debugging. If a container exits, crashes, or is stopped manually, its logs remain available as long as the container still exists. List all containers, including stopped ones, find the container you need, and read its logs exactly as you would for a running container.

docker ps -a
docker logs --tail 200 web
docker logs --timestamps web | tail -n 100

This workflow is especially useful for crash loops, where a container starts, fails, and exits before you can attach to it. The exit code from docker ps -a, combined with the last lines of the log, usually reveals whether the failure was a configuration error, a missing environment variable, a failed database connection, or a permission problem. If the container was removed with docker rm, its json-file logs are gone too, so avoid removing a crashed container until you have captured the logs you need.

For containers that restart automatically, check both the current logs and the restart count. A high restart count with the same error repeated in the logs points to a persistent configuration problem, not a transient failure, and fixing the configuration is more effective than repeatedly restarting the container.

Troubleshooting Disk-Full Problems

When a disk fills up, Docker itself may start failing, and containers can become unresponsive. Start by checking overall Docker disk usage and then identifying the largest container log files. The json-file logs live under the Docker containers directory, and a single file that is gigabytes in size is a clear sign that rotation was never configured.

docker system df
du -sh /var/lib/docker/containers/*/*-json.log 2>/dev/null | sort -rh | head

Do not simply delete a live container log file and expect the problem to be solved. The Docker daemon may still hold the file open, so disk space may not be reclaimed until the container is recreated. The safe fix is to set rotation in daemon.json or Compose, recreate the affected containers, and then remove the old log files along with the old containers. For containers you cannot recreate immediately, stopping the container, truncating its log file, and starting it again is safer than deleting the file while the container runs, but rotation should still be configured afterwards to prevent a recurrence.

Note that docker system prune does not delete logs for containers that still exist, so it will not rescue a host whose problem is unrotated json-file logs. Pruning removes stopped containers, unused images, and unused volumes, which helps with general disk pressure, but log rotation is a separate fix. After reclaiming space, monitor disk usage for a few days to confirm that logs are now capped at the expected size.

Other disk-full symptoms have different causes. If docker logs returns nothing for a container that should be logging, check whether the application writes to a file inside the container instead of standard output, or whether it uses a logging driver that does not store logs locally. Applications that log only to files need a volume or a log shipper to make those files visible on the host.

Centralizing Logs Briefly

On a single host, docker logs plus rotation is enough. Once you run multiple hosts, or you need to search logs across services, correlate events, or retain logs after containers are deleted, centralization becomes worthwhile. Docker supports drivers such as syslog, gelf, and fluentd that forward logs to a collector instead of storing them only as local json files. Alternatively, you can keep json-file locally and run a shipper such as Filebeat or Fluent Bit that reads the log files and forwards them to Elasticsearch, Loki, or another central store.

The trade-off is important: when logs are forwarded with a driver that does not cache locally, docker logs may no longer show the full history on the host. Many teams therefore keep json-file with rotation for local debugging and add a shipper for central search, getting both fast local access and durable central retention. For full details on available drivers and their options, see the official Docker logging documentation.

Conclusion

Effective container logging is mostly about discipline, not tooling. Learn the core docker logs flags so you can find the right lines quickly, set json-file rotation in daemon.json and Compose before disk pressure forces the issue, and remember that stopped containers still hold their logs until they are removed. When a single host is no longer enough, centralize with a shipper or a forwarding driver, but keep local rotation in place. With those habits, logs stay a debugging asset instead of becoming the outage itself.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.