當Docker在CentOS上運行緩慢時,可以通過以下幾種方法進行優化:
echo "* soft nofile 65536" >> /etc/security/limits.conf
echo "* hard nofile 65536" >> /etc/security/limits.conf
echo "net.core.rmem_max = 16777216" >> /etc/sysctl.conf
echo "net.core.wmem_max = 16777216" >> /etc/sysctl.conf
echo "net.ipv4.tcp_rmem = 4096 87380 16777216" >> /etc/sysctl.conf
echo "net.ipv4.tcp_wmem = 4096 65536 16777216" >> /etc/sysctl.conf
sysctl -p
swapoff -a
echo "swapoff -a" >> /etc/rc.local
/etc/docker/daemon.json
文件:{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": { "max-size": "100m", "max-file": "3" },
"storage-driver": "overlay2",
"storage-opts": [ "overlay2.override_kernel_check=true" ],
"insecure-registries" : [],
"registry-mirrors": ["https://mirrors.aliyun.com"],
"debug": false,
"experimental": false,
"features": { "buildkit": true }
}
systemctl restart docker
docker run -it --cpus=2 --memory="512m" ubuntu:latest
--network host
讓容器共享主機的網絡命名空間。--sysctl
參數傳遞內核參數給容器:docker run --sysctl net.ipv4.ip_forward=1 ubuntu:latest
overlay2
存儲驅動通常性能較好,但也可以嘗試其他驅動如 aufs
或 btrfs
。docker stats
命令實時監控容器的資源使用情況。logrotate /var/lib/docker/containers/*.log
在進行任何配置更改后,建議進行性能測試以驗證優化效果,并確保備份重要數據,以防萬一。