Docker限制容器性能的方法
在docker-compose.yml的末尾添加 cpus: “1.5”和 mem_limit: “512m”,例:
version: "3.8"
services:
jellyfin:
image: jellyfin/jellyfin:latest
container_name: jellyfin
user: 1000:1000
network_mode: bridge
ports:
- "8096:8096"
- "8920:8920"
restart: unless-stopped
volumes:
- ./config:/config
- ./cache:/cache
- /mnt/hdd_sg:/media
environment:
- TZ=Asia/Tokyo
- JELLYFIN_PublishedServerUrl=http://raspi4b.local:8096
cpus: "1.5"
mem_limit: "512m"
有时你添加上述配置之后启动容器时会报warning:
Your kernel does not support memory limit capabilities or the cgroup is not mounted. Limitation discarded. 0.0s
这是因为树莓派等设备默认不打开内存限制选项。
下面的命令可以确认这一点:
ls /sys/fs/cgroup/memory*
如果找不到文件那就是没启用。
更改/boot/cmdline.txt文件,在末尾添加以下内容:(根据系统不同可能是/boot/firmware/cmdline.txt)
cgroup_enable=memory cgroup_memory=1
重启之后再看/sys/fs/cgroup/memory,应该就会有文件了,此时再重新启动容器就可以启用内存限制。