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

3

11

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?

Streeter answered 10/10, 2017 at 17:29 Comment(0)
S
13

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.

Streeter answered 10/10, 2017 at 17:31 Comment(0)
R
2

Taken from the docker documentation:

Limit a container’s access to memory Docker can enforce hard memory limits, which allow the container to use no more than a given amount of user or system memory, or soft limits, which allow the container to use as much memory as it needs unless certain conditions are met, such as when the kernel detects low memory or contention on the host machine. Some of these options have different effects when used alone or when more than one option is set.

Most of these options take a positive integer, followed by a suffix of b, k, m, g, to indicate bytes, kilobytes, megabytes, or gigabytes.

This page also includes some extra information about memory limits when running docker on Windows.

Rosemari answered 10/10, 2017 at 17:37 Comment(0)
R
1
docker run -m 50m <imageId> <command...> 

This is how it should be given. This forces the docker container to use 50m of memory. As soon as it tries to use more than that, it will be shut down.

However using free -m you won't be able to see anything related to the container memory usage. you have to go inside it to see allowed memory.

Rowney answered 3/5, 2020 at 14:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.