A Little Bit More on Docker Memory Management

Jason Bell
3 min readJan 6, 2023

Docker is a containerization platform that allows developers to package and deploy applications in a lightweight, portable environment. Memory management and how to use it isn’t a well discussed topic from my searching around for the work I’m doing on Synthetica Data Engine. It’s one of the key aspects of managing Docker containers and it can bite you badly if you do not address it. In this article, I will discuss some best practices and techniques for managing memory usage in Docker containers.

Memory Limits in Docker

Docker allows you to set resource limits for your containers, including the amount of memory that can be used. By setting a memory limit, you can prevent a single container from consuming all of the available memory on the host machine and potentially causing the host to become unstable or crash.

To set a memory limit for a Docker container, you can use the –memory flag when starting the container. For example:

docker run -it --memory 256m my_funky_image:latest

This will start a container with a memory limit of 256 megabytes. If the container exceeds this limit, it will be terminated.

It’s important to note that setting a memory limit does not guarantee that the container will be able to use that much memory. The host machine may not have enough available memory to allocate to the container. In this case, the container will be forced to use swap space, which can significantly impact performance.

Memory Reservations in Docker

In addition to setting a memory limit, you can also set a memory reservation for a container using the –memory-reservation flag. A memory reservation is a guaranteed amount of memory that will always be available to the container, regardless of the memory usage of other containers on the host.

For example:

docker run -it --memory-reservation 128m my_funky_image:latest

This will start a container with a guaranteed reservation of 128 megabytes of memory. This can be useful for ensuring that critical containers always have access to the resources they need, even in a resource-constrained environment.

Memory Swapping in Docker

As mentioned earlier, if a container exceeds its memory limit, it will be terminated. However, you can also configure Docker to allow a container to use swap space if it exceeds its memory limit. This is known as memory swapping.

To enable memory swapping for a container, use the –memory-swap flag when starting the container. The –memory-swap flag should be set to a value greater than the –memory flag.

docker run -it --memory 256m --memory-swap 512m my_funky_image:latest

This will start a container with a memory limit of 256 megabytes and a memory swap limit of 512 megabytes. If the container exceeds its memory limit, it will be able to use swap space to continue running. However, using swap space can significantly impact performance, so it should be used with caution.

Monitoring Memory Usage in Docker

It’s important to monitor the memory usage of your Docker containers to ensure that they are operating within their limits and reservations. You can use the docker stats command to view real-time memory usage statistics for your containers.

docker stats --format "table {{.Name}}\t{{.MemUsage}}"
NAME MEM USAGE
my_funky_image:latest 128MiB / 256MiB

This will display the memory usage for each container, including the total memory usage and the memory limit. You can use this information to identify containers that are using excessive memory and take steps to optimize their usage.

--

--

Jason Bell

A polymath of ML/AI, expert in container deployments and engineering. Author of two machine learning books for Wiley Inc.