【问题标题】:What unit does the docker run "--memory" option expect?docker run“--memory”选项期望什么单位?
【发布时间】:2018-03-22 04:55:56
【问题描述】:

我想将 Docker 容器的内存限制为 1 GB。根据文档,我们可以使用--memory 选项指定所需的内存限制:

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

但是,documentation 没有描述页面上任何地方的参数的格式或单位

--memory , -m        内存限制

我应该向--memory 和其他相关选项(如--memory-reservation--memory-swap)提供哪些单位?只是字节?

【问题讨论】:

    标签: docker


    【解决方案1】:

    就我而言,RTFM 的经典案例。 --memory 选项支持单位后缀,因此我们不需要计算确切的字节数:

     -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 选项也支持此约定。

    【讨论】:

      【解决方案2】:

      取自docker documentation

      限制容器对内存的访问 Docker 可以强制执行硬内存 限制,允许容器使用不超过给定的数量 用户或系统内存,或软限制,允许容器 使用尽可能多的内存,除非满足某些条件,例如 当内核检测到主机内存不足或争用时 机器。其中一些选项单独使用时会产生不同的效果 或者当设置了多个选项时。

      这些选项大多采用正整数,后跟 bkmg,表示字节、千字节、兆字节或千兆字节。

      This page 还包含一些关于在 Windows 上运行 docker 时内存限制的额外信息。

      【讨论】:

        【解决方案3】:
        docker run -m 50m <imageId> <command...> 
        

        这是应该的。这会强制 docker 容器使用 50m 的内存。一旦它尝试使用更多,它将被关闭。

        但是使用 free -m 您将无法看到与容器内存使用相关的任何内容。您必须进入它才能看到允许的内存。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-02-08
          • 1970-01-01
          • 2023-03-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-09-24
          相关资源
          最近更新 更多