This guide explains how to build and run a Docker container that monitors CPU temperature. When the temperature of Package id 0
exceeds 70°C, the container will send an email alert using Gmail's SMTP server.
- Docker installed on your system.
- A Gmail account with an app password created. You can create an app password here.
Ensure you have the Dockerfile
and monitor.sh
files in your working directory.
Open a terminal and navigate to the directory containing the Dockerfile
and monitor.sh
. Run the following command to build the Docker image:
docker build -t your-dockerhub-username/cpu-monitor:latest .
Replace your-dockerhub-username
with your actual Docker Hub username.
Execute the following command to run the Docker container. Replace the environment variables with your actual values:
docker run -d \
--name cpu-monitor \
-e SMTP_HOST=smtp.gmail.com \
-e SMTP_PORT=587 \
-e [email protected] \
-e SMTP_PASS=your-app-password \
-e [email protected] \
your-dockerhub-username/cpu-monitor:latest
Environment variables:
SMTP_HOST
: SMTP server address (Gmail:smtp.gmail.com
)SMTP_PORT
: SMTP server port (Gmail:587
)SMTP_USER
: Your Gmail addressSMTP_PASS
: Your Gmail app passwordEMAIL_TO
: The recipient's email address for the alerts
Check if the container is running correctly:
docker ps
You should see a container named cpu-monitor
in the list of running containers.
To verify that the script is running and check for any errors, view the container logs:
docker logs cpu-monitor
Instead of building the image, you can directly pull it from Docker Hub.
Use the following command to pull the image from Docker Hub:
docker pull jjaegii/cpu-monitor:latest
Execute the following command to run the Docker container:
docker run -d \
--name cpu-monitor \
-e SMTP_HOST=smtp.gmail.com \
-e SMTP_PORT=587 \
-e [email protected] \
-e SMTP_PASS=your-app-password \
-e [email protected] \
jjaegii/cpu-monitor:latest
Use the deployment.yaml
file to deploy to a Kubernetes cluster.
apiVersion: apps/v1
kind: Deployment
metadata:
name: cpu-monitor
labels:
app: cpu-monitor
spec:
replicas: 1
selector:
matchLabels:
app: cpu-monitor
template:
metadata:
labels:
app: cpu-monitor
spec:
containers:
- name: cpu-monitor
image: jjaegii/cpu-monitor:latest
env:
- name: SMTP_HOST
value: "smtp.gmail.com"
- name: SMTP_PORT
value: "587"
- name: SMTP_USER
value: "[email protected]"
- name: SMTP_PASS
value: "your-app-password"
- name: EMAIL_TO
value: "[email protected]"
Use the following command to deploy to the Kubernetes cluster:
kubectl apply -f deployment.yaml
- Ensure that your Docker daemon has access to the sensors. Depending on your system configuration, you might need to run the container with elevated privileges.
- The monitoring script checks the temperature every 60 seconds. Adjust the sleep duration in the
monitor.sh
script if you need a different frequency.
- If you encounter authentication issues with Gmail, ensure that the app password is correct and that your Gmail account settings allow access from less secure apps if needed.
- Verify that the
sensors
command is available and provides the expected output on your host system. You might need to installlm-sensors
on your host.