keepcalculator

06 Numbers and Powers

Docker Resource Drain: The Hidden 24/7 Container Cost

Card No. 06·87848 · Updated · Standard formula, shown below

CPU hours consumed 1,440 core-hours
Memory hours
2,880 GB-hours
Result
Total resource hours
5,760 GB-hours (CPU+RAM)
Result
CPU cores allocated
2 cores
You entered
You enter
CPU cores allocated, Memory limit, Days running
You get
CPU hours consumed, Memory hours, Total resource hours
Rounding
0 dp

Docker containers can quietly consume CPU, memory, and disk 24/7 even when you aren’t actively using them—often because of unoptimized base images, runaway log files, and background processes that never sleep. This article explains the most common oversight—unbounded resource limits and default logging drivers—and shows you how to detect and fix it before your host runs out of resources.

Formula

The calculator above estimates the total resource consumption of a Docker container over a given period. It uses two simple formulas:

  • CPU hours = CPU cores allocated × 24 hours × days running
  • Memory hours = Memory limit (GB) × 24 hours × days running

Where:

  • CPU cores allocated is the number of vCPUs you assigned to the container (via --cpus or Compose cpus:).
  • Memory limit is the RAM cap in GB (via --memory or Compose mem_limit:).
  • Days running is the uptime of the container.

The output is a rough estimate of the maximum possible resource draw, not the actual usage—actual usage is often lower, but the allocation still reserves capacity on the host.

Worked Example

Using the default values from the calculator: 2 CPU cores, 4 GB memory, 30 days.

  1. CPU hours = 2 × 24 × 30 = 1,440 core-hours
  2. Memory hours = 4 × 24 × 30 = 2,880 GB-hours
  3. Total resource hours = (2×2 + 4) × 24 × 30 = (4 + 4) × 720 = 5,760 GB-hours

So a modest container with 2 vCPUs and 4 GB RAM running for a month consumes up to 1,440 core-hours of CPU time and 2,880 GB-hours of memory—even if it does nothing useful.

Why Containers Drain Resources 24/7

Docker containers are lightweight VMs, but they are not free. The most common oversight is assuming that a container that is “idle” uses zero resources. In reality, every container has a running process (PID 1) that consumes CPU cycles, and the kernel allocates memory for its page cache, sockets, and file descriptors. If you don’t set limits, Docker defaults to unlimited CPU and memory on the host, meaning a single runaway container can starve the entire machine.

Another hidden drain is the logging driver. By default, Docker captures all stdout/stderr from the container and writes it to JSON files on the host. Without a rotation policy, these files grow indefinitely, eating disk space. A chatty application can produce gigabytes of logs per day, silently filling your disk.

Background processes inside the container—like cron jobs, health checks, or monitoring agents—can also keep waking up and consuming CPU even when the main service is idle. Similarly, the container’s filesystem layer (the writable layer) accumulates temporary files and deleted-but-open files, which are not freed until the container is removed.

How to Detect Resource Drain

Start with Docker’s built-in tools:

  • docker stats shows real-time CPU, memory, network, and disk I/O for each running container.
  • docker top <container> lists the processes inside the container, revealing unexpected background tasks.
  • docker system df shows disk usage by images, containers, and volumes.
  • docker inspect <container> reveals the resource limits (or lack thereof) and logging driver configuration.

For historical trends, use cAdvisor or Prometheus, but the quickest check is docker stats --no-stream to get a single snapshot. Look for containers that use more CPU than expected (e.g., >10% when idle) or memory that grows over time (a memory leak).

Common Mistakes That Waste Resources

  • No memory limit—a container can use all host RAM, forcing the kernel to swap or OOM-kill processes.
  • No CPU limit—a busy loop in one container can saturate all cores, affecting other containers.
  • Unbounded log files—without --log-opt max-size=10m --log-opt max-file=3, logs grow forever.
  • Running unnecessary containers—forgetting to stop dev databases, test servers, or sidecar containers.
  • Using huge base images—a full Ubuntu image is ~200 MB, while Alpine is ~5 MB; larger images mean more disk and RAM for page cache.
  • Not cleaning up—dangling images and stopped containers still occupy disk space.

How to Set Resource Limits

In Docker Compose (v2+), add:

services:
  app:
    image: myapp
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 256M
        reservations:
          cpus: '0.1'
          memory: 64M

For docker run, use:

docker run --cpus=0.5 --memory=256m --memory-swap=256m --log-opt max-size=10m --log-opt max-file=3 myapp

Set the logging driver globally in /etc/docker/daemon.json:

{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  }
}

Then restart Docker. This prevents log files from consuming disk space.

Comparing Docker to Other Virtualization

Docker uses the host kernel, so it is inherently lighter than a full VM (which includes a guest OS). A VM reserves memory for its entire allocated RAM, while a container only uses what its processes touch—but the container’s memory limit is a hard cap, not a reservation. However, Docker’s shared kernel means that a container can’t have a different OS, and a kernel panic in the host takes down all containers. VMs provide stronger isolation but at a higher resource cost. For resource efficiency, Docker is generally better, but only if you set limits; otherwise, a misbehaving container can affect the whole host.

Costs and Capacity Planning

Resource drain translates directly to cost in cloud environments, where you pay for the VM or Kubernetes node that hosts the containers. If you run a cluster of 10 containers, each with 2 CPUs and 4 GB RAM, you are reserving 20 CPUs and 40 GB RAM—even if the actual usage is 5% of that. This over-provisioning means you pay for idle capacity. Use the calculator above to estimate the total resource hours per month, then compare that to your actual usage from docker stats. If the ratio is high, you are over-allocating.

For on-premise servers, the cost is less direct, but the drain still reduces the number of containers you can run on a single host, forcing you to buy more hardware.

Safety and Best Practices

  • Always set memory and CPU limits—even for development containers.
  • Use log rotation to prevent disk exhaustion.
  • Regularly run docker system prune to remove stopped containers, dangling images, and unused networks.
  • Use multi-stage builds to keep images small.
  • Monitor with docker stats or a tool like cAdvisor.
  • For production, consider using Kubernetes with ResourceQuotas and LimitRanges to enforce limits across the cluster.

Remember: the calculator gives an upper bound, not actual usage. To get real numbers, use monitoring, but the limits you set are what the scheduler uses to allocate capacity.

Frequently Asked Questions

Why does my container use 100% CPU when idle?

An idle container should use near 0% CPU. If it’s at 100%, check docker top to see what process is running. Common culprits are a busy-wait loop in the application, a misconfigured health check that runs too often, or a background agent (like a log shipper) that is stuck. Fix the process, or add a CPU limit to cap the damage.

How do I know if my container has a memory leak?

Run docker stats at intervals and note the memory usage. If it steadily increases and never drops, you likely have a leak. Use docker exec to inspect the process’s RSS, or use a profiling tool. Restart the container periodically as a stopgap, but fix the underlying code.

What is the default logging driver in Docker?

The default is json-file, which stores each container’s logs in a JSON file under /var/lib/docker/containers/<id>/. Without rotation, this file grows indefinitely. You can change the driver to local (which has built-in rotation) or set the max-size/max-file options.

Can I run Docker without any limits?

Technically yes, but it is risky. A single container can consume all host resources, causing the kernel to OOM-kill processes (including your container) or making the host unresponsive. Always set at least a memory limit and a CPU limit, even in development.

How do I clean up unused Docker resources?

Use docker system prune -a to remove all stopped containers, unused networks, dangling images, and build cache. For volumes, use docker volume prune. Be careful: -a removes all images not used by a running container, so you may need to re-pull them later.

Does Docker use more resources than a native process?

Docker adds a small overhead (a few percent CPU and some memory) due to the daemon, namespaces, and cgroups. For most workloads, this is negligible. The bigger cost is the image size (disk) and the fact that you often run more containers than necessary. A native process is lighter, but Docker provides isolation and reproducibility that often outweigh the overhead.

Related

Formula used
in cpus | CPU cores allocated | cores | 2 | 0.1 | 64
in mem | Memory limit | GB | 4 | 0.1 | 512
in days | Days running | days | 30 | 1 | 3650
out cpu_hours | CPU hours consumed | core-hours | cpus * 24 * days | 0
out mem_gb_hours | Memory hours | GB-hours | mem * 24 * days | 0
out total_gb_hours | Total resource hours | GB-hours (CPU+RAM) | (cpus * 2 + mem) * 24 * days | 0

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →