Centos7 安装Docker

来自CloudWiki
跳转至: 导航搜索

前提条件

目前,CentOS 仅发行版本中的内核支持 Docker。

Docker 运行在 CentOS 7 上,要求系统为64位、系统内核版本为 3.10 以上。

Docker 运行在 CentOS-6.5 或更高的版本的 CentOS 上,要求系统为64位、系统内核版本为 2.6.32-431 或者更高版本。

使用 yum 安装(CentOS 7下)

Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker 。

通过 uname -r 命令查看你当前的内核版本

[root@controller ~]# uname -r

3.10.0-327.el7.x86_64


安装 Docker

从 2017 年 3 月开始 docker 在原来的基础上分为两个分支版本: Docker CE 和 Docker EE。

Docker CE 即社区免费版,Docker EE 即企业版,强调安全,但需付费使用。

本文介绍 Docker CE 的安装使用。

检查内核版本

[root@controller ~]# uname -r

3.10.0-327.el7.x86_64


移除旧的版本

$ 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

安装一些必要的系统工具

sudo yum install -y yum-utils device-mapper-persistent-data lvm2

添加软件源信息

sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

更新 yum 缓存

sudo yum makecache fast

Loaded plugins: fastestmirror, langpacks
base                                                     | 3.6 kB     00:00
docker-ce-stable                                         | 3.5 kB     00:00
extras                                                   | 3.4 kB     00:00
updates                                                  | 3.4 kB     00:00
(1/2): docker-ce-stable/x86_64/updateinfo                  |   55 B   00:00
(2/2): docker-ce-stable/x86_64/primary_db                  |  26 kB   00:00
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Metadata Cache Created

安装 Docker-ce

sudo yum -y install docker-ce

如遇到错误:Error: Package: 3:docker-ce-18.09.6-3.el7.x86_64 (docker-ce-stable)Requires: container-selinux >= 2.9

需要执行以下命令:

yum install policycoreutils-python wget

wget http://mirror.centos.org/centos/7/extras/x86_64/Packages/container-selinux-2.107-3.el7.noarch.rpm

rpm -ivh container-selinux-2.107-3.el7.noarch.rpm

启动 Docker 后台服务

sudo systemctl start docker

sudo systemctl enable docker

测试运行 hello-world

[root@runoob ~]# docker run hello-world

由于本地没有hello-world这个镜像,所以会下载一个hello-world的镜像,并在容器内运行。

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

镜像加速

鉴于国内网络问题,后续拉取 Docker 镜像十分缓慢,我们可以需要配置加速器来解决,我使用的是网易的镜像地址:http://hub-mirror.c.163.com。

新版的 Docker 使用 /etc/docker/daemon.json(Linux) 或者 %programdata%\docker\config\daemon.json(Windows) 来配置 Daemon。

请在该配置文件中加入(没有该文件的话,请先建一个):

{
  "registry-mirrors": ["http://hub-mirror.c.163.com"]
}

[root@controller yum.repos.d]# sudo systemctl restart docker

删除 Docker CE

这里仅告知方法,请仅在需要删除的时候进行删除。执行以下命令来删除 Docker CE:

$ sudo yum remove docker-ce
$ sudo rm -rf /var/lib/docker

参考文档

[1] http://www.runoob.com/docker/docker-tutorial.html

[2] https://cloud.tencent.com/developer/labs/lab/10054