0
0
Dockerdevops~10 mins

Viewing container logs in Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Viewing container logs
Start Docker Container
Container Runs
Logs Generated
Run 'docker logs <container>'
Docker Fetches Logs
Display Logs to User
User Reads Logs or Follows with -f
End or Continue Following
This flow shows how logs are generated by a running container and how the 'docker logs' command fetches and displays them to the user.
Execution Sample
Docker
docker run -d --name myapp nginx

docker logs myapp

docker logs -f myapp
Starts an nginx container in the background, then shows how to view its logs and follow live log output.
Process Table
StepCommand RunActionResult/Output
1docker run -d --name myapp nginxStart container named 'myapp' in detached modeContainer ID printed, nginx starts running
2docker logs myappFetch logs from 'myapp' containerShows nginx startup logs (e.g., 'Starting nginx...')
3docker logs -f myappFollow logs live from 'myapp'Shows current logs and waits, streaming new logs as they appear
4Ctrl+C to stop followingStop live log streamingReturns to command prompt
💡 User stops following logs or container stops running
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Container StateNot runningRunning (nginx started)Running (logs fetched)Running (logs streaming)Running or stopped depending on user
Logs AvailableNoneStartup logs generatedLogs displayed onceLogs displayed continuouslyLogs remain stored until container removed
Key Moments - 2 Insights
Why do we use '-f' with 'docker logs'?
Using '-f' streams logs live as they are generated, shown in execution_table step 3, so you can watch events in real time instead of seeing only past logs.
What happens if the container is not running when we run 'docker logs'?
You still get the logs generated before the container stopped, because logs are stored until container removal. This is implied in variable_tracker where logs remain available after container stops.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what does the command in step 2 do?
AStarts the container named 'myapp'
BFetches and displays logs from the 'myapp' container
CStops the container named 'myapp'
DRemoves the container named 'myapp'
💡 Hint
Refer to the 'Action' and 'Result/Output' columns in step 2 of the execution table
At which step does the user start seeing live log updates?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Check the 'Action' column for 'Follow logs live' in the execution table
If the container stops running, what happens to the logs when you run 'docker logs'?
ALogs are lost and cannot be retrieved
BLogs are still available and displayed
CDocker restarts the container automatically
DYou get an error saying container not found
💡 Hint
See variable_tracker row 'Logs Available' after container stops
Concept Snapshot
docker logs <container>
- Shows logs from a container
- Use -f to follow logs live
- Logs persist after container stops
- Useful for debugging running or stopped containers
Full Transcript
This lesson shows how to view logs from a Docker container. First, you start a container in detached mode. The container runs and generates logs. You can fetch these logs anytime using 'docker logs <container>'. Adding '-f' streams logs live as they are created. Logs remain available even if the container stops, until it is removed. This helps you monitor and debug containers easily.