Green DevOps on VPS: Automating Docker Container Energy Measurement and Optimization with Scaphandre
Introduction to Green DevOps in the Cloud Era
As cloud computing continues to power the global digital transformation, the environmental impact of data centers has come under intense scrutiny. Traditional DevOps practices focus heavily on speed, scalability, and high availability. However, a new paradigm is emerging: Green DevOps. This approach integrates environmental sustainability into the continuous integration and continuous deployment (CI/CD) pipelines, aiming to minimize the carbon footprint of digital infrastructure.
While enterprise-grade cloud providers are beginning to offer sustainability dashboards, independent developers and small-to-medium enterprises (SMEs) relying on Virtual Private Servers (VPS) are often left in the dark. How do you measure and optimize what you cannot see? This technical guide explores how to build a Green DevOps infrastructure on a standard Linux VPS by monitoring and optimizing Docker container energy consumption using an open-source tool called Scaphandre.
The Challenge of Power Measurement on Virtual Private Servers
On bare-metal servers, measuring power consumption is relatively straightforward. Engineers can read energy metrics directly from hardware components using interfaces like Intel's Running Average Power Limit (RAPL) or AMD's equivalents. These interfaces provide precise, real-time data on how many microwatts the CPU and memory modules are consuming.
However, when operating within a Virtual Private Server (VPS), a hypervisor abstracts the underlying hardware. Standard virtual machines do not have direct access to host-level RAPL interfaces. This presents a unique challenge for Green DevOps: How can we isolate and measure the power consumption of individual Docker containers running on a shared, virtualized kernel?
Introducing Scaphandre: The Metrology Agent
To bridge this visibility gap, we utilize Scaphandre, an open-source monitoring agent developed by Hubblo. Named after the French word for a heavy diving suit, Scaphandre "dives" into the operating system to collect detailed resource consumption data. It acts as a metrics provider, translating hardware power consumption or advanced statistical estimations into actionable data points.
Scaphandre solves the virtualization hurdle by using intelligent power distribution algorithms. If direct RAPL access is available (on dedicated or bare-metal VPS instances), it reads the data directly. On heavily virtualized instances where RAPL is blocked, Scaphandre can calculate estimated energy profiles based on precise per-process CPU time and resource utilization metrics combined with known hardware baselines.
Why Scaphandre for Docker?
- Granular Monitoring: It tracks consumption down to the specific Process ID (PID), allowing it to map power metrics directly to specific Docker containers.
- Native Prometheus Integration: It features a built-in Prometheus exporter, making it seamless to integrate into existing cloud monitoring stacks.
- Low Overhead: Written in Rust, Scaphandre is highly efficient and introduces negligible CPU and memory overhead to your host system.
Step-by-Step Architecture: Setting Up the Green DevOps Stack
To automate the measurement and optimization pipeline, we will deploy a comprehensive stack on our VPS consisting of Scaphandre, Prometheus, and Grafana, all managed via Docker Compose.
Step 1: Preparing the VPS Host
Ensure your VPS is running a modern Linux distribution (e.g., Ubuntu 22.04 LTS or newer) with Docker and Docker Compose installed. Before deploying Scaphandre, verify if the host kernel has access to the RAPL modules by running:
lsmod | grep intel_raplIf this returns no results, Scaphandre will fall back to its advanced estimation mode based on CPU time allocation, which still provides highly accurate relative metrics for container comparison.
Step 2: Configuring the Docker Compose Stack
Create a directory named green-devops-stack and define the following docker-compose.yml file. This configuration mounts the host's /proc file system and system metrics into Scaphandre, enabling it to inspect process-level resource usage.
version: '3.8'
services:
scaphandre:
image: hubblo/scaphandre:latest
volumes:
- /proc:/mod_proc
- /sys/class/powercap:/sys/class/powercap
command: prometheus
ports:
- "8080:8080"
restart: always
security_opt:
- no-new-privileges:true
prometheus:
image: prom/prometheus:latest
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
ports:
- "9090:9090"
restart: always
grafana:
image: grafana/grafana:latest
ports:
- "3000:3000"
environment:
- GF_SECURITY_ADMIN_PASSWORD=admin_secure_password
restart: alwaysStep 3: Configuring Prometheus Scrape Targets
Create the corresponding prometheus.yml file in the same directory to instruct Prometheus to scrape the metrics generated by Scaphandre every 10 seconds.
global:
scrape_interval: 10s
scrape_configs:
- job_name: 'scaphandre'
static_configs:
- targets: ['scaphandre:8080']Deploy the stack by executing docker compose up -d. Your monitoring architecture is now actively recording the energy footprint of your VPS.
Visualizing Container Energy Consumption in Grafana
With data flowing from Scaphandre to Prometheus, you can now build a dynamic sustainability dashboard in Grafana. Scaphandre exposes several critical metrics, including:
scaph_process_power_consumption_microwatts: The real-time power consumption of a specific PID.scaph_host_power_consumption_microwatts: The total power consumption of the host instance.
To cross-reference PIDs with actual Docker container names, you can leverage custom PromQL queries that join Scaphandre's process metrics with container cgroup metadata. A foundational PromQL expression to calculate the power consumption of a specific container in Watts looks like this:
sum(scaph_process_power_consumption_microwatts{exe_path=~"/usr/bin/dockerd.*"}) / 1000000By organizing these metrics into charts, your engineering team can identify "carbon-heavy" containers—services that draw disproportionate amounts of power relative to their functional business value.
Automating Optimization: The Continuous Green Delivery Pipeline
Measuring energy consumption is only the first phase; true Green DevOps requires automated optimization. Once Scaphandre flags a container as inefficient, you can implement automated responses within your CI/CD pipelines and runtime environment.
1. Dynamic Resource Capping
When Scaphandre detects a container exceeding a pre-defined energy threshold during stress testing or production peaks, you can use Docker's runtime constraints to dynamically throttle its resource access. For instance, reducing CPU shares automatically scales down power consumption:
docker update --cpus="1.5" --memory="2g" my_inefficient_container2. Eco-Routing and Workload Shifting
In multi-VPS or hybrid cloud setups, you can automate workload shifting. If Scaphandre metrics indicate that a background batch processing job on VPS Alpha is triggering a massive power spike, a custom script or orchestrator can pause the task and shift execution to a time window when the underlying grid electricity is cleaner, or to a more energy-efficient VPS instance.
3. Automated Green Code Audits
Integrate Scaphandre into your staging environment. Run integration tests for every new pull request while monitoring the Scaphandre metrics. If a code change causes a statistically significant increase in microwatts per request compared to the main branch, the CI/CD pipeline can automatically fail the build, prompting developers to optimize their algorithms before code ever hits production.
Conclusion: The Business and Environmental ROI
Transitioning to a Green DevOps framework on your VPS infrastructure yields a double benefit. Environmentally, it significantly lowers the operational carbon footprint of your applications. Financially, optimizing for energy efficiency directly correlates with reducing CPU and memory utilization. In the cloud world, lower resource consumption allows you to downsize your VPS tiers, directly lowering monthly infrastructure costs.
By deploying Scaphandre alongside Docker, Prometheus, and Grafana, you gain total visibility into the invisible cost of computing. Sustainability is no longer just a corporate buzzword—it becomes a quantifiable engineering metric built directly into your deployment pipeline.
