We are using Google Cloud Platform and its service Compute Engine. We have Docker installed on one of our VM instances (which is part of Compute Engine). Can we set up an alert in GCP Monitoring to be triggered when a container exits?
We are using Google Cloud Platform and its service Compute Engine. We have Docker installed on one of our VM instances (which is part of Compute Engine). Can we set up an alert in GCP Monitoring to be triggered when a container exits?
Yes. You have to configure docker with Cloud logging driver, create a log based metric and an alerting policy on that metric.
Solution:
Configure docker with cloud logging driver.
SSH to your instance.
Run this command as root: dockerd --log-driver=gcplogs. This will forward your docker logs to Google Cloud Logging.
If running a container-optimized OS then follow these steps:
echo '{"log-driver":"gcplogs"}' | sudo tee /etc/docker/daemon.json
sudo systemctl restart docker
Try starting and exiting a container.
These are the logs generated whenever we exit a container. Keep in mind the two exit messages

Create a log based metric
Go to Logging -> Select Log-based Metrics.
Click on Create Metric.
Metric Type: Counter. In Details, enter a Log Metric name (e.g. mysite-container-exited)
Under Filter Selection -> Build filter, copy the following code
resource.type="gce_instance"
log_name="projects/myproject-12345/logs/gcplogs-docker-driver" replace myproject-12345 with your project name.
jsonPayload.container.name="/mysite" change mysite to your container name.
jsonPayload.message=~"exit$" This is regex that matches exit as the last word in a line.
It should look something like this.

Create an alerting policy



As we have seen that two exit commands are issued per container to the logs whenever we exit a container, thats's why the threshold is set to above 1.
You can monitor more containers by creating a new metric and changing the value of jsonPayload.container.name="/mysite" to your container name.
Now try to start and stop the container. You will receive an alert on email in 2-3 minutes.
