RedHat Docker Install Script - CodeSwordsman

One-click Docker install script for RHEL 8 / 9: checks subscription status, removes conflicting podman/runc, configures the Aliyun Docker CE repo and installs docker-ce with buildx and the compose plugin, plus SELinux, firewalld and uninstall notes.

RHEL 8 / 9 · Docker 27.xUpdated 2026-10-3024 commands

Preparation & Cleanup

Confirm the RHEL version and repos; default repos need an active subscription

cat /etc/redhat-release
dnf repolist

Check subscription status, then register and auto-attach if needed

sudo subscription-manager status
sudo subscription-manager register --auto-attach

RHEL ships podman and runc; runc conflicts with containerd.io, so remove it first

sudo dnf remove -y podman buildah runc

Provides dnf config-manager for adding third-party repos

sudo dnf install -y dnf-plugins-core

One-Click Install Script

Save as install-docker-rhel.sh and run it as root to finish the install

#!/bin/bash
set -euo pipefail

echo "当前系统: $(cat /etc/redhat-release)"

# 订阅检查,未注册时给出提示而不中断
if command -v subscription-manager >/dev/null 2>&1; then
  subscription-manager status >/dev/null 2>&1 || echo "警告: 订阅未注册,请先执行 subscription-manager register"
fi

# 移除与 containerd.io 冲突的包
sudo dnf remove -y podman buildah runc || true
sudo dnf install -y dnf-plugins-core

# 添加阿里云 Docker CE 仓库
sudo dnf config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/rhel/docker-ce.repo

# 安装 Docker 引擎与插件
sudo dnf install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

# 开机自启并验证
sudo systemctl enable --now docker
docker --version
docker compose version

If the repo returns 404, use the centos path or the official download.docker.com repo.

Make it executable and run as root to avoid repeated sudo prompts

chmod +x install-docker-rhel.sh
sudo bash install-docker-rhel.sh

Step-by-Step Install

Add the Docker CE repo and confirm the repo file exists

sudo dnf config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/rhel/docker-ce.repo
ls /etc/yum.repos.d/ | grep docker

Refresh metadata and list available versions from the Docker repo

sudo dnf makecache
dnf list docker-ce --showduplicates | head

Install the engine, CLI, containerd, buildx and the compose plugin

sudo dnf install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Use --allowerasing to let dnf remove conflicting runc or podman packages

sudo dnf install -y --allowerasing docker-ce

Verify all three work; compose is a plugin, not the legacy binary

docker --version
docker compose version
docker buildx version

Post-Install Configuration

Enable at boot, start now and confirm the active state

sudo systemctl enable --now docker
systemctl status docker --no-pager

Join the docker group to skip sudo; newgrp applies it immediately

sudo usermod -aG docker "$USER"
newgrp docker

The docker group is effectively root-level access; grant it carefully.

Set a registry mirror from your cloud console instead of public links

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<'EOF'
{
  "registry-mirrors": ["<你的镜像加速地址>"]
}
EOF

Reload and restart; the mirror should appear in docker info output

sudo systemctl daemon-reload
sudo systemctl restart docker
docker info | grep -A2 "Registry Mirrors"

Keep mirror and log settings in one JSON; separate writes overwrite each other

sudo tee /etc/docker/daemon.json <<'EOF'
{
  "registry-mirrors": ["<你的镜像加速地址>"],
  "log-driver": "json-file",
  "log-opts": { "max-size": "100m", "max-file": "3" }
}
EOF

Verify & Troubleshoot

Run the official test image to confirm the engine and image pulling

docker run --rm hello-world

Check these logs on failure; malformed daemon.json is the usual cause

systemctl status docker --no-pager
journalctl -u docker -n 50 --no-pager

Under Enforcing SELinux, mount with :z or :Z or the container cannot read the path

getenforce
docker run --rm -v /data/html:/usr/share/nginx/html:z nginx:1.25

Docker publishes ports outside firewalld, so open them explicitly for external access

sudo firewall-cmd --add-port=8080/tcp --permanent
sudo firewall-cmd --reload

RHEL 8/9 default to cgroup v2 with overlay2; check kernel args if v1 is expected

docker info | grep -iE "cgroup|storage driver"

Uninstall & Cleanup

Remove Docker packages while keeping data in /var/lib/docker

sudo dnf remove -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Risky

Delete all images, containers and volumes with no recovery

sudo rm -rf /var/lib/docker /var/lib/containerd

Remove the Docker repo so later installs do not hit a stale source

sudo rm -f /etc/yum.repos.d/docker-ce.repo
sudo dnf makecache

This website uses cookies to ensure you get the best experience on our website. Privacy Policy.