我想将 Docker 容器的内存限制为 1 GB。根据文档,我们可以使用 --memory
指定所需的内存限制选项:
$ docker run --memory <size> ...
然而,documentation没有在页面上的任何地方描述参数的格式或单位:
--memory , -m Memory limit
我应该提供什么单位给
--memory
和其他相关选项,如 --memory-reservation
和 --memory-swap
?只是字节?
最佳答案
就我而言,RTFM 的经典案例。 --memory
option 支持单位后缀,因此我们不需要计算确切的字节数:
-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).
因此,要按照问题中的描述启动具有 1 GB 内存限制的容器,这两个命令都将起作用:
$ docker run --memory 1g ...
$ docker run --memory 1073741824 ...
--memory-reservation
和 --memory-swap
选项也支持这个约定。
关于docker - docker run "--memory"选项期望什么单位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46672578/