0
0
DockerHow-ToBeginner · 3 min read

How to Use Docker Compose Logs: Syntax and Examples

Use docker compose logs to view logs from containers managed by Docker Compose. You can add options like -f to follow logs live or specify service names to see logs for specific containers.
📐

Syntax

The basic syntax of the docker compose logs command is:

  • docker compose logs [options] [SERVICE...]

Here:

  • options are flags to control output (like following logs live).
  • SERVICE is the name of one or more services defined in your docker-compose.yml file to filter logs.
bash
docker compose logs [options] [SERVICE...]
💻

Example

This example shows how to view logs for all services and then follow logs live for a specific service named web.

bash
docker compose logs

docker compose logs -f web
Output
Attaching to web_1, db_1 web_1 | Starting web server... db_1 | Database ready # After running 'docker compose logs -f web', you will see live log updates from the 'web' service as they happen.
⚠️

Common Pitfalls

Common mistakes when using docker compose logs include:

  • Not specifying the correct service name, resulting in no logs shown.
  • Expecting logs from containers not managed by the current Compose project.
  • Forgetting to use -f to follow logs live if real-time updates are needed.

Always check your service names in docker-compose.yml and ensure your Compose project is running.

bash
docker compose logs unknown_service
# No output because 'unknown_service' does not exist

docker compose logs -f
# Follows logs for all services live
Output
ERROR: No such service: unknown_service # Following logs will show live output from all running services.
📊

Quick Reference

OptionDescription
-f, --followFollow log output live
--tail="all"Show only the last N lines, e.g., --tail=100
-t, --timestampsShow timestamps with logs
SERVICEName of one or more services to filter logs

Key Takeaways

Use docker compose logs to see logs from all or specific services.
Add -f to follow logs live as new entries appear.
Specify service names to filter logs to only those containers.
Check service names carefully to avoid errors or empty output.
Use options like --tail and -t for better log control.