Filter events by object type and action using --filter
In this step, you will learn how to filter Docker events based on the object type and action using the --filter
flag. This allows you to focus on specific types of events, such as container events or image events, and specific actions, such as create
, start
, stop
, or delete
.
The --filter
flag takes a key-value pair in the format key=value
. For filtering by object type, the key is type
and the value can be container
, image
, volume
, network
, daemon
, or plugin
. For filtering by action, the key is event
and the value is the specific action.
Let's start by filtering for only container events. We will run the hello-world
container again to generate events.
docker run hello-world
Now, use the docker events
command with the --filter type=container
flag to view only container-related events.
docker events --filter type=container --since 5m
You should see events like create
, start
, and die
for the hello-world
container, but not events related to image pulling.
Next, let's filter for a specific action, for example, only start
events for containers.
docker events --filter type=container --filter event=start --since 5m
This command will only show the start
event for the hello-world
container if it occurred within the last 5 minutes.
You can also filter by the name or ID of the object. For example, to filter events for a specific container named my-container
, you would use --filter container=my-container
.
Let's run the hello-world
container with a specific name.
docker run --name my-hello-container hello-world
Now, filter events specifically for the container named my-hello-container
.
docker events --filter container=my-hello-container --since 5m
You should see all events related to the my-hello-container
within the last 5 minutes.