CentOS Docker Install Script - CodeSwordsman

One-click Docker install script for CentOS 7 / 8 / Stream 9: detects yum or dnf, switches EOL mirrors to vault, pins the last docker-ce version supported on CentOS 7, configures the Aliyun repo and installs the compose plugin, with SELinux and uninstall notes.

CentOS 7 / 8 / Stream 9 · Docker 25.x / 27.xUpdated 2026-10-3024 commands

Preparation & Repo Fix

Confirm the version and package manager: CentOS 7 uses yum, 8 and Stream 9 use dnf

cat /etc/centos-release
command -v dnf || command -v yum

CentOS 7 is EOL; switch mirrors to vault before installing anything

sudo sed -i 's/mirrorlist/#mirrorlist/g; s|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*.repo
sudo yum clean all && sudo yum makecache

Back up /etc/yum.repos.d first; a bad edit breaks yum entirely.

CentOS 8 is EOL; point repos at the fixed vault version directory

sudo sed -i 's|mirrorlist.centos.org|vault.centos.org|g; s|\$releasever|8.5.2111|g' /etc/yum.repos.d/CentOS-*.repo
sudo dnf clean all && sudo dnf makecache

Remove legacy Docker and install yum-utils for the config-manager command

sudo yum remove -y docker docker-client docker-common docker-engine
sudo yum install -y yum-utils

One-Click Install Script

Save as install-docker-centos.sh and run as root; it picks yum or dnf automatically

#!/bin/bash
set -euo pipefail

echo "当前系统: $(cat /etc/centos-release 2>/dev/null || cat /etc/redhat-release)"

# CentOS 8/Stream 用 dnf,CentOS 7 只有 yum
if command -v dnf >/dev/null 2>&1; then PM=dnf; else PM=yum; fi
echo "包管理器: ${PM}"

VER=$(grep -oE '[0-9]+' /etc/centos-release | head -1)

# 仓库管理工具
sudo ${PM} install -y yum-utils

# 添加阿里云 Docker CE 仓库
sudo ${PM} config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

# CentOS 7 最高支持 docker-ce 25.x,需锁定版本
if [ "${VER}" = "7" ]; then
  sudo ${PM} install -y --nobest docker-ce-25.0.5 docker-ce-cli-25.0.5 containerd.io docker-buildx-plugin docker-compose-plugin
else
  sudo ${PM} install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
fi

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

On CentOS 7, switch to vault mirrors first or dependency resolution fails.

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

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

Step-by-Step Install

Add the Docker CE repo and confirm the repo file exists

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

Refresh the cache and list versions; CentOS 7 tops out at 25.x

sudo yum makecache fast
yum list docker-ce --showduplicates | head

CentOS 8 and Stream 9 install the latest version with the compose plugin

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

Pin the last supported version on CentOS 7 and use --nobest to avoid solver failures

sudo yum install -y --nobest docker-ce-25.0.5 docker-ce-cli-25.0.5 containerd.io

Verify the engine and compose plugin; CentOS 7 should report 25.x

docker --version
docker compose 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

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

CentOS defaults to Enforcing SELinux; mount with :z or :Z or access is denied

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

Docker publishes ports outside firewalld, so open them explicitly

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

Inspect the yum log to see which dependencies were skipped or downgraded

sudo tail -n 50 /var/log/yum.log

Uninstall & Cleanup

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

sudo yum 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 and clean caches so later installs do not hit a stale source

sudo rm -f /etc/yum.repos.d/docker-ce.repo
sudo yum clean all

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