一、卸载旧版本
#如果之前安装过旧版本的Docker,可以使用下面命令卸载
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine \
docker-ce
二、安装最新版本docker
- 安装Docker存储驱动的依赖包
yum install -y yum-utils device-mapper-persistent-data lvm2
- 添加稳定的Docker软件源,之前有设置则无需
yum-config-manager --add-repo=https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
- 更新索引与下载引擎
# 更新yum索引列表
sudo yum makecache fast
# 安装Docker引擎
sudo yum install docker-ce -y
- 更换镜像源
# 更换国内镜像源
vi /etc/docker/daemon.json
# daemon.json添加下面的json
{
"registry-mirrors": ["https://xxxxxx.mirror.aliyuncs.com"]
}
# 重载
sudo systemctl daemon-reload && sudo systemctl restart docker
- 启动docker
# 设置开启自启
systemctl enable docker.service
# 启动docker服务
systemctl start docker.service
- 验证是否安装成功
# 查看docker状态
systemctl status docker.service
docker --help #Docker帮助
docker --version #查看Docker版本
三、问题解决
3.1 启动docker报错
启动docker报错Failed to listen on Docker Socket for the API.
- 启动时查看日志发现报错
# journalctl -xe
Failed to listen on Docker Socket for the API.
- 查找socket这个配置文件,修改如下
# find / -name docker.socket
/usr/lib/systemd/system/docker.socket
# vim docker.socket
[Unit]
Description=Docker Socket for the API
[Socket]
ListenStream=/var/run/docker.sock
SocketMode=0660
SocketUser=root
SocketGroup=root # 这里原来是docker改成root即可,保存退出
[Install]
WantedBy=sockets.target
- 再次重启docker
systemctl restart docker.service
3.2 启动容器报错
启动容器时报错:no such file or directory
类似以下报错:
docker: Error response from daemon: open /var/lib/docker/overlay2/a74c485871bd268bac033de5b980ef643343d053c770ccd8d4a490636285a874/.tmp-committed106274925: no such file or directory.
docker: Error response from daemon: symlink ../fc981df74ae6e02e2c8280c4e4d4cf25e1ac1ed0d8df34734ab30a6d26fdf89-init/diff /var/lib/docker/overlay2/l/UROJ3TNA2WONLDDTHTRSATZICO: no such file or directory.
- 删除docker数据目录
rm -rf /var/lib/docker
- 重新安装docker