keepcalculator

06 Numbers and Powers

Docker Containers per VPS: Practical Sizing Guide

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

You can run anywhere from a handful to several hundred Docker containers on a single VPS, depending primarily on the total resources (CPU, RAM, disk I/O) each container needs and the overhead of the host system. The practical limit is not a fixed number but a function of your workload: a 2 GB VPS might comfortably run 10 lightweight web apps, while a 32 GB server could host 200+ small services. This guide explains how to estimate your own capacity, what actually limits container density, and how to avoid common pitfalls.

What Actually Limits Container Count

Docker containers are not virtual machines. They share the host kernel and do not reserve memory or CPU unless you explicitly set limits. This means the theoretical maximum is bounded by the host’s resources, not by Docker itself. The key constraints are:

  • RAM: The most common bottleneck. Each process inside a container consumes memory; if the sum exceeds the VPS’s RAM, the kernel’s OOM killer will terminate processes (often the container’s main process).
  • CPU: CPU time is shared. If your containers are idle most of the time, you can run many more than if they are all doing heavy computation simultaneously.
  • Disk space and I/O: Each image and container layer takes disk space. More importantly, a high number of containers doing frequent reads/writes can saturate the disk’s IOPS, causing slowdowns.
  • Network ports and connections: Each container that exposes a port needs a unique host port (or a reverse proxy to route by domain). Also, the kernel has limits on the number of open file descriptors and network connections.
  • Process/thread limits: The host has a maximum number of processes (often thousands), and each container adds a few. This is rarely the first limit, but it exists.

How Docker Overhead Works

Unlike a virtual machine that includes a full guest OS, a Docker container uses the host’s kernel and only packages the application and its dependencies. The overhead per container is minimal—just a few megabytes of memory for the container’s metadata and the process itself. For example, an empty Alpine Linux container running sleep infinity might use only 1–2 MB of RAM. In practice, the overhead is so small that the total resource usage is almost entirely determined by the applications you run inside.

However, Docker itself (the daemon) and the networking layer consume a small amount of memory and CPU. On a typical VPS, the daemon uses around 50–100 MB of RAM, and each container adds a few MB for its network namespace and cgroups. This is negligible compared to the applications’ footprint.

How to Estimate Your VPS Capacity

To find the practical limit for your specific workload, follow these steps:

  1. Measure baseline usage: Run your containerized application and check its memory and CPU usage with docker stats. Record the average and peak values.
  2. Reserve host overhead: Subtract the OS and Docker daemon usage (typically 200–500 MB) from your total RAM.
  3. Divide the remaining RAM by the per-container memory footprint. For example, if your VPS has 4 GB RAM, the OS uses 500 MB, and each container uses 300 MB, you can run about 11 containers (3.5 GB / 0.3 GB).
  4. Consider CPU: If your containers are CPU-bound, estimate how many can run concurrently without exceeding 100% of a core. For a 2-core VPS, if each container uses 0.5 cores at peak, you can run 4 at full load, but many more if they are idle.
  5. Add a safety margin: Always leave at least 20% headroom for spikes and system processes.

For example, a typical WordPress container might use 200–400 MB RAM, while a lightweight static site server (like Nginx serving small files) might use only 10–20 MB. A database container (PostgreSQL or MySQL) can easily use 1 GB or more, depending on cache settings.

Common Problems When Running Many Containers

Even if you have enough RAM and CPU, you may hit other limits:

  • Port exhaustion: If you map each container to a unique host port, you have a hard limit of ~65,000 ports. In practice, you’ll run out of memory long before that, but it’s still a consideration.
  • Reverse proxy configuration: To run many web services on port 80/443, you need a reverse proxy (like Nginx or Traefik) that routes based on domain names. This adds a small overhead but is essential.
  • Disk inodes: Each container layer creates files, and the filesystem has a limit on the number of inodes (files). If you create hundreds of containers, you might exhaust inodes even if disk space is fine. Check with df -i.
  • Log growth: Containers that write logs to stdout can fill up disk quickly. Configure log rotation (e.g., --log-opt max-size=10m) to avoid this.
  • Monitoring and management: With many containers, manually managing them becomes error-prone. Use Docker Compose for groups of related containers and consider orchestration tools like Docker Swarm or Kubernetes if you need to scale across multiple hosts.

Comparison: Containers vs. Virtual Machines

It’s helpful to compare containers to VMs to understand why the limit is so different. A VM includes a full OS, so each VM might use 1–2 GB RAM just for the OS. A container shares the host OS, so the per-instance overhead is often just tens of megabytes. This is why you can run hundreds of containers on a single VPS but only a handful of VMs with the same resources.

However, containers are less isolated. A vulnerability in the kernel or a misconfigured container can affect the host and other containers. VMs provide stronger isolation because each has its own kernel. For multi-tenant or high-security workloads, VMs are safer. For most internal services, containers are perfectly fine.

Cost and Efficiency Considerations

Running more containers on one VPS can save money because you avoid paying for multiple VPS instances. For example, instead of renting five 2 GB VPSs for five small apps, you could rent one 8 GB VPS and run all five containers there, often at a lower total cost. However, you also create a single point of failure: if the VPS goes down, all services go down. Consider your uptime requirements and whether you need redundancy.

Also, be aware of the VPS provider’s fair-use policies. Some providers throttle CPU if you consistently use a high percentage for long periods. Check your provider’s terms to avoid surprises.

Practical Sizing Examples

To give you a concrete idea, here are rough estimates for common workloads (assuming a modern VPS with SSD and 2 vCPUs):

  • 1 GB RAM VPS: 5–10 tiny containers (static sites, lightweight bots), or 2–3 small web apps, or 1 database + 1 app.
  • 4 GB RAM VPS: 20–40 lightweight containers, or 5–10 typical web apps (each ~300 MB), or 2–3 databases plus apps.
  • 16 GB RAM VPS: 80–150 lightweight containers, or 20–40 typical web apps, or a mix of databases and apps.

These numbers assume each container is idle most of the time. If your containers are constantly busy (e.g., video encoding, data processing), you’ll need to adjust based on CPU usage, not just RAM.

Frequently Asked Questions

Is there a hard limit to the number of containers Docker can run?

Not really. Docker itself imposes no fixed limit; the limit is the host’s resources. However, the Linux kernel has a default maximum number of processes (often around 30,000 or more) and a limit on open file descriptors, which could eventually be reached if you run thousands of containers. In practice, you’ll hit memory or CPU limits first.

Can I run 100 containers on a 2 GB VPS?

Yes, if each container uses very little memory (e.g., 10–20 MB) and is mostly idle. For example, 100 tiny Alpine-based containers running a simple HTTP server might use around 2 GB total. But if any container uses more memory, you’ll quickly run out. Always test with your actual workload.

How do I check how much memory each container uses?

Use the docker stats command. It shows live CPU, memory, network I/O, and disk I/O for each running container. You can also use docker inspect to see memory limits if you set them.

Should I set memory limits on containers?

It’s a good practice to set memory limits (--memory or in Docker Compose mem_limit) to prevent one container from consuming all RAM and causing OOM kills for others. Set a limit slightly above the container’s normal usage, and monitor for any OOM events.

Does using Docker Swarm or Kubernetes allow more containers on one VPS?

No, they don’t increase the resource limit; they help manage containers across multiple hosts. On a single host, they add a small overhead (especially Kubernetes, which runs several system pods). For a single VPS, Docker Compose is usually sufficient and lighter.

What happens if I run out of memory?

The Linux kernel’s Out-Of-Memory (OOM) killer will terminate processes to free memory. This often kills the container’s main process, causing the container to exit. You’ll see errors in the container logs or system logs. To avoid this, monitor memory usage and set limits.

Related

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 →