
1. RockyLinux 9.7安装Docker全攻略作为RHEL的完美替代品RockyLinux 9.7在企业级应用中越来越受欢迎。最近在部署一套新的CI/CD环境时我选择了RockyLinux 9.7作为基础系统并需要在其上安装Docker。整个过程看似简单但实际遇到了不少坑特别是与SELinux和firewalld的兼容性问题。本文将分享从系统准备到Docker稳定运行的完整过程包含我在生产环境中验证过的配置方案。2. 环境准备与系统检查2.1 系统基础配置确认在开始安装前必须确保系统满足Docker运行的基本要求。执行以下命令检查内核版本和系统架构uname -r # 需要3.10或更高版本 cat /etc/redhat-release # 确认是RockyLinux 9.7 lscpu # 检查CPU架构(x86_64/aarch64)注意如果使用虚拟机务必在BIOS中开启虚拟化支持(VT-x/AMD-V)。我在KVM环境中曾因未开启导致docker.service启动失败。2.2 必要的依赖安装RockyLinux 9.7默认的软件仓库可能不包含最新版Docker需要先添加必要的仓库sudo dnf install -y dnf-plugins-core sudo dnf config-manager --add-repohttps://download.docker.com/linux/centos/docker-ce.repo安装基础工具链sudo dnf install -y device-mapper-persistent-data lvm2 git curl wget3. Docker安装详细步骤3.1 官方仓库安装Docker CE执行以下命令安装Docker社区版及其组件sudo dnf makecache sudo dnf install -y docker-ce docker-ce-cli containerd.io安装完成后验证版本docker --version # 应显示20.10.x或更高 containerd --version # 应显示1.6.x或更高3.2 关键配置调整编辑daemon.json配置文件首次需要创建sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json EOF { exec-opts: [native.cgroupdriversystemd], log-driver: json-file, log-opts: { max-size: 100m }, storage-driver: overlay2, storage-opts: [ overlay2.override_kernel_checktrue ] } EOF经验在RockyLinux 9.7上必须设置storage-driver:overlay2否则会遇到存储驱动不兼容的警告。4. 系统集成与权限配置4.1 SELinux与Firewalld整合RockyLinux默认启用SELinux需要特别处理sudo setenforce 0 # 临时设置为permissive模式 sudo sed -i s/^SELINUXenforcing/SELINUXpermissive/ /etc/selinux/config配置firewalld放行Docker流量sudo firewall-cmd --permanent --zonepublic --add-masquerade sudo firewall-cmd --permanent --zonepublic --add-port2376/tcp sudo firewall-cmd --permanent --zonepublic --add-port2377/tcp sudo firewall-cmd --permanent --zonepublic --add-port7946/tcp sudo firewall-cmd --permanent --zonepublic --add-port7946/udp sudo firewall-cmd --permanent --zonepublic --add-port4789/udp sudo firewall-cmd --reload4.2 用户组与权限管理将当前用户加入docker组避免sudosudo usermod -aG docker $USER newgrp docker # 立即生效验证非root用户权限docker run hello-world # 应能正常执行5. 服务管理与优化5.1 系统服务配置启用并启动Docker服务sudo systemctl enable --now docker sudo systemctl status docker # 检查状态配置日志轮转防止日志爆满sudo tee /etc/logrotate.d/docker EOF /var/lib/docker/containers/*/*.log { rotate 7 daily compress delaycompress missingok copytruncate } EOF5.2 性能调优建议修改systemd服务参数提升性能sudo mkdir -p /etc/systemd/system/docker.service.d sudo tee /etc/systemd/system/docker.service.d/override.conf EOF [Service] ExecStart ExecStart/usr/bin/dockerd -H fd:// --containerd/run/containerd/containerd.sock --default-ulimit nofile102400:102400 EOF应用配置并重启服务sudo systemctl daemon-reload sudo systemctl restart docker6. 常见问题解决方案6.1 虚拟化支持问题如果遇到virtualization support not detected错误确认BIOS中已开启VT-x/AMD-V检查内核模块加载lsmod | grep kvm对于物理机可能需要安装kvm驱动sudo dnf install -y qemu-kvm libvirt virt-install bridge-utils sudo systemctl enable --now libvirtd6.2 存储驱动问题当看到storage-driver相关警告时确认/etc/docker/daemon.json配置正确检查当前存储驱动docker info | grep Storage Driver如果使用devicemapper建议改用overlay2sudo dnf install -y yum-utils device-mapper-persistent-data lvm26.3 镜像加速配置为提升国内拉取镜像速度建议配置镜像加速器sudo tee /etc/docker/daemon.json EOF { registry-mirrors: [ https://registry.cn-hangzhou.aliyuncs.com, https://docker.mirrors.ustc.edu.cn ] } EOF sudo systemctl restart docker7. 生产环境验证7.1 基础功能测试运行测试容器验证基础功能docker run -it --rm alpine sh -c echo Hello from Docker! docker run -d -p 80:80 nginx # 测试端口映射 curl localhost # 应看到Nginx欢迎页7.2 持久化存储验证测试卷挂载功能mkdir ~/test-volume docker run -v ~/test-volume:/data alpine sh -c echo persistent data /data/test.txt cat ~/test-volume/test.txt # 应看到写入内容7.3 网络连通性检查验证容器间通信docker network create test-net docker run -d --name web --network test-net nginx docker run -it --rm --network test-net alpine wget -O- web