LinuxCommandLibrary

docker-compose-logs

View Docker Compose service logs

TLDR

View logs for all services

$ docker compose logs
copy

View logs for a specific service
$ docker compose logs [service_name]
copy

View logs and follow new output (like tail --follow)
$ docker compose logs [[-f|--follow]]
copy

View logs with timestamps
$ docker compose logs [[-t|--timestamps]]
copy

View only the last n lines of logs for each container
$ docker compose logs [[-n|--tail]] [n]
copy

View logs from a specific time onwards
$ docker compose logs --since [timestamp]
copy

View logs until a specific time
$ docker compose logs --until [timestamp]
copy

View logs for multiple specific services
$ docker compose logs [service1 service2 ...]
copy

SYNOPSIS

docker-compose logs [OPTIONS] [SERVICE...]

PARAMETERS

--no-color
    Produces monochrome output, disabling color coding for service names.

-f, --follow
    Follows log output in real-time. This keeps the terminal open and continuously displays new log entries as they are generated by the services.

-t, --timestamps
    Displays timestamps for each log entry, showing when the log message was generated.

--tail=NUM
    Specifies the number of lines to show from the end of the logs. Defaults to "all" if not specified, showing the entire log history. A value of 0 will show only new logs when used with --follow.

--since=TIME
    Shows logs generated since a specified timestamp (e.g., 2023-01-02T13:23:37Z) or a relative duration (e.g., 42m for 42 minutes, 2h for 2 hours, 1d for 1 day).

--until=TIME
    Shows logs generated up to a specified timestamp (e.g., 2023-01-02T13:23:37Z) or a relative duration (e.g., 42m for 42 minutes before now).

DESCRIPTION

The docker-compose logs command is a crucial tool for debugging and monitoring multi-container applications managed by Docker Compose.

It allows users to stream the consolidated standard output and standard error (stdout and stderr) logs from one or more services defined in a docker-compose.yml file. By default, it displays logs from all services in the project, but you can specify individual services to narrow down the output.

This command aggregates logs, making it easier to see the interplay between different parts of your application. It supports options to follow logs in real-time (similar to tail -f), display timestamps, show a specific number of trailing lines, or filter logs by a time range. Unlike docker logs which targets individual containers, docker-compose logs operates at the service level, automatically gathering logs from all containers associated with a given service.

It's an indispensable command for understanding application behavior, diagnosing issues, and observing real-time system activity in a Docker Compose environment.

CAVEATS

  • The command relies on the Docker daemon's log driver configuration. If a service's container is configured with the 'none' log driver, docker-compose logs will not be able to retrieve its output.
  • By default, docker-compose logs looks for a docker-compose.yml file in the current directory. If your configuration file is elsewhere or has a different name, you'll need to specify it using the -f option for docker-compose itself (e.g., docker-compose -f my-app.yml logs).
  • When following logs (-f) from many services or services with high log volume, output can be overwhelming. External tools like grep or less can be piped to filter or navigate the output effectively.
  • The `docker-compose` CLI (often a Python application) is gradually being replaced by `docker compose` (a Docker CLI plugin). While their `logs` command functionality is largely similar, users should be aware of which version they are using.

LOG DRIVERS

Docker Compose logs are retrieved from the Docker daemon's configured log driver for each container. The default driver, json-file, is suitable for `docker-compose logs`. Other drivers like syslog, gelf, or awslogs might direct logs externally, meaning `docker-compose logs` might only show a subset or no logs, depending on the driver's configuration and whether it copies logs to stdout/stderr or only sends them to an external endpoint.

FILTERING AND PAGING OUTPUT

For extensive log output, it's common practice to pipe the output of docker-compose logs to other Linux utilities:

  • Paging: `docker-compose logs | less` allows you to scroll through logs.
  • Filtering: `docker-compose logs | grep "error"` helps in finding specific patterns or messages.
  • Combining: `docker-compose logs -f | grep "service_name"` can follow logs and filter in real-time for messages from a specific service (even if not directly specified in the command arguments).

COLOR CODING

By default, docker-compose logs color-codes the output based on the service name, making it easier to distinguish between logs from different services in a combined stream. The --no-color option disables this behavior.

HISTORY

The docker-compose logs command has been a core feature of Docker Compose since its inception. Docker Compose itself originated as 'Fig', a tool developed to simplify multi-container Docker application definitions. After being acquired by Docker, Inc., Fig was rebranded as Docker Compose. The `logs` functionality has always been pivotal for developers and operators to gain insights into their multi-service applications, allowing for quick debugging and monitoring of the orchestrated containers. Its consistent behavior across various versions of Docker Compose highlights its fundamental role in the Docker ecosystem.

SEE ALSO

docker(1), docker-compose(1), docker-compose up(1), docker-compose ps(1), docker logs(1), tail(1), grep(1)

Copied to clipboard