Create the Dockerfile
FROM httpd:latest
# Install bash to run the script and cron to run the cron job
RUN apt-get update && apt-get install -y bash procps cron
# Copy the stats script
COPY stats.sh /usr/local/apache2/htdocs/stats.sh
# Set up a cron job to update the stats every 5 seconds and serve the HTML file
RUN echo "*/1 * * * * root /bin/bash /usr/local/apache2/htdocs/stats.sh > /usr/local/apache2/htdocs/index.html" >> /etc/cron.d/system_log
# Start cron in the background and httpd in the foreground
CMD ["sh", "-c", "cron && httpd-foreground"]
Create script file:
#!/bin/bash
# Get CPU and Memory usage
cpu_usage=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1"%"}')
mem_usage=$(free -m | awk 'NR==2{printf "%.2f", $3*100/$2 }')
# Generate HTML page
cat <<EOF
<!DOCTYPE html>
<html>
<head>
<title>System Stats</title>
<meta http-equiv="refresh" content="5">
<style>
body { font-family: Arial, sans-serif; text-align: center; margin-top: 50px; }
h1 { color: #333; }
</style>
</head>
<body>
<h1>System Stats</h1>
<p><strong>CPU Usage:</strong> $cpu_usage</p>
<p><strong>Memory Usage:</strong> $mem_usage%</p>
</body>
</html>
EOF
Create the image:
docker build -t system-stats-httpd .
[+] Building 1.2s (9/9) FINISHED docker:desktop-linux
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 576B 0.0s
=> [internal] load metadata for docker.io/library/httpd:latest 1.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [1/4] FROM docker.io/library/httpd:latest@sha256:ae1124b8d23ee3fc35d49da35d5c748a2fce318d1f55ce59ccab889d612f8be8 0.0s
=> => resolve docker.io/library/httpd:latest@sha256:ae1124b8d23ee3fc35d49da35d5c748a2fce318d1f55ce59ccab889d612f8be8 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 30B 0.0s
=> CACHED [2/4] RUN apt-get update && apt-get install -y bash procps cron 0.0s
=> CACHED [3/4] COPY stats.sh /usr/local/apache2/htdocs/stats.sh 0.0s
=> CACHED [4/4] RUN echo "*/1 * * * * root /bin/bash /usr/local/apache2/htdocs/stats.sh > /usr/local/apache2/htdocs/index.html" >> /etc/cron.d/system_log 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => exporting manifest sha256:af0c6ba1de54a77ac0f980b78f29d28572786391ccec760abbc762a7d442c0a5 0.0s
=> => exporting config sha256:5b99919d3200df79269dcdf98bb0bc9da16ce8b22f0af6df5bfb9a03e3fb1755 0.0s
=> => exporting attestation manifest sha256:6270bacfb3c7daca9d21e9204f420bd77c0dc417eca4a7c59a185881684df64a 0.0s
=> => exporting manifest list sha256:474f4506661c44fbd3f44cb954a1af29baf137b7bcae0343938c4e01e64ca6f1 0.0s
=> => naming to docker.io/library/system-stats-httpd:latest 0.0s
=> => unpacking to docker.io/library/system-stats-httpd:latest 0.0s
What's next:
View a summary of image vulnerabilities and recommendations → docker scout quickview
Run the container:
docker run -d --name system-stats -p 9090:80 system-stats-httpd
b633f7f3c3c1616151192e47407daa8f828d2ca2eeed9f54aedea11944fdb332
Verify the container status:
docker ps --no-trunc
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b633f7f3c3c1616151192e47407daa8f828d2ca2eeed9f54aedea11944fdb332 system-stats-httpd "sh -c 'cron && httpd-foreground'" 12 minutes ago Up 12 minutes 0.0.0.0:9090->80/tcp system-stats
Browse the site: