0
0
Dockerdevops~5 mins

Viewing container logs in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you run applications inside containers, you often need to see what is happening inside them. Viewing container logs helps you check the output and errors your app produces, so you can understand its behavior or find problems.
When you want to check if your app inside a container started correctly.
When you need to find out why your containerized app crashed or stopped.
When you want to monitor real-time output from a running container.
When debugging issues during development of containerized applications.
When verifying that logs are being generated as expected inside containers.
Commands
This command runs an Nginx web server container in the background with the name 'example-app'. We use this to have a running container to view logs from.
Terminal
docker run -d --name example-app nginx:1.23
Expected OutputExpected
a1b2c3d4e5f67890123456789abcdef0123456789abcdef0123456789abcdef0
-d - Run container in detached mode (in background)
--name - Assign a custom name to the container
This command shows all the logs that the 'example-app' container has produced so far. It helps you see what the container outputted since it started.
Terminal
docker logs example-app
Expected OutputExpected
172.17.0.1 - - [27/Apr/2024:12:00:00 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.68.0"
This command shows the logs and keeps showing new log lines as they appear, like watching a live feed. The '-f' flag means 'follow'.
Terminal
docker logs -f example-app
Expected OutputExpected
172.17.0.1 - - [27/Apr/2024:12:00:00 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.68.0"
-f - Follow log output in real time
This command shows only the last 5 lines of logs from the container. Useful when logs are very long and you want to see recent activity.
Terminal
docker logs --tail 5 example-app
Expected OutputExpected
172.17.0.1 - - [27/Apr/2024:12:00:00 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.68.0" 172.17.0.1 - - [27/Apr/2024:12:01:00 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "-" "curl/7.68.0"
--tail - Show only the last N lines of logs
Key Concept

If you remember nothing else from this pattern, remember: use 'docker logs' with the container name to see what your container is outputting.

Common Mistakes
Trying to view logs of a container that is not running or does not exist.
Docker will return an error because there are no logs to show for a stopped or unknown container.
Make sure the container exists by using 'docker ps -a' before viewing logs. Note that logs can be viewed for stopped containers as well.
Not using the '-f' flag when wanting to see live logs.
Without '-f', you only see logs up to the moment you run the command, missing new log entries.
Use 'docker logs -f container-name' to follow logs in real time.
Using container ID partially or incorrectly when running 'docker logs'.
Docker requires the full or unique prefix of the container ID or the exact container name to find logs.
Use the full container name or check container IDs with 'docker ps' to use the correct identifier.
Summary
Run a container with 'docker run' to have a container producing logs.
Use 'docker logs container-name' to view all logs from that container.
Add '-f' to follow logs live and '--tail N' to see only recent lines.