标签搜索

目 录CONTENT

文章目录

修改已启动Docker容器的内存限制

陈铭
2023-09-15 / 0 评论 / 0 点赞 / 190 阅读 / 362 字 / 正在检测是否收录...

解法

刚启动的容器

# -m 或 --memory:设置内存的使用限额,例如 100M, 2G。
# --memory-swap:设置 内存+swap 的使用限额。不设置的话默认为--memory的两倍
docker run -m 4g --memory-swap -1 <image> <command>

已启动的容器

docker stop <containerId>
docker update <containerId> -m 4g  --memory-swap -1
docker start <containerId>

# 如果只指定了 --memory 则 --memory-swap 默认为 --memory 的两倍
# 如果 --memory-swap 和 --memory 设置了相同值,则表示不使用 Swap
# 如果 --memory-swap 设置为 -1 则表示不对容器使用的 Swap 进行限制
# 如果设置了 --memory-swap 参数,则必须设置 --memory 参数
# 后期 update --memory 时数值不能超过 --memory-swap 的值,否则会报错 Memory limit should be smaller than already set memoryswap limit

查询配置

docker inspect <containerId>

可能的错误情况

情况一

Your kernel does not support swap limit capabilities or the cgroup is not mounted

解法

# 打开/etc/default/grub文件
sudo vi /etc/default/grub
# 加入下面配置
GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1"
# 更新grub系统
sudo update-grub
# 重启机器

情况二

Memory limit should be smaller than already set memoryswap limit, update the memoryswap at the same time

解法

docker 默认没有启用memory-swap交换内存,直接设置了内存问题会出问题,也就是说宿主 swap 支持使用多少则容器即可使用多少
设置内存的同时必须设置–memory-swap
0

评论区