1. yum方式安装
在线安装,需要服务器有公网访问权限
-
查看操作系统内核版本,尽量保持在3.10及以上
[root@centos7 ~]# uname -a Linux centos7 3.10.0-1127.el7.x86_64 #1 SMP Tue Mar 31 23:36:51 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
-
更新yum到最新
[root@centos7 ~]# yum update 已加载插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com ......
-
安装依赖包
[root@centos7 ~]# yum install -y yum-utils device-mapper-persistent-data lvm2 已加载插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com ......
-
设置yum源
中央仓库
[root@centos7 ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo 已加载插件:fastestmirror, langpacks 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@centos7 ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 已加载插件:fastestmirror, langpacks adding repo from: https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo grabbing file https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo repo saved to /etc/yum.repos.d/docker-ce.repo
-
查看仓库中所有docker版本
[root@centos7 ~]# yum list docker-ce --showduplicates | sort -r 已加载插件:fastestmirror, langpacks 可安装的软件包 * updates: mirrors.aliyun.com Loading mirror speeds from cached hostfile * extras: mirrors.aliyun.com docker-ce.x86_64 3:20.10.7-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.6-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.5-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.4-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.3-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.2-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.1-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.0-3.el7 docker-ce-stable ......
-
选择当前稳定版本进行安装
安装最新版
[root@centos7 ~]# yum install -y docker-ce
安装指定版本
# yum install -y docker-ce-<version> [root@centos7 ~]# yum install -y docker-ce-20.10.5
安装指定版本(包括依赖)
# yum install -y docker-ce-<version> docker-ce-cli-<version> [root@centos7 ~]# yum install -y docker-ce-20.10.5 docker-ce-cli-20.10.5
-
测试安装是否成功
[root@centos7 ~]# systemctl start docker [root@centos7 ~]# docker version Client: Docker Engine - Community Version: 20.10.5 API version: 1.41 Go version: go1.13.15 Git commit: 55c4c88 Built: Tue Mar 2 20:33:55 2021 OS/Arch: linux/amd64 Context: default Experimental: true Server: Docker Engine - Community Engine: Version: 20.10.5 API version: 1.41 (minimum version 1.12) Go version: go1.13.15 Git commit: 363e9a8 Built: Tue Mar 2 20:32:17 2021 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.4.8 GitCommit: 7eba5930496d9bbe375fdf71603e610ad737d2b2 runc: Version: 1.0.0 GitCommit: v1.0.0-0-g84113ee docker-init: Version: 0.19.0 GitCommit: de40ad0
2. rpm方式安装
离线安装,不需要服务器有公网访问权限
-
到docker官方下载地址https://download.docker.com/linux/centos/7/x86_64/stable/Packages/下载rpm文件,可能有多个rpm文件,如20.10.6版本需要下载docker-ce、docker-ce-cli、docker-ce-rootless-extras、docker-scan-plugin
-
然后将下载的rpm文件放到服务器的某个目录下,再执行安装命令,若提示缺失依赖,则根据提示去下载相应缺失的依赖后再重新执行安装命令
[root@centos7 docker]# pwd /opt/software/docker [root@centos7 docker]# ll 总用量 74840 -rw-r--r-- 1 root root 27885524 7月 21 16:28 docker-ce-20.10.6-3.el7.x86_64.rpm -rw-r--r-- 1 root root 34713112 7月 21 16:28 docker-ce-cli-20.10.6-3.el7.x86_64.rpm -rw-r--r-- 1 root root 9660840 7月 21 16:31 docker-ce-rootless-extras-20.10.6-3.el7.x86_64.rpm -rw-r--r-- 1 root root 4373740 7月 21 16:31 docker-scan-plugin-0.8.0-3.el7.x86_64.rpm [root@centos7 docker]# rpm -ivh ./*.rpm 准备中... ################################# [100%] 正在升级/安装... 1:docker-scan-plugin-0:0.8.0-3.el7 ################################# [ 25%] 2:docker-ce-cli-1:20.10.6-3.el7 ################################# [ 50%] 3:docker-ce-rootless-extras-0:20.10################################# [ 75%] 4:docker-ce-3:20.10.6-3.el7 ################################# [100%]
3. 启动与停止
-
启动
# 启动 [root@centos7 ~]# systemctl start docker # 设置开机自启 [root@centos7 ~]# systemctl enable docker Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
-
停止
# 停止 [root@centos7 ~]# systemctl stop docker # 关闭开机自启 [root@centos7 ~]# systemctl disable docker Removed symlink /etc/systemd/system/multi-user.target.wants/docker.service.
4. 测试
-
配置阿里云镜像仓库地址
[root@centos7 ~]# mkdir -p /etc/docker [root@centos7 ~]# vim /etc/docker/daemon.json [root@centos7 ~]# systemctl daemon-reload [root@centos7 ~]# systemctl restart docker
daemon.json文件内容如下,注册镜像地址需要自行登录阿里云控制台,在容器镜像服务的镜像加速器中获取
{ "registry-mirrors": ["https://******.mirror.aliyuncs.com"] }
-
运行hello-world
[root@centos7 ~]# docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world b8dfde127a29: Pull complete Digest: sha256:df5f5184104426b65967e016ff2ac0bfcd44ad7899ca3bbcf8e44e4461491a9e Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/
5. 卸载
-
卸载docker-ce
[root@centos7 ~]# yum remove -y docker-ce
-
卸载相关依赖包
[root@centos7 ~]# rpm -qa | grep docker | xargs rpm -e --nodeps