0
0
Dockerdevops~5 mins

Docker logs for troubleshooting - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes your Docker container does not work as expected. Docker logs help you see what is happening inside the container so you can find and fix problems quickly.
When your web app container crashes and you want to see error messages.
When a background service in a container is not responding and you want to check its activity.
When you deploy a new version of your app and want to verify it started correctly.
When you want to monitor real-time output from a container to understand its behavior.
When debugging why a container stopped unexpectedly.
Commands
This command lists all running containers so you can find the container ID or name to check logs.
Terminal
docker ps
Expected OutputExpected
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b1c2d3e4f5g6 nginx:1.23.3 "/docker-entrypoint.…" 10 minutes ago Up 10 minutes 0.0.0.0:8080->80/tcp my-nginx
This shows all the logs from the container named 'my-nginx'. It helps you see what happened since the container started.
Terminal
docker logs my-nginx
Expected OutputExpected
172.17.0.1 - - [27/Apr/2024:10:00:00 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0" 172.17.0.1 - - [27/Apr/2024:10:01:00 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "-" "Mozilla/5.0"
This command follows the logs in real-time, showing new log lines as they appear. Useful for live troubleshooting.
Terminal
docker logs -f my-nginx
Expected OutputExpected
172.17.0.1 - - [27/Apr/2024:10:02:00 +0000] "GET /about HTTP/1.1" 200 720 "-" "Mozilla/5.0" 172.17.0.1 - - [27/Apr/2024:10:03:00 +0000] "GET /contact HTTP/1.1" 200 680 "-" "Mozilla/5.0"
-f - Follow logs in real-time
This shows only the last 5 lines of logs. It helps to quickly see recent activity without too much output.
Terminal
docker logs --tail 5 my-nginx
Expected OutputExpected
172.17.0.1 - - [27/Apr/2024:10:00:55 +0000] "GET /home HTTP/1.1" 200 600 "-" "Mozilla/5.0" 172.17.0.1 - - [27/Apr/2024:10:01:10 +0000] "GET /api/data HTTP/1.1" 200 1024 "-" "Mozilla/5.0" 172.17.0.1 - - [27/Apr/2024:10:01:30 +0000] "POST /api/update HTTP/1.1" 201 256 "-" "Mozilla/5.0" 172.17.0.1 - - [27/Apr/2024:10:01:50 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "-" "Mozilla/5.0" 172.17.0.1 - - [27/Apr/2024:10:02:00 +0000] "GET /about HTTP/1.1" 200 720 "-" "Mozilla/5.0"
--tail - Show only last N lines
Key Concept

If you remember nothing else from this pattern, remember: docker logs lets you see what your container is doing or what errors it has, which is key to fixing problems.

Common Mistakes
Trying to get logs from a container that is not running.
Docker logs only show output from containers that exist; if the container stopped or was removed, logs may be unavailable or incomplete.
Use 'docker ps -a' to check all containers including stopped ones, then get logs from the correct container ID or name.
Not using the '-f' flag when wanting to see live logs.
Without '-f', you only see past logs and miss new log entries as they happen.
Add '-f' to follow logs in real-time for live troubleshooting.
Using incorrect container name or ID in the logs command.
Docker will return an error if the container name or ID does not exist.
Run 'docker ps' to find the exact container name or ID before running 'docker logs'.
Summary
Use 'docker ps' to find running containers and their names or IDs.
Run 'docker logs <container-name>' to see all logs from a container.
Use 'docker logs -f <container-name>' to watch logs live as they happen.
Use 'docker logs --tail N <container-name>' to see only the last N lines of logs.