LinuxCommandLibrary

docker-logs

View a container's logs

TLDR

Print logs from a container

$ docker logs [container_name]
copy

Print logs and follow them
$ docker logs [[-f|--follow]] [container_name]
copy

Print last 5 lines
$ docker logs [container_name] [[-n|--tail]] [5]
copy

Print logs and append them with timestamps
$ docker logs [[-t|--timestamps]] [container_name]
copy

Print logs from a certain point in time of container execution (i.e. 23m, 10s, 2013-01-02T13:23:37)
$ docker logs [container_name] --until [time]
copy

SYNOPSIS

docker logs [OPTIONS] CONTAINER

PARAMETERS

CONTAINER
    The name or ID of the Docker container whose logs are to be fetched.

--details
    Show extra details provided to the logs, such as environment variables or labels.

-f, --follow
    Follow log output. This streams new logs as they are generated, similar to tail -f.

--since string
    Show logs only since a specific timestamp (e.g., '2013-01-01T13:23:37') or relative duration (e.g., '42m' for 42 minutes ago).

--tail string
    Number of lines to show from the end of the logs (e.g., '100' for the last 100 lines). By default, 'all' lines are shown.

-t, --timestamps
    Show timestamps for each log entry.

--until string
    Show logs only until a specific timestamp (e.g., '2013-01-01T13:23:37') or relative duration (e.g., '1h' for 1 hour ago).

DESCRIPTION

The docker logs command is a fundamental utility for interacting with Docker containers, allowing users to retrieve and display the output generated by a running or stopped container. It captures all data written to the container's STDOUT and STDERR streams. This command is invaluable for debugging applications, monitoring container health, and understanding the behavior of services within the Docker environment.

By default, Docker containers often use the json-file logging driver, which stores log data on the host filesystem. docker logs can display historical logs since the container's creation or stream new log entries in real-time, similar to the tail -f command. It supports filtering by timestamps, showing only recent entries, and can display full log lines without truncation. This capability makes docker logs an essential tool for operations, development, and troubleshooting in any Dockerized application stack.

CAVEATS

docker logs relies on the container's configured logging driver. It works best with local drivers such as json-file (default) or local. If a container is configured to use an external logging driver (e.g., syslog, fluentd, awslogs), docker logs may not display any output, as logs are streamed directly to the external service. Furthermore, retrieving logs from containers with extremely large log files or during high I/O can be resource-intensive. Log rotation settings can also affect the availability of historical log data.

LOG DRIVERS

Docker's logging drivers determine where container logs are sent. While docker logs primarily works with json-file and local drivers that store logs on the host, other drivers like syslog, fluentd, or awslogs direct logs to external services. Understanding the configured log driver is crucial when using docker logs to retrieve container output.

FILTERING AND PAGING

For more advanced log analysis, the output of docker logs can be piped to other standard Linux utilities. For instance, docker logs CONTAINER | grep "error" can filter specific messages, and docker logs CONTAINER | less allows for interactive paging and searching through extensive log histories.

HISTORY

The docker logs command has been an integral part of the Docker CLI since its early releases. Its functionality has remained consistently core to managing and debugging containers, focusing on retrieving STDOUT and STDERR streams. Over time, options like --since, --tail, and --until were added to enhance its capabilities for filtering and viewing specific log segments, making it more robust for diverse operational needs.

SEE ALSO

tail(1), cat(1), grep(1), docker inspect(1), docker run(1)

Copied to clipboard