Docker requires a 64-bit version of CentOS 7 or 8 with a kernel version of 3.10 or higher. While CentOS 7 meets the minimum requirement, its older kernel may lack support for advanced storage drivers like overlay2, potentially leading to reduced performance or instability.
Remove Existing Docker Installations
Older Docker packages may be installed under names like docker or docker-engine. Uninstall them using:
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine
Install Docker via YUM
Begin by installing the required package management tool:
sudo yum install -y yum-utils
For faster downloads in China, use the Aliyun mirror:
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sudo sed -i 's|https://download.docker.com|https://mirrors.aliyun.com/docker-ce|g' /etc/yum.repos.d/docker-ce.repo
If you prefer the official repository, use:
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
To install a testing version, enable the test repository:
sudo yum-config-manager --enable docker-ce-test
Update the package index and install Docker:
sudo yum makecache fast
sudo yum install -y docker-ce docker-ce-cli containerd.io
Automated Installation Script
Docker provides a one-line installer for quick setup in development environments. To use the Aliyun mirror:
curl -fsSL https://get.docker.com -o install-docker.sh
sudo sh install-docker.sh --mirror Aliyun
For Azure China Cloud users:
sudo sh install-docker.sh --mirror AzureChinaCloud
The script automatically configures repositories, installs the latest stable version, and starts the service.
Additional Configuration for CentOS 8
CentOS 8 uses nftables as the default firewall back end, which is not fully compatible with Docker. To resolve this, switch to iptables:
Option 1: Edit the fierwall configuration file:
sudo sed -i 's/#FirewallBackend=nftables/FirewallBackend=iptables/' /etc/firewalld/firewalld.conf
Option 2: Allow Docker’s bridge interface directly:
sudo firewall-cmd --permanent --zone=trusted --add-interface=docker0
sudo firewall-cmd --reload
Start and Enable Docker
sudo systemctl enable --now docker
Create the Docker User Group
By default, Docker requires root privileges to interact with its daemon via Unix socket. To avoid running Docker commands as root, create a dedicated group and add your user to it:
sudo groupadd docker
sudo usermod -aG docker $USER
Log out and log back in for group membership changes to take effect.
Verify Installation
Run the following command to confirm Docker is working correctly:
docker run hello-world
If you see a welcome message and container execution output, Docker is successfully installed and configured.