docker - 如何限制容器内存大小?

 qyfdxlwb 发布于 2022-10-25 09:16

我分别用两种linux系统部署Docker0.8,都没法限制容器的内存大小。具体情况如下:

  1. 用CentOS6.5【内核为3.10.5-3.el6.x86_64】做主机,新建容器,得到结果如下:
# docker run -i -t -d -m 512m centos /bin/bash   
WARNING: WARNING: Your kernel does not support swap limit capabilities. Limitation discarded.
9229fe1bac59992337e909cbc5a11f3e0fa81553ad7b136bec95a62181a251fd

该容器新建成功,但不能限制内存大小。

  1. Ubuntu 12.04【内核为3.8.0-38-generic】做主机,并且修正/etc/default/grub,更改$ GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1"之后,新建容器,得到结果如下:
# sudo docker run -i -t -m 512m -d ubuntu /bin/bash
89d629ed37d63b67b49ed8c4c11cab653d61d5b85227bdd10c8a9a0a718c686e

看似未报错,登陆该新建的容器里面查看内存【free命令】,发现并未限制。

大家手头的主机都是什么系统,有没有这种情况?如何解决?盼回复,谢谢!

4 个回答
  • @colin5063

    我是CentOS6.7 应该差不多

    我是修改的/etc/grub.conf 在当前使用的内核的kernel项的最后加上cgroup_enable=memory swapaccount=1

    reboot

    然后 cat /proc/cmdline 就会发现,配置生效了

    2022-10-26 14:43 回答
  • 有人知道Centos6下面这个问题怎么解决?
    我的docker是1.6.2的, Centos是6.6,内核版本是3.10.81

    2022-10-26 14:43 回答
  • 楼主,我用ubuntu虚拟机做实验的时候也遇到这个问题,请参照这里解决
    http://docs.docker.com/articles/runmetrics/
    以下是重点摘录

    If you want to enable memory and swap accounting, you must add the following command-line parameters to your kernel:

    $ cgroup_enable=memory swapaccount=1
    

    On systems using GRUB (which is the default for Ubuntu), you can add those parameters by editing /etc/default/grub and extending GRUB_CMDLINE_LINUX. Look for the following line:

    $ GRUB_CMDLINE_LINUX=""
    

    And replace it by the following one:

    $ GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1"
    

    Then run sudo update-grub, and reboot.

    These parameters will help you get rid of the following warnings:

    WARNING: Your kernel does not support cgroup swap limit.
    WARNING: Your kernel does not support swap limit capabilities. Limitation discarded.
    
    2022-10-26 14:43 回答
  • container中的free命令,看的是Host上的内存。

    要查看container的内存限制用这个:

    cat /cgroup/memory/lxc/{full_container_id}/memory.limit_in_bytes
    
    

    /cgroup/下还可以查看其它资源的限制:

    ll /cgroup/
    总用量 0
    drwxr-xr-x 3 root root 0 3月  17 13:33 blkio
    drwxr-xr-x 3 root root 0 3月  17 13:33 cpu
    drwxr-xr-x 3 root root 0 3月  17 13:33 cpuacct
    drwxr-xr-x 3 root root 0 3月  17 13:33 cpuset
    drwxr-xr-x 3 root root 0 3月  17 13:33 devices
    drwxr-xr-x 3 root root 0 3月  17 13:33 freezer
    drwxr-xr-x 3 root root 0 3月  17 13:33 memory
    drwxr-xr-x 3 root root 0 3月  17 13:33 net_cls
    
    2022-10-26 14:43 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有