0
0
Dockerdevops~30 mins

Docker events monitoring - Mini Project: Build & Apply

Choose your learning style9 modes available
Docker Events Monitoring
📖 Scenario: You are a system administrator who wants to monitor Docker container events in real-time. This helps you track when containers start, stop, or change state, which is important for managing your applications.
🎯 Goal: Build a simple script that listens to Docker events and filters them to show only container start and stop events.
📋 What You'll Learn
Use the docker events command to listen to Docker events.
Filter events to show only start and stop container events.
Display the event type and container ID for each event.
💡 Why This Matters
🌍 Real World
Monitoring Docker events helps system administrators track container lifecycle changes and troubleshoot issues quickly.
💼 Career
Understanding Docker event monitoring is useful for DevOps engineers and system administrators managing containerized applications.
Progress0 / 4 steps
1
Set up Docker event listening command
Write a command to listen to Docker events using docker events and save it in a variable called event_command.
Docker
Need a hint?

Use a string variable to store the command docker events.

2
Add filters for start and stop events
Add a variable called filters that contains the filter options to show only start and stop events for containers. Use the Docker CLI filter syntax: --filter 'event=start' --filter 'event=stop'.
Docker
Need a hint?

Filters must be a string with two filter options for start and stop events.

3
Combine command and filters
Create a variable called full_command that combines event_command and filters with a space between them.
Docker
Need a hint?

Use string concatenation with a space to combine the command and filters.

4
Run the command and display events
Write code to run full_command using subprocess.Popen to capture output line by line. For each line, print only the event type and container ID. Use print(event_type, container_id). Assume event lines contain the words start or stop and the container ID is the word after container.
Docker
Need a hint?

Use subprocess.Popen with shell=True and text=True to read output line by line.

Split each line and find the event type and container ID by checking words.