In this step, we will learn how to output Docker events in JSON format using the --json
flag with the docker events
command. Outputting events in JSON format is useful for programmatic processing and integration with other tools.
First, ensure you are in the ~/project/my-compose-app
directory.
cd ~/project/my-compose-app
Now, let's run the docker events
command with the --json
flag. This will stream the events in JSON format.
docker events --json
You will see a continuous stream of JSON objects, where each object represents a Docker event. The output will look similar to this (details will vary):
{"status":"create","id":"1234567890abcdef...","from":"nginx:latest","Type":"container","Action":"create","Actor":{"ID":"1234567890abcdef...","Attributes":{"image":"nginx:latest","name":"my-compose-app-web-1"}},"scope":"local","time":1678886400,"timeNano":1678886400123456789}
{"status":"start","id":"1234567890abcdef...","from":"nginx:latest","Type":"container","Action":"start","Actor":{"ID":"1234567890abcdef...","Attributes":{"image":"nginx:latest","name":"my-compose-app-web-1"}},"scope":"local","time":1678886401,"timeNano":1678886401987654321}
...
Each line is a valid JSON object containing detailed information about the event, such as the event status
, the affected object's id
, Type
, Action
, and Actor
details including Attributes
like the image name and container name.
To demonstrate capturing these events, let's stop and then start the web
service again while the docker events --json
command is running in another terminal or background. Since we are using a single terminal in this lab, we will stop the docker events --json
command first by pressing Ctrl + C
.
Now, let's stop the web
service.
docker-compose stop web
You should see output indicating that the web
container is being stopped.
[+] Stopping 1/1
⠿ Container my-compose-app-web-1 Stopped
Now, let's run docker events --json
in the background and then start the service again. We will use the &
symbol to run the command in the background.
docker events --json &
You will see a process ID (PID) printed, indicating the command is running in the background.
Now, start the web
service again.
docker-compose start web
You should see output indicating that the web
container is being started.
[+] Starting 1/1
⠿ Container my-compose-app-web-1 Started
The docker events --json
command running in the background will capture the stop
and start
events. To see the output from the background process, you might need to bring it to the foreground using the fg
command, or check the terminal output after the background process is stopped. However, for the purpose of this step, simply running the command with --json
is sufficient to understand the format.
To stop the background docker events --json
process, you can use the jobs
command to list background jobs and then kill %<job_number>
. Alternatively, you can find the process ID using ps aux | grep 'docker events --json'
and use the kill
command with the PID. A simpler way in this context is to just proceed to the next step, as the background process won't interfere significantly.
In this step, we successfully used the --json
flag to view Docker events in a structured JSON format, which is valuable for automation and analysis.