Centos7 安装docker
官方手册:https://docs.docker.com/install/linux/docker-ce/centos/
查看当前是否安装docker
[root@k8s-w2 ~]# docker -v
Docker version 19.03.5, build 633a0ea
卸载现有docker
官方提供方案,无法卸载
[root@k8s-w2 ~]# sudo yum remove docker \
> docker-client \
> docker-client-latest \
> docker-common \
> docker-latest \
> docker-latest-logrotate \
> docker-logrotate \
> docker-engine
Loaded plugins: fastestmirror
No Match for argument: docker
No Match for argument: docker-client
No Match for argument: docker-client-latest
No Match for argument: docker-common
No Match for argument: docker-latest
No Match for argument: docker-latest-logrotate
No Match for argument: docker-logrotate
No Match for argument: docker-engine
No Packages marked for removal
亲测有效方案
列出已经安装的docker组件
[root@k8s-w2 ~]# yum list installed |grep docker
containerd.io.x86_64 1.2.10-3.2.el7 @docker-ce-stable
docker-ce.x86_64 3:19.03.5-3.el7 @docker-ce-stable
docker-ce-cli.x86_64 1:19.03.5-3.el7 @docker-ce-stable
[root@k8s-w2 ~]#
删除已安装的docker组件(全部删除)
[root@k8s-w2 ~]# yum -y remove containerd.io.x86_64 docker-ce.x86_64 docker-ce-cli.x86_64
Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package containerd.io.x86_64 0:1.2.10-3.2.el7 will be erased
---> Package docker-ce.x86_64 3:19.03.5-3.el7 will be erased
---> Package docker-ce-cli.x86_64 1:19.03.5-3.el7 will be erased
--> Finished Dependency Resolution
Dependencies Resolved
==================================================================================================================================================================
Package Arch Version Repository Size
==================================================================================================================================================================
Removing:
containerd.io x86_64 1.2.10-3.2.el7 @docker-ce-stable 90 M
docker-ce x86_64 3:19.03.5-3.el7 @docker-ce-stable 104 M
docker-ce-cli x86_64 1:19.03.5-3.el7 @docker-ce-stable 169 M
Transaction Summary
==================================================================================================================================================================
Remove 3 Packages
······
Removed:
containerd.io.x86_64 0:1.2.10-3.2.el7 docker-ce.x86_64 3:19.03.5-3.el7 docker-ce-cli.x86_64 1:19.03.5-3.el7
Complete!
设置yum仓库
安装或升级相关依赖
[root@k8s-w2 ~]# yum install -y yum-utils \
> device-mapper-persistent-data \
> lvm2
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.163.com
* updates: mirrors.aliyun.com
base | 3.6 kB 00:00:00
docker-ce-stable | 3.5 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
Package yum-utils-1.1.31-52.el7.noarch already installed and latest version
Package device-mapper-persistent-data-0.8.5-1.el7.x86_64 already installed and latest version
Package 7:lvm2-2.02.185-2.el7_7.2.x86_64 already installed and latest version
Nothing to do
[root@k8s-w2 ~]#
设置yum仓库
出现如下异常:Could not resolve host: download.docker.com; Unknown error
[root@k8s-w2 ~]# yum-config-manager \
> --add-repo \
> https://download.docker.com/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
Could not fetch/save url https://download.docker.com/linux/centos/docker-ce.repo to file /etc/yum.repos.d/docker-ce.repo: [Errno 14] curl#6 - "Could not resolve host: download.docker.com; Unknown error"
[root@k8s-w2 ~]# ping www.baidu.com
ping: www.baidu.com: Name or service not known
设置dns解析,新增电信dns地址61.139.2.69,或者新增8.8.8.8等
[root@k8s-w2 ~]# vi /etc/resolv.conf
# Generated by NetworkManager
nameserver 61.139.2.69
nameserver 10.0.2.3
# 保存后
[root@k8s-w2 ~]# ping www.baidu.com
PING www.a.shifen.com (14.215.177.38) 56(84) bytes of data.
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=1 ttl=54 time=35.4 ms
[root@k8s-w2 ~]# yum-config-manager \
> --add-repo \
> https://download.docker.com/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
[root@k8s-w2 ~]
设置阿里云镜像加速器
注意文件格式并替换为自己的镜像加速器
[root@uat-master ~]# sudo mkdir -p /etc/docker
[root@uat-master ~]# sudo tee /etc/docker/daemon.json <<-'EOF'
> {
> "registry-mirrors": ["https://5s9vglu3.mirror.aliyuncs.com"]
> }
> EOF
{
"registry-mirrors": ["https://xxxxx.mirror.aliyuncs.com"]
}
[root@uat-master ~]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://xxxxx.mirror.aliyuncs.com"]
}
[root@uat-w2 ~]# sudo systemctl daemon-reload
[root@uat-w2 ~]#
安装最新docker社区版
[root@k8s-w2 ~]# sudo yum install docker-ce docker-ce-cli containerd.io
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.163.com
* updates: mirrors.aliyun.com
········
安装docker指定版本
查找可用的版本
[root@k8s-w2 ~]# yum list docker-ce --showduplicates | sort -r
* updates: mirrors.aliyun.com
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror
Installed Packages
* extras: mirrors.163.com
docker-ce.x86_64 3:19.03.5-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.5-3.el7 @docker-ce-stable
docker-ce.x86_64 3:19.03.4-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.3-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.2-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.1-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.0-3.el7 docker-ce-stable
······
安装指定版本
yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
这里以18.09.9版本为例
[root@k8s-w2 docker]# yum -y install docker-ce-18.09.9 docker-ce-cli-18.09.9 containerd.io
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.163.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package containerd.io.x86_64 0:1.2.10-3.2.el7 will be installed
---> Package docker-ce.x86_64 3:18.09.9-3.el7 will be installed
---> Package docker-ce-cli.x86_64 1:18.09.9-3.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==================================================================================================================================================================
Package Arch Version Repository Size
==================================================================================================================================================================
Installing:
containerd.io x86_64 1.2.10-3.2.el7 docker-ce-stable 23 M
docker-ce x86_64 3:18.09.9-3.el7 docker-ce-stable 21 M
docker-ce-cli x86_64 1:18.09.9-3.el7 docker-ce-stable 16 M
Transaction Summary
==================================================================================================================================================================
Install 3 Packages
······
Installed:
containerd.io.x86_64 0:1.2.10-3.2.el7 docker-ce.x86_64 3:18.09.9-3.el7 docker-ce-cli.x86_64 1:18.09.9-3.el7
Complete!
查看docker版本
[root@k8s-w2 docker]# docker -v
Docker version 18.09.9, build 039a7df9ba
[root@k8s-w2 docker]#
启动docker,并设置开机启动
[root@k8s-w2 docker]# systemctl start docker
[root@k8s-w2 docker]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@k8s-w2 docker]#
验证,安装成功
[root@k8s-w2 docker]# docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:4df8ca8a7e309c256d60d7971ea14c27672fc0d10c5f303856d7bc48f8cc17ff
Status: Downloaded newer image for hello-world:latest
[root@k8s-w2 docker]#