Terminus by Warp
Output Logs in Docker Compose

Output Logs in Docker Compose

Razvan Ludosanu
Razvan Ludosanu
Founder, learnbackend.dev

The short answer

In Docker Compose, to output the logs of all the containers related to all the services defined in your [.inline-code] compose.yaml[.inline-code]  file at once, you can use the [.inline-code] docker compose logs[.inline-code]  command without arguments:

 $ docker compose logs

For example, considering the following [.inline-code] compose.yaml[.inline-code] file:

 version: '3'

services:
  website:
    build: .
    ports:
      - "3000:3000"
  api:
    build: .
    ports:
      - "3001:3001"

This command will output all the logs of both the [.inline-code] website[.inline-code]  and [.inline-code] api[.inline-code] services:

 $ docker compose logs
website-1 | [INFO] Starting website server …
api-1 | [INFO] Starting api server …
website-1 | [INFO] New client login attempt 
api-1 | [ERROR] Credentials not found

[#view-the-logs-of-specific-services] Viewing the logs of a specific service [#view-the-logs-of-specific-services]

To output the logs of one or more specific services, you can use the [.inline-code]docker compose logs[.inline-code] command as follows:

 $ docker compose logs <service_name …>

Where [.inline-code] service_name …[.inline-code]  is a list of service names separated by a space character.

For example, this command will only output the logs of the containers related to the [.inline-code] api[.inline-code]  service:

 $ docker compose logs api
api-1 | [INFO] Starting api server …
api-1 | [ERROR] Credentials not found

You can learn more about container logs by reading our article on how to monitor the logs of single containers in Docker.

[#customize-the-logs-output] Customizing the logs output [#customize-the-logs-output]

To output the logs of containers without including the name of their related services, which helps making the output cleaner and easier to parse, you can use the [.inline-code] --no-log-prefix[.inline-code]  flag:

 $ docker compose logs --no-log-prefix [<service_name> …]

For example, this command will only output the logs generated by the containers without the names of the related services:

 $ docker compose logs --no-log-prefix
[INFO] Starting website server …
[INFO] Starting api server …
[INFO] New client login attempt 
[ERROR] Credentials not found

[#include-the-logs-timestamps] Including the logs timestamp in the output [#include-the-logs-timestamps]

To output the logs of containers including the timestamp at which they were generated, you can use the [.inline-code] -t[.inline-code]  flag (short for [.inline-code] --timestamps[.inline-code] ):

 $ docker compose logs -t [<service_name> …]

For example, this command will include the timestamp of every single log line:

 $ docker compose logs -t
website-1 | 2024-02-23T13:40:41.928346630Z  [INFO] Starting website server …
api-1 | 2024-02-23T13:41:32.628446430Z [INFO] Starting api server …
website-1 | 2024-02-23T14:12:20.928346630Z [INFO] New client login attempt 
api-1 | 2024-02-23T14:13:30.928346630Z [ERROR] Credentials not found

[#view-the-logs-real-time] Displaying service logs in real-time [#view-the-logs-real-time]

To monitor the logs of the containers related to one or more services in real-time, you can use the [.inline-code] docker compose logs[.inline-code]  command with the [.inline-code] -f[.inline-code]  flag (short for [.inline-code] --follow)[.inline-code] :

 $ docker compose logs -f [<service_name …>]

Note that this command will not only output the new logs generated by the containers in real-time, but also the previous ones. You can learn more about filtering these logs in the following sections.

[#easily-recall-syntax-with-ai] Easily retrieve this command using Warp’s AI Command Suggestions [#easily-recall-syntax-with-ai]

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:

For example, entering [.inline-code] docker compose logs real-time[.inline-code]  in the AI Command Suggestions will prompt a [.inline-code] docker[.inline-code]  command that can then quickly be inserted into your shell by doing [.inline-code] CMD+ENTER[.inline-code] .

[#filter-logs] Filtering container logs [#filter-logs]

In Docker Compose, container logs can be filtered in several ways.

[#output-the-last-n-lines] Outputting the last N logs [#output-the-last-n-lines]

To show the latest log entries, you can use the [.inline-code] -n[.inline-code]  flag (short for [.inline-code] --tail[.inline-code] ) followed by the number of logs to display:

 $ docker compose logs -n <number> [<service_name …>]

For example, the following command shows the latest 80 logs of the containers related to the [.inline-code] api[.inline-code] service:

 $ docker compose logs -n 80 api

[#filter-logs-by-date-and-time] Filtering logs by date and time [#filter-logs-by-date-and-time]

To filter container logs based on timeframes, you can either use the [.inline-code] --since[.inline-code]  flag to fetch the logs that occurred after a specific time:

 $ docker compose logs --since <time> [<service_name …>]

Or you can use the [.inline-code] --until[.inline-code]  flag to fetch the logs that occurred before a specific time:

 $ docker compose logs --until <time> [<service_name …>]

Where [.inline-code] time[.inline-code]  is either:

  • A timestamp expressed in the following format: [.inline-code] YYYY-MM-DDTHH:MM:SS.NNNNNNZ[.inline-code] .
  • A relative duration expressed in the following format: [.inline-code] <number>[h|m|s][.inline-code]  where [.inline-code] h[.inline-code] , [.inline-code] m[.inline-code] , [.inline-code] s[.inline-code]  stand for hours, minutes, and seconds.

For example, this command will output all the logs generated in the last hour:

 $ docker compose logs --since="1h"

And this command will output all the logs generated until January 2nd, 2024:

 $ docker compose logs --until="2024-01-02"

[#filter-logs-with-patterns] Filtering logs based on patterns using the [.inline-code] grep[.inline-code] command [#filter-logs-with-patterns]

To filter container logs based on specific keywords or patterns, you can pipe the output of the [.inline-code] docker compose logs[.inline-code]  command into the [.inline-code] grep[.inline-code]  command as follows:

 $ docker compose logs [<service_name …>] | grep '<pattern>'

For instance, this command will output all the logs of the containers related to the [.inline-code] api[.inline-code]  service containing the [.inline-code] ERROR[.inline-code] keyword:

 $ docker compose logs api | grep 'ERROR'
api-1 | [ERROR] Credentials not found

You can learn more filtering methods by reading our article on how to filter the output of commands with grep and awk.

Experience the power of Warp

  • Write with an IDE-style editor
  • Easily navigate through output
  • Save commands to reuse later
  • Ask Warp AI to explain or debug
  • Customize keybindings and launch configs
  • Pick from preloaded themes or design your own
brew install --cask warp
Copied!
Join the Windows waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.
Join the Linux waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.
Join the Linux waitlist or join the Windows waitlist
Join the Windows waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.