1、卸载旧版本
```bash
$ 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
```
2、安装需要的软件包
```bash
$ sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
```
3、配置YUM源
```bash
$ sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
```
4、安装 Docker
```bash
$ sudo yum install docker-ce
```
安装指定版本
```bash
$ yum list docker-ce --showduplicates | sort -r
docker-ce.x86_64 18.03.0.ce-1.el7.centos docker-ce-stable
......
$ sudo yum install docker-ce-<VERSION STRING>
```
5、启动 Docker
```bash
$ sudo systemctl start docker
```
注:启动`Docker`服务前,建议先配置镜像加速器,以及自定义`data-root`的路径。
```bash
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"data-root": "/opt/data/docker",
"registry-mirrors": ["https://registry.docker-cn.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
```
---
# 可选配置
配置`no-root`用户管理Docker,只需将普通用户加入docker组。
```bash
$ sudo groupadd docker
$ sudo usermod -aG docker $USER
```
配置自启动
```bash
sudo systemctl enable docker
```
参考资料:
- [Get Docker CE for CentOS](https://docs.docker.com/install/linux/docker-ce/centos/)
- [Optional Linux post-installation steps](https://docs.docker.com/install/linux/linux-postinstall/)