CentOS 7 环境下安装 Docker(使用 yum 安装)
Docker 运行在 CentOS 7 上,要求系统为 64 位、系统内核版本为 3.10 以 上。 Docker 运行在 CentOS-6.5 或更高的版本的 CentOS 上,要求系统为 64 位、系统内核版本为 2.6.32-431 或者更高版本。
- 通过"uname -r "命令查看当前内核版本。
- 保证系统能连接互联网。
步骤 1:更改 CentOS 源为阿里云开源镜像站
1) 备份
# mkdir /etc/yum.repos.d/bak
# mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak/
2) 编辑 repo 文件
# cp /etc/yum.repos.d/bak/CentOS-Base.repo /etc/yum.repos.d/Cent OS-vault-7.8.2003.repo
编辑/etc/yum.repos.d/CentOS-vault-7.8.2003.repo 文件:
(1)将所有“mirrorlist=”开头行前面加“#”注释掉;并将“baseurl=” 开头行“#”注释符删除,即取消注释(如果被注释的话)。
(2)将所有的“mirror.centos.org/centos/$releasever”替换为“mirr ors.aliyun.com/centos-vault/7.8.2003”。
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client. You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#
[base]
name=CentOS-$releasever - Base
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
baseurl=http://mirrors.aliyun.com/centos-vault/7.8.2003/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#released updates
[updates]
name=CentOS-$releasever - Updates
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
baseurl=http://mirrors.aliyun.com/centos-vault/7.8.2003/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
baseurl=http://mirrors.aliyun.com/centos-vault/7.8.2003/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
baseurl=http://mirrors.aliyun.com/centos-vault/7.8.2003/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
3) 建立 yum 缓冲,加快软件安装速度
# yum makecache fast
步骤 2: 安装必要的一些系统工具
# yum install -y yum-utils device-mapper-persistent-data lvm2
步骤 3:添加 Docker 的 yum 软件源
yum-config-manager --add-repo http://mirrors.aliyun.com/dockerce/linux/centos/docker-ce.repo
//说明:这里使用阿里云开源镜像站,速度比 Docker 官方网址快。
步骤 4: 更新并安装 Docker-CE
# yum makecache fast
# yum -y install docker-ce
步骤 5: 开启 Docker 服务
# systemctl start docker
# systemctl enable docker
安全校验
[root@localhost ~]# docker version
普通用户加入用户组docker
1. 创建普通用户 john
# useradd john+学号
# passwd john+学号
# usermod -aG docker john+学号
# cat /etc/group |grep john+学号
2. 赋予普通用户执行命令权限
# visudo
在文件末尾增加一行:
john+学号 ALL=(ALL) ALL
保存退出。
3. 刷新用户组
# newgrp docker 切换到 john+学号用户环境:
# su john+学号
如果不刷新用户组,启动 Docker 时,会报如下错误:
$ docker run hello-world
docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.40/containers/create: dial unix /var/run/docker.sock: connect: permission denied.
See 'docker run --help'