A single KVM VPS can run a surprising number of Docker containers, but only if you set four things up front: memory and CPU limits, log rotation, restart policies and named volumes. Without them, one runaway container or one chatty log file can take the whole server down.
Prerequisites
You need a Linux VPS with root or sudo access, Docker Engine installed from Docker's own repository, and a rough idea of how much RAM each service needs. KVM gives your VPS its own kernel, which is what lets Docker use cgroups for limits. StreamData's KVM VPS plans run from 1 GB to 128 GB of RAM, so pick a size from the total of your container limits plus about a gigabyte for the host.
Step 1: Limit memory and CPU
Docker's resource constraints documentation states that by default a container has no resource limits and can use whatever the host kernel scheduler allows. On a small VPS that means a memory leak in one container invites the kernel's out-of-memory killer to choose a victim, and the documentation warns that Docker lowers the OOM priority of the daemon but not of containers, so containers are the more likely casualties.
Set limits when you start each container:
docker run -d --name api \
--memory=512m --memory-swap=512m \
--cpus=1.5 \
myorg/api:1.4--memoryis the hard ceiling; the documented minimum is 6 MB.--memory-swapis memory plus swap, and it only applies when--memoryis set. Setting it equal to--memorygives the container no swap.--cpus=1.5allows at most one and a half CPUs; it is equivalent to a CFS period of 100000 with a quota of 150000.--cpu-sharesis a relative weight (default 1024) that only matters under contention and reserves nothing.- Do not use
--oom-kill-disableunless-mis also set, because without a limit the host itself can run out of memory.
Step 2: Rotate logs before they fill the disk
Docker's logging documentation says the default driver is json-file, which does not rotate logs by default and can exhaust disk space; the page recommends the local driver for most setups. If you keep json-file, set rotation in /etc/docker/daemon.json:
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}Restart Docker afterwards. The documentation is explicit that changes apply to newly created containers, so existing ones must be re-created. Confirm with:
docker info --format '{{.LoggingDriver}}'
docker inspect -f '{{.HostConfig.LogConfig.Type}}' apiFor chatty applications, --log-opt mode=non-blocking buffers messages so the application does not stall when logging backs up. The buffer defaults to 1m and new messages are dropped if it fills, so choose it when availability matters more than every log line.
Step 3: Choose restart policies
Docker's guide on starting containers automatically describes four policies: no (the default), on-failure[:max-retries], always and unless-stopped. It also notes that a policy takes effect only after the container has run successfully for at least ten seconds, which prevents restart loops for containers that never start.
docker run -d --name redis --restart unless-stopped redisFor most services unless-stopped is the right choice: the container comes back after a reboot or daemon restart, but stays down if you stopped it on purpose. Do not combine restart policies with systemd or another process manager for the same container, because the two will conflict.
Step 4: Keep data in volumes
The documentation on volumes says a container's writable layer is deleted with the container, while volume data survives removal. Docker stores a volume under /var/lib/docker/volumes/NAME/_data, and volumes are easier to back up and migrate than bind mounts.
docker volume create pgdata
docker run -d --name db --mount source=pgdata,target=/var/lib/postgresql/data postgres:16To back up, the documentation suggests starting a throwaway container with --volumes-from, mounting a host directory, and archiving the volume into it. Copy that archive off the VPS. Unused volumes are not cleaned up automatically, so review them before running docker volume prune.
Verify
docker stats --no-stream
docker ps --format 'table {{.Names}}\t{{.Status}}'
df -h /var/lib/dockerCheck that each container respects its memory ceiling, that the log directory is not growing without bound, and that a reboot brings every service back.
Common mistakes
- Running without limits, then discovering which container the kernel kills.
- Changing
daemon.jsonand forgetting that old containers keep old settings. - Storing a database in the container's writable layer.
- Opening database ports to the internet instead of a private Docker network.
- Never testing a restore from a volume backup.
If the combined limits outgrow one machine, move up to a larger plan or a dedicated server. Backups remain the customer's responsibility unless a written order says otherwise, so schedule them yourself.