0
0
DockerHow-ToBeginner · 3 min read

How to Use Docker Logs: View Container Output Easily

Use the docker logs [OPTIONS] CONTAINER command to see the output of a running or stopped Docker container. This shows the container's standard output and error streams, helping you debug or monitor your container's activity.
📐

Syntax

The basic syntax of the docker logs command is:

  • docker logs [OPTIONS] CONTAINER

Where:

  • CONTAINER is the container ID or name you want to see logs from.
  • OPTIONS modify the output, such as showing timestamps or following live logs.
bash
docker logs [OPTIONS] CONTAINER
💻

Example

This example shows how to run a container and then view its logs using docker logs. It demonstrates how to see the output of a container named myapp.

bash
docker run -d --name myapp busybox sh -c "echo Hello Docker Logs; sleep 10"
docker logs myapp
Output
Hello Docker Logs
⚠️

Common Pitfalls

Common mistakes when using docker logs include:

  • Trying to get logs from a container that does not exist or is not running.
  • Not using the -f option to follow live logs when needed.
  • Confusing container names or IDs, which must be exact.

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

bash
docker logs wrong-container-name
# Error: No such container: wrong-container-name

docker logs -f myapp
# Follows live logs until stopped
📊

Quick Reference

OptionDescription
-f, --followFollow log output live
--since stringShow logs since a specific time, e.g., '2024-06-01T13:23:00'
--tail stringShow only the last N lines, e.g., '100'
-t, --timestampsShow timestamps with logs

Key Takeaways

Use docker logs CONTAINER to view container output.
Add -f to follow logs live as they are generated.
Check container names or IDs carefully before running the command.
Use options like --since and --tail to filter logs.
If logs are empty, verify the container is running or has output.