📖 Scenario: You are working as a system administrator who needs to monitor Docker container activities in real-time. This helps you quickly see when containers start, stop, or change state, so you can respond fast to issues.
🎯 Goal: Build a simple Docker command setup that listens to Docker events and filters them to show only container start and stop events in real-time.
📋 What You'll Learn
Use the docker events command to listen to Docker events
Filter events to show only container start and stop actions
Use a command-line filter with --filter option
Display the filtered events live as they happen
💡 Why This Matters
🌍 Real World
System administrators and DevOps engineers use Docker events monitoring to track container lifecycle changes and troubleshoot issues quickly.
💼 Career
Knowing how to monitor Docker events is essential for maintaining healthy container environments and responding to incidents in production.
Progress0 / 4 steps
1
Set up Docker events command
Write the base command docker events to start listening to all Docker events in real-time.
Docker
Hint
Use the docker events command without any filters to see all events.
2
Add filter for container events
Add the filter --filter type=container to the docker events command to listen only to container-related events.
Docker
Hint
Use --filter type=container to limit events to containers only.
3
Filter for start and stop actions
Add two filters --filter event=start and --filter event=stop to show only container start and stop events.
Docker
Hint
Use multiple --filter event= options to select specific event types.
4
Run and observe filtered Docker events
Run the full command docker events --filter type=container --filter event=start --filter event=stop and observe the output when you start or stop a container.
Docker
Hint
Try starting or stopping a container in another terminal to see events appear here.
Practice
(1/5)
1. What is the primary purpose of the docker events command?
easy
A. To show real-time activity and changes in Docker objects
B. To list all Docker images available locally
C. To start a new Docker container
D. To remove unused Docker volumes
Solution
Step 1: Understand the command's function
The docker events command streams live events related to Docker objects like containers, images, and networks.
Step 2: Compare with other Docker commands
Commands like docker images, docker run, and docker volume prune serve different purposes unrelated to real-time event monitoring.
Final Answer:
To show real-time activity and changes in Docker objects -> Option A
Quick Check:
docker events = real-time Docker activity [OK]
Hint: Remember: events = live Docker activity stream [OK]
Common Mistakes:
Confusing events with listing images
Thinking it starts containers
Assuming it cleans volumes
2. Which of the following is the correct syntax to show Docker events filtered by container ID abc123?
easy
A. docker events --filter container=abc123
B. docker events -c abc123
C. docker events --container abc123
D. docker events filter container abc123
Solution
Step 1: Recall Docker events filter syntax
The correct filter syntax uses --filter key=value, so filtering by container ID is --filter container=abc123.
Step 2: Check other options for syntax errors
Options B, C, and D use incorrect flags or missing dashes, making them invalid commands.
Final Answer:
docker events --filter container=abc123 -> Option A
Quick Check:
Filter syntax = --filter key=value [OK]
Hint: Use --filter key=value to filter events [OK]
Common Mistakes:
Using short flags like -c which don't exist
Omitting --filter or using wrong flag names
Writing filter without dashes
3. What will be the output of the command docker events --filter event=start --filter container=mycontainer --since 10m?
medium
A. Shows all events for all containers in the last 10 minutes
B. Shows only start events for container 'mycontainer' from last 10 minutes
C. Shows stop events for container 'mycontainer' from last 10 minutes
D. Shows start events for all containers since 10 minutes ago
Solution
Step 1: Analyze the filters used
The command filters events by event=start and container=mycontainer, so only start events for that container are shown.
Step 2: Understand the time filter
The --since 10m option limits events to those in the last 10 minutes.
Final Answer:
Shows only start events for container 'mycontainer' from last 10 minutes -> Option B
Quick Check:
Filters combine to narrow events [OK]
Hint: Multiple --filter options combine to narrow events [OK]
Common Mistakes:
Ignoring the container filter
Confusing event=start with stop
Misunderstanding --since time filter
4. You run docker events --filter event=stop --filter container=webapp but see no output even though the container stopped recently. What is the likely cause?
medium
A. The docker events command does not support filtering
B. The container never stopped
C. The container name is incorrect or misspelled
D. You need to add --since to see recent events
Solution
Step 1: Verify container name correctness
If the container name is wrong or misspelled, no matching events will appear.
Step 2: Check command capabilities and event timing
The docker events command supports filtering, and recent stop events should show without --since if they happened after the command started.
Final Answer:
The container name is incorrect or misspelled -> Option C
Quick Check:
Correct container name needed for filtering [OK]
Hint: Check container name spelling when no events appear [OK]
Common Mistakes:
Assuming filtering is unsupported
Forgetting container names are case-sensitive
Thinking --since is always required
5. You want to monitor Docker events for container dbserver and automatically trigger a script when it stops. Which command correctly streams only stop events for that container?
hard
A. docker events --container=dbserver --event=stop
B. docker events --filter event=stop --container dbserver
C. docker events --filter stop --filter container=dbserver
D. docker events --filter container=dbserver --filter event=stop
Solution
Step 1: Use correct filter syntax for container and event
The correct syntax uses --filter key=value. So --filter container=dbserver and --filter event=stop are valid.
Step 2: Identify invalid options
Options B, C, and D use incorrect flags like --container or missing event= format, which are invalid.
Final Answer:
docker events --filter container=dbserver --filter event=stop -> Option D
Quick Check:
Use --filter key=value for all filters [OK]
Hint: Always use --filter key=value for event and container filters [OK]
Common Mistakes:
Using --container or --event flags incorrectly
Omitting 'event=' or 'container=' in filters
Mixing filter syntax with invalid flags
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.