How to Set Memory Limit on a Docker Container

In this post, we will walk you through how to set a memory limit on a docker container. Docker containers are becoming more and more popular as the technology advances. In order to give your application enough RAM, it is important that you configure the right amount of memory for each container. This blog post will cover all the steps required to ensure that your applications have enough resources available for optimal performance!

The first thing that you need to do is figure out the total amount of memory needed for all your containers. You can easily get this number by multiplying each container’s RAM requirements times the number of containers running simultaneously on one server. Now, take a look at how many physical resources are available for use on your machine and subtract them from the total RAM available on your machine. The number you have left is how much memory can be allocated to docker containers.

If you want to set the memory limit only for a specific container, then all you have to do is add –memory-swap flag.


docker run -it --name deploy1  --memory=512m ubuntu /bin/bash

In this example, we are setting the Nginx container to have a swap limit of 256MB. We also set up a WordPress database with 2048MB RAM and no swap limit.