Installing Docker on CentOS
With container technology as popular as it is today, knowing how to install Docker is an essential skill.
The installation steps in Docker's official docs aren't complicated in themselves, but following them verbatim from mainland China usually gets you stuck on download speeds. On top of that, production environments typically need a pinned Docker version and a relocated image storage directory — details the official docs scatter across several pages. This post pulls together my full Docker installation process on CentOS, including the post-install configuration I always apply, so future reinstalls are just a matter of following along.
Installation Steps
- Install the required yum packages
# yum-utils provides the yum-config-manager tool used to add repositories later
# device-mapper-persistent-data and lvm2 are dependencies of the devicemapper storage driver
sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
These are all foundational tools: yum-config-manager is the command we need in the next step to add the repository, and the other two packages are storage-driver dependencies — installing them now avoids errors later.
- Configure a China-based mirror to speed up Docker downloads
# Add Alibaba Cloud's docker-ce repository in place of the official one
sudo yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Docker's official repository is very slow — or times out entirely — from mainland China. Switching to Alibaba Cloud's mirror brings package download speeds back to normal. Under the hood, this command writes a repo file into /etc/yum.repos.d/, after which yum can resolve Docker-related packages from that repository.
- List the available package versions
# List every available docker-ce version in the repository, sorted newest first
yum list docker-ce --showduplicates | sort -r
The --showduplicates flag lists every historical version in the repository instead of only the latest. The second column of the output is the version string, which we'll use in the next step to install a specific version.
- Install a specific Docker version
# Replace <VERSION_STRING> with the version string found in the previous step
sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
Example: installing a particular Docker version.
# Install version 20.10.9, keeping docker-ce and docker-ce-cli in sync
sudo yum install docker-ce-20.10.9-3.el7 docker-ce-cli-20.10.9-3.el7 containerd.io
This installs three packages, each with its own role: docker-ce is the daemon (dockerd), docker-ce-cli is the command-line client, and containerd.io is the underlying container runtime that actually handles container creation and lifecycle management. Running yum install docker-ce without a version installs whatever is newest in the repository; for production, pin the version so different machines don't drift apart.
- Start the Docker service
sudo systemctl start docker
Once started, run docker info or docker version to confirm the daemon is up. That completes the installation.
Going Further
1) Changing Docker's default image storage path and configuring a registry mirror
By default, Docker stores images and container data under /var/lib/docker. Many servers have limited space on the root partition, and a handful of large images can fill it up quickly, so it's common practice to move the storage directory to a larger data disk. Meanwhile, pulling from Docker Hub is slow in mainland China, and configuring a local registry mirror makes a noticeable difference. Both settings live in daemon.json.
- Edit /etc/docker/daemon.json with vim
# The file doesn't exist by default; saving it in vim creates it
vim /etc/docker/daemon.json
- Set
data-rootto change the image storage path, and setregistry-mirrorsto change the image download source — it's an array, so you can list multiple addresses separated by commas.
{
"data-root": "/home/docker-data",
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn/"]
}
The mirror here is the USTC mirror. registry-mirrors is an array, so you can configure several addresses and Docker will try them in order when pulling images.
After editing daemon.json, restart Docker:
service docker restart
Then run docker info to verify the changes took effect. If they did, Docker Root Dir in the output will show the new storage path, and Registry Mirrors will list the configured mirror addresses.
2) Enabling Docker to start on boot
# Register as a system service so Docker starts automatically after a reboot
systemctl enable docker
Servers get rebooted sooner or later. Forget this step and none of your containers come back after a reboot — you'd have to start the Docker service by hand. So enable it right after installation while you're at it.
Pitfalls and Notes
- daemon.json is strict JSON — one extra comma or missing quote and Docker fails to start. If Docker won't restart after a config change, check this file's syntax first.
- Changing
data-rootonly switches to a new directory; the existing images and container data under/var/lib/dockerare not migrated automatically. If you need them, copy them over yourself before restarting. - When pinning versions, keep the
docker-ceanddocker-ce-cliversion strings identical — mixing them can produce client/daemon incompatibility warnings. - If the machine has an older Docker installed (e.g. the legacy
dockerordocker-enginepackages), remove it cleanly before installing docker-ce to avoid conflicts.
Wrapping Up
The whole process boils down to five steps: install the tooling, switch to a local mirror, list the versions, install a pinned version, and start the service. Don't rush to use it right away — first set up the image storage path, the registry mirror, and start-on-boot. Each is a one-time configuration that pays off for the life of the machine. Next time a fresh server needs provisioning, just work through this note from top to bottom.
COMMENTS