0
0
DockerHow-ToBeginner · 3 min read

How to View Docker Container Logs Easily

Use the docker logs [container_name_or_id] command to view the logs of a running or stopped Docker container. Add -f to follow the logs live as new entries appear.
📐

Syntax

The basic command to view logs is docker logs [OPTIONS] CONTAINER.

  • CONTAINER: The container's name or ID whose logs you want to see.
  • -f: Follow the log output live, like watching a live feed.
  • --tail N: Show only the last N lines of logs.
bash
docker logs [OPTIONS] CONTAINER
💻

Example

This example shows how to run a container and then view its logs live.

bash
docker run -d --name mynginx nginx

docker logs mynginx

docker logs -f mynginx
Output
a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6 [logs output of nginx container] [logs output live as new entries appear]
⚠️

Common Pitfalls

Common mistakes when viewing container logs include:

  • Using the wrong container name or ID, which causes an error.
  • Not using -f when you want to watch logs live.
  • Expecting logs from containers that have no output or have been removed.

Always check container status with docker ps -a before viewing logs.

bash
docker logs wrong_container_name
# Error: No such container: wrong_container_name

docker logs -f mynginx
# Correct usage to follow logs live
Output
Error: No such container: wrong_container_name # Live logs will stream after this command
📊

Quick Reference

CommandDescription
docker logs CONTAINERShow all logs of the container
docker logs -f CONTAINERFollow logs live as they appear
docker logs --tail 100 CONTAINERShow last 100 lines of logs
docker ps -aList all containers to find names or IDs

Key Takeaways

Use docker logs CONTAINER to see container logs.
Add -f to follow logs live in real time.
Check container names or IDs with docker ps -a before viewing logs.
Use --tail to limit how many log lines you see.
Logs are only available for existing containers, not removed ones.