










创建文件 /etc/systemd/system/docker_limit.slice:
[Unit]
Description=Docker global resource slice
[Slice]
CPUAccounting=true
MemoryAccounting=true
CPUQuota=360%
MemoryHigh=12G
MemoryMax=14.4G
执行:
sudo systemctl daemon-reload
sudo systemctl enable --now docker_limit.slice
编辑 /etc/docker/daemon.json:
{
"exec-opts": ["native.cgroupdriver=systemd"],
"cgroup-parent": "docker_limit.slice"
}
执行:
sudo systemctl restart docker
无需重建镜像,直接重启即可迁移到新 slice:
docker restart <容器ID或名称>
# 获取容器 PID
PID=$(docker inspect -f '{{.State.Pid}}' <容器ID>)
# 查看 cgroup 路径
cat /proc/$PID/cgroup
正常输出应包含:0::/docker_limit.slice/docker-<容器ID>.scope
docker info 中 Cgroup Driver 为 systemd,否则 slice 不生效。invalid slice name,请检查 systemctl is-active docker_limit.slice 是否为 active。restart docker;旧容器必须执行 restart 才会进入新 slice。daemon.json 中的 cgroup-parent 必须带前导斜杠(/docker_limit.slice)。CPUQuota 按核数×90%计算(如4核设360%),内存按总容量×90%设置。此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。