Docker containers are supposed to be lightweight, so why is my server running out of RAM?

Docker containers are supposed to be lightweight, so why is my server running out of RAM?

Published Nov 26, 2026, 7:30 AM EST Umair Khurshid is a technology writer and developer with a strong focus on Linux, FreeBSD, cloud infrastructure, and automation. Before focusing on writing, Umair worked as a developer and DevOps engineer building and automating cloud-native systems. Docker containers are lightweight because they share the host operating system’s kernel, eliminating much of the overhead associated with running virtual machines. However, the applications inside those containers still have essentially the same memory requirements as they would if you installed them directly on your server. When deploying a new self-hosted application involves little more than copying a Docker Compose file and running a command, it is easy to underestimate how much software you are actually running. Even if you have a reasonably powerful home server with more than 32GB of RAM, you can eventually find yourself dealing with sluggish applications and containers that unexpectedly restart. Fortunately, there are several things you can do before spending money on a RAM upgrade. Lightweight containers can still run heavyweight applications The size of a Docker image tells you very little about memory consumption When you create a VM, you need to allocate resources to an entire guest operating system. Even when your application is idle, the guest OS needs memory for its kernel and background services (the storage is a different story). Docker eliminates much of this overhead by allowing containers to share the host’s Linux kernel. However, running PostgreSQL inside a Docker container does not make its database operations any less memory-intensive. Similarly, an application distributed as a relatively small Alpine Linux-based image can consume several GB of RAM once it starts processing data. The size of a Docker image primarily reflects its storage requirements. Consider something like Immich. Suppose its machine learning container consumes around 500MB of RAM while sitting idle. When you import several thousand photos and initiate facial recognition, its memory consumption could climb to 3GB or more. Meanwhile, its other containers may be generating thumbnails and writing information to PostgreSQL. In this scenario, an Immich deployment that normally consumes around 2GB of RAM could temporarily require more than 5GB. Actual memory consumption depends on the machine learning models you use and how many jobs you process concurrently. This is why estimating the hardware requirements of a home server based solely on the number of containers you intend to run is rarely a good idea. A single application performing machine learning tasks could easily consume more memory than several dozen lightweight networking utilities. You are probably running more applications than you realize Docker Compose makes it easy to accumulate dependencies One of the things I like about Docker Compose is how little effort it takes to deploy applications with fairly complicated dependencies. However, this convenience can make it easy to lose track of what is running on your server. You might have installed eight self-hosted applications, several of which have their own PostgreSQL and Redis instances. Suddenly, you have more than 20 containers running in the background. You can count your running containers using: docker ps -q | wc -l To see how many containers are using each Docker image, run: docker ps --format '{{.Image}}' | sort | uniq -c | sort -rn This can help identify duplicate database instances, although containers using different image tags will appear separately. Suppose you have three applications, each running a PostgreSQL instance configured with 128MB of shared_buffers. Collectively, those instances allocate 384MB to their respective shared buffer pools alone, before accounting for connections and query execution. If all three applications support an external PostgreSQL server, you could potentially run one instance with three separate databases and dedicated credentials. Depending on your workloads, a shared buffer pool of 256MB might be sufficient, saving 128MB in buffer allocations alone. Connection pooling using something like PgBouncer can also reduce the overhead associated with maintaining large numbers of database connections. The problem with this approach is that consolidating databases introduces a shared dependency and can complicate upgrades. If you are not particularly comfortable managing databases, the additional maintenance and troubleshooting involved may not be worth the memory savings. Your applications may be allocating more memory than necessary Some applications automatically determine their memory budgets Certain applications calculate their memory based on how much memory they detect on the system. Without container-level restrictions, that could be the entire memory capacity of your server. Java runtimes generally recognize container memory limits, although older versions may incorrectly calculate their heap sizes based on the host’s RAM. If you are running a Java application, you can explicitly configure its maximum heap size using an option such as -Xmx512m.Node.js provides a similar option for its old-generation heap: --max-old-space-size=512 Some applications also require memory for operations outside their managed heaps. Databases have their own memory management settings. For example, PostgreSQL allows you to configure parameters such as those in postgresql.conf: shared_buffers = 128MB work_mem = 4MB Be particularly careful with work_mem, since multiple operations within a single query can each require memory under this setting. Concurrent queries can considerably increase the total memory consumption. If you are using Redis exclusively as a disposable cache, you could configure it to evict less recently used entries when it reaches a specified memory threshold: services: redis: image: redis:alpine command: redis-server --maxmemory 256mb --maxmemory-policy allkeys-lru Avoid this eviction policy if your application relies on Redis to retain important data or queued jobs. Redis can also consume memory beyond its configured maxmemory threshold. With applications like Immich, reducing the concurrency of background processing jobs can also help keep peak memory consumption manageable, particularly if you are running your home server on older hardware. Docker does not restrict container memory consumption by default A single container can exhaust your server’s available RAM Unless you explicitly configure resource constraints, Docker Engine does not impose an individual memory limit on your containers. An application with a memory leak could gradually consume most of your server’s RAM over several days. When Linux can no longer satisfy memory allocation requests, its out-of-memory (OOM) killer may terminate processes to recover memory. You can prevent an individual container from consuming excessive amounts of memory by configuring limits in your Docker Compose file. services: redis: image: redis:alpine mem_limit: 512m memswap_limit: 512m Both parameters are supported by the Compose Specification. Alternatively, you can configure the memory limit through deploy.resources.limits.memory. If you prefer using the Docker CLI, the equivalent command is: docker run -d --name redis \ --memory=512m \ --memory-swap=512m \ redis:alpine The swap parameter represents the combined memory and swap allowance. Setting it equal to the memory limit prevents the container from using swap on systems that support swap accounting. However, imposing restrictive memory limits without investigating an application’s requirements can introduce problems of its own. An application that regularly needs 2GB of RAM will likely encounter problems if you restrict it to 512MB. Configure the application’s internal memory settings where possible, then use Docker’s resource constraints to protect the rest of your server. Remember to leave some memory available for the host operating system itself. There comes a point when you actually need more RAM The convenience of deploying applications with Docker can make it surprisingly easy to outgrow your existing hardware. Before upgrading, check how much memory Linux actually has available. You might discover that consolidating a few database instances or adjusting background job concurrency is sufficient to accommodate your existing workloads. It is also worth reviewing the containers you have accumulated over time. If you are still running applications you have barely touched in months, shutting them down might make more sense than buying additional RAM to keep them running. However, if your server continues to struggle under normal workloads after you have addressed these issues, a RAM upgrade is probably justified. Docker has already eliminated much of the operating system overhead associated with running multiple applications. There is only so much memory containerization can save you.

Original Source

Read the full article at Howtogeek →

KhanList aggregates and links to publicly available news content. We do not host full articles from third-party sources. Always verify important information with original sources.