Docker Community Forums

Share and learn in the Docker community.

How to limit the CPU and memory of a docker container?


I am trying to limit the memory of the docker container. i.e currently for “free -m” command inside the container it is showing the entire host memory info.

$: free -m on my host machine

$: free -m inside the container

I need to limit the docker container memory 3 or 4GB. for that I tried to use "docker run -d --memory=“4gb” {image_name} " and "docker run -d -m=“4gb” {image_name} ". Even, I tried by giving “4100m” as memory But, nothing is working for me. May I know how can I limit the memory for the docker container please. Can anybody help me on this please. kind request.

Siddhartha V

how to Set Docker Memory and CPU Usage Limit
Configure System to Enable Limiting Resources.
Limit Docker Container Memory Access. Set Maximum Memory Access. Set Swap to Disk Memory Limit. Set Soft Limit to Container Memory.
Limit Docker Container CPU Usage.

The --memory flag is how much RAM the container can use.
First you can try to change the value while the container is running by executing:

docker update -m 4G {container_name}

Please note that you might also need to specify the --memory-swap flag at the same time.
Otherwise you could limit the kernel memory (–kernel-memory), however this is deprecated since Docker 20.10.

Additionally to check the stats of the container specifically, run:
docker stats {container_name}


Hello @ibosk , Thank you.

It worked. But it is not working as I thought. Actually what I thought/expecting was If I give specific memory then, the container will have memory. i.e if I run "free -m" inside the container then I should see “4GB” as the total memory. But it is again showing the entire host memory.

But, If I run “docker stats container_name” then there I can see the memory as 4GB. This is quite confusing here. May I get the explanation for this case please. kind request.


Siddhartha V