CentOS Docker 安装

CentOS / RHEL 系用 yum 仓库安装 Docker,以及防火墙与 SELinux 的处理。

Beginner
Key PointsCentOS 用 yum-config-manager 添加 docker-ce 仓库;装完记得开 firewalld 端口、处理 SELinux 的挂载标签。

卸载旧版本

Bash
sudo yum remove -y docker docker-client docker-common docker-engine podman runc

添加仓库并安装

Bash
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install -y docker-ce docker-ce-cli containerd.io \
                    docker-buildx-plugin docker-compose-plugin
sudo systemctl enable --now docker

国内网络

Access to download.docker.com is often slow from mainland China; switch to a mirror repo first.

Bash
sudo yum-config-manager --add-repo \
  https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

SELinux 与挂载

With SELinux enforcing, containers cannot read host directories by default. Add :z (shared) or :Z (private) to relabel the mount — far safer than disabling SELinux.

Bash
docker run -d -v /data/html:/usr/share/nginx/html:z -p 80:80 nginx

防火墙

Bash
# Docker 会自己写 iptables,firewalld 要放行对外端口
sudo firewall-cmd --permanent --add-port=80/tcp
sudo firewall-cmd --reload
On CentOS 8+ Docker and podman conflict (podman ships a docker shim). Keep one container engine per host.

Friends & Partners