Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set RAM memory of a Docker container by terminal or DockerFile

I need to have a Docker Container with 6gb of RAM memory. I tried this command:

docker run -p 5311:5311 --memory=6g my-linux

But it doesn't work because I logged in to the Docker Container and I checked the amount of memory available. This is the output which shows there are only 2gb available:

>> cat /proc/meminfo

MemTotal:        2046768 kB
MemFree:         1747120 kB
MemAvailable:    1694424 kB

I tried setting the preferences -> advance in the Docker Application. If I set 6gb, it works... I mean, I have a container with 6gb MemTotal. In this way all my containers will have 6gb...

I was wondering how to allocate 6gb of memory for only one container using some commands or setting something in the Docker File. Any help?

like image 238
dventi3 Avatar asked Jun 28 '17 15:06

dventi3


People also ask

How do I allocate more RAM to a docker container?

Within the command, specify how much memory you want to dedicate to that specific container. The value of memory_limit should be a positive integer followed by the suffix b, k, m, or g (short for bytes, kilobytes, megabytes, or gigabytes). For example, to limit the container with 1 GB of RAM, add --memory="1g" .

How much RAM is my docker container using?

Run the docker stats command to display the status of your containers. Memory is listed under the MEM USAGE / LIMIT column. This provides a snapshot of how much memory the container is utilizing and what it's memory limit is.

Does docker limit memory by default?

Docker does not apply memory limitations to containers by default. The Host's Kernel Scheduler determines the capacity provided to the Docker memory. This means that in theory, it is possible for a Docker container to consume the entire host's memory.

How much RAM should docker have?

Minimum: 8 GB; Recommended: 16 GB.


1 Answers

Don't rely on /proc/meminfo for tracking memory usage from inside a docker container. /proc/meminfo is not containerized, which means that the file is displaying the meminfo of your host system.

Your /proc/meminfo indicates that your Host system has 2G of memory available. The only way you'll be able to make 6G available in your container without getting more physical memory is to create a swap partition.

Once you have a swap partition larger or equal to ~4G, your container will be able to use that memory (by default, docker imposes no limitation to running containers).

If you want to limit the amount of memory available to your container explicitly to 6G, you could do docker run -p 5311:5311 --memory=2g --memory-swap=6g my-linux, which means that out of a total memory limit of 6G (--memory-swap), upto 2G may be physical memory (--memory). More information about this here.

There is no way to set memory limits in the Dockerfile that I know of (and I think there shouldn't be: Dockerfiles are there for building containers, not running them), but docker-compose supports the above options through the mem_limit and memswap_limit keys.

like image 143
Georg Grab Avatar answered Oct 12 '22 20:10

Georg Grab