当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
在进行任何配置更改后,建议进行性能测试以验证优化效果,并确保备份重要数据,以防万一。