Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What unit does the docker run "--memory" option expect?

Tags:

docker

I'd like to constrain the memory of a Docker container to 1 GB. According to the documentation, we can specify the desired memory limit using the --memory option:

$ docker run --memory <size> ...

However, the documentation does not describe the format or units for the argument anywhere on the page:

--memory , -m           Memory limit

What units should I supply to --memory and other related options like --memory-reservation and --memory-swap? Just bytes?

like image 539
Cy Rossignol Avatar asked Oct 10 '17 17:10

Cy Rossignol


People also ask

Does Docker run system memory?

By default, Docker containers have access to the full RAM and CPU resources of the host. Leaving them to run with these default settings may lead to performance bottlenecks. If you don't limit Docker's memory and CPU usage, Docker can use all the systems resources.

How much memory is required for Docker?

The maximum amount of memory the container can use. If you set this option, the minimum allowed value is 6m (6 megabytes). That is, you must set the value to at least 6 megabytes.

What is default Docker memory limit?

By default, the container can swap the same amount of assigned memory, which means that the overall hard limit would be around 256m when you set --memory 128m .

How much memory is my Docker container using?

If you need more detailed information about a container's resource usage, use the /containers/(id)/stats API endpoint. On Linux, the Docker CLI reports memory usage by subtracting cache usage from the total memory usage.


1 Answers

Classic case of RTFM on my part. The --memory option supports a unit suffix so we don't need to calculate the exact byte number:

 -m, --memory=""
      Memory limit (format: <number>[<unit>], where unit = b, k, m or g)

   Allows you to constrain the memory available to a container. If the
   host supports swap memory, then the -m memory setting can be larger
   than physical RAM. If a limit of 0 is specified (not using -m), the
   container's memory is not limited. The actual limit may be rounded up
   to a multiple of the operating system's page size (the value would be
   very large, that's millions of trillions).

So, to start a container with a 1 GB memory limit as described in the question, both of these commands will work:

$ docker run --memory 1g ... 
$ docker run --memory 1073741824 ...

The --memory-reservation and --memory-swap options also support this convention.

like image 196
Cy Rossignol Avatar answered Oct 10 '22 15:10

Cy Rossignol