Docker 安装(使用阿里云镜像)
Docker 从 1.13 版本之后采用时间线的方式作为版本号, 分为社区版 CE 和企业版 EE.
社区版是免费提供给个人开发者和小型团体使用的, 企业版会提供额外的收费服务, 比如经过官方测试认证过的基础设施, 容器, 插件等.
社区版按照 stable 和 edge 两种方式发布, 每个季度更新 stable 版本, 如 17.06,17.09; 每个月份更新 edge 版本, 如 17.09,17.10.
1) Docker 要求 CentOS 系统的内核版本高于 3.10 , 查看本页面的前提条件来验证你的 CentOS 版本是否支持 Docker .
通过 uname -r 命令查看你当前的内核版本
uname -r
需要注意的是以下命令均是在 root 账户下执行的, 如果是普通账户有些命令则可能需要加上 sudo
2)执行 yum update
yum update 非必选项, 但是建议执行操作
3)卸载旧版本 docker
yum -y remove docker docker-common docker-selinux docker-engine
3)设置 yum 源
- yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
- yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #阿里云 yum 源
需要注意的是, 目前 (2019 年 3 月) 测试的时候, docker 不设置阿里云镜像源也是能正常安装的, 并且阿里云镜像源地址也可能会更改, 所以以上地址可能会返回 404, 如果以上镜像源地址不存在也可以忽略这一步直接进行下一步.
4)查看所有仓库中所有 docker 版本, 并选择特定版本安装
yum list docker-ce --showduplicates | sort -r
5)安装 docker
- yum install -y docker-ce #由于 repo 中默认只开启 stable 仓库, 故这里安装的是最新稳定版 17.12.0
- yum install -y <FQPN> # 例如: sudo yum install docker-ce-17.12.0.ce
6)启动 docker 并加入开机自动启动
- systemctl start docker
- systemctl enable docker
7)配置阿里云镜像加速
- mkdir -p /etc/docker
- vi /etc/docker/daemon.JSON
添加以下内容
- {
- "registry-mirrors":
- ["https://5f2jam6c.mirror.aliyuncs.com",
- "http://hub-mirror.c.163.com"]
- }
8)重新加载配置文件
systemctl reload docker
9)重启 docker
systemctl restart docker
安装过程中可能会出现的问题:
1. 正在处理依赖关系 docker-ce-selinux>= 17.03.0.ce-1.el7.CentOS, 它被软件包 docker-ce-17.03.0.ce-1.el7.CentOS.x86_64 需要
软件包 docker-ce-selinux 已经被 docker-ce-cli 取代, 但是取代的软件包并未满足需求 "
等一大串的问题
这时我们需要通过 yum install 安装一个 rpm 包
通过这个地址我们查看和我们安装 docker 版本一直的 rpm 包
通过
yum install https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-selinux-17.03.0.ce-1.el7.centos.noarch.rpm
问题解决
2. 非 root 用户使用 docker 命令报错
以上我们为了方便在安装过程中使用的是 root 账户, 然而实际操作中我们可能更多的是用普通用户操作 docker 命令. 从 root 用户切到普通用户执行 docker 命令时会报如下错误
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http:///var/run/docker.sock/v1.39/containers/JSON: dial unix /var/run/docker.sock: connect: permission denied
官方解释如下:
- The docker daemon binds to a Unix socket instead of a TCP port. By default that Unix socket is owned by the user root and other users can access it with sudo. For this reason, docker daemon always runs as the root user.
- To avoid having to use sudo when you use the docker command, create a Unix group called docker and add users to it. When the docker daemon starts, it makes the ownership of the Unix socket read/writable by the docker group.
创建 docker 用户组
sudo groupadd docker
此步骤并非必须, 默认情况下 docker 安装过程中会自动创建一个 docker 用户组
将当前用户加入 docker 用户组
sudo gpasswd -a ${USER} docker
注意以上命令只能用当前用户来操作, 因为变量 USER 获取的是当前用户, 如果要以 root 用户来操作, 则需要指定的是具体的用户名, 而非此变量
当前用户退出系统重新登陆
这里并不是批退出系统, 只要切换一下用户即可, 比如先切到 root 用户然后再切回来就可以了
3. 安装 docker 时报 container-selinux>= 2.9 错误
由于测试环境的机器是不同批准到来的, 第一批安装非常顺序, 第二批是直接放置在办公室的 PC 机, 也非常不顺序, 安装过程遇到各种各样麻烦.
上面说过, 加入集群时发现 docker 没有安装成功, 安装 docker 时出现了以下错误
- Error: Package: docker-ce-18.03.1.ce-1.el7.CentOS.x86_64 (docker-ce-edge)
- Requires: container-selinux>= 2.9
- You could try using --skip-broken to work around the problem
- You could try running: rpm -Va --nofiles --nodigest
这个报错是 container-selinux 版本低或者是没安装的原因
yum 安装 container-selinux 一般的 yum 源又找不到这个包
需要安装 epel 源 才能 yum 安装 container-selinux
然后在安装 docker-ce 就可以了.
- wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
- yum install epel-release #阿里云上的 epel 源
- yum makecache
然后 yum install container-selinux
来源: https://www.cnblogs.com/tylerzhou/p/10969072.html