dventi3
dventi3

Reputation: 991

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?

Upvotes: 7

Views: 12916

Answers (1)

Georg Grab
Georg Grab

Reputation: 2301

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.

Upvotes: 8

Related Questions