1.5 Centos7下Docker的安装与配置

作者:魏者老

时间:2021-06-07

1.5.1 准备工作

查阅官方参考文档,最权威的参考资料。

初接触Docker技术,建议关闭Linux系统的防火墙及Selinux。

[root@weipeng ~]#systemctl stop firewalld.service
[root@weipeng ~]#systemctl disable firewalld.service
[root@weipeng ~]#sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@weipeng ~]#grep SELINUX=disabled /etc/selinux/config
[root@weipeng ~]#setenforce 0

1.5.2 查看内核及Linux系统分发版本

查看kernel内核版本

[root@weipeng ~]# uname -r	//验证内核版本与你将要装的docker版本是否存在兼容性的问题-查官网
3.10.0-1062.el7.x86_64

附:3.10的内核版本显然有点低,推荐4.19版本,本人一直再使用,还没出现过问题。内核升级技术在后续步骤。

查看Linux系统分发版本

[root@weipeng ~]# cat /etc/os-release 
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

1.5.3 安装常用工具

[root@weipeng ~]# yum -y install vim yum-utils curl wget bash-completion

1.5.4 升级linux内核

Linux内核升级虽然不是r必须的,但建议大家升级一下,否则使用新docker版本很可能出现意外!

[root@weipeng ~]#rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
[root@weipeng ~]#rpm -Uvh https://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
#查询可升级的内核版本
[root@weipeng ~]#yum --disablerepo="*" --enablerepo="elrepo-kernel" list available  --showduplicates
#激活elrepo源同时安装稳定版本最新版内核(实际生产环境不建议,最好安装经过测试的指定版本,如4.4系列版本,本人一直使用,从没有出现过问题)
[root@weipeng ~]#yum --enablerepo=elrepo-kernel -y install kernel-lt
[root@weipeng ~]#grub2-set-default  0 && grub2-mkconfig -o /etc/grub2.cfg
[root@weipeng ~]#reboot

重启成功后再次查看内核版本,核验内核版本是否成功升级。

1.5.5 卸载docker旧版本

新安装的系统此步骤可以跳过。

#如果已安装过旧版本,需要首先卸载
[root@weipeng ~]yum remove docker-ce docker-ce-cli containerd.io
#删除docker相关资源目录
[root@weipeng ~]rm -rf /var/lib/docker

1.5.6 优化内核参数

[root@weipeng ~]vim /etc/sysctl.conf
#增加3行内容,解决docker启动警告与外部通讯问题
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
#开启ip转发功能
net.ipv4.conf.all.forwarding = 1
#设置生效
[root@weipeng ~]sysctl -p 

1.5.7 docker版本的选择

Docker目前有2中版本

  • docker-ce Docker Engine-Communit(docker引擎社区版),免费。
  • docker-ee Docker Engine-Enterprise(docker引擎企业版),商业收费。

常用docker-ce版本,无论是测试环境还是生产环境。

1.5.8 安装docker-ce

#设置添加阿里云docker源(官方速度太慢)
[root@weipeng ~]yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#更新Yum文件索引缓存
[root@weipeng ~]yum makecache fast
#安装docker-ce最新版
[root@weipeng ~]yum -y install docker-ce docker-ce-cli containerd.io //也可以只安装docker-ce,其他软件包属于依赖包,将会自动安装          

#注意
#如果是生产环境,建议安装经过验证的指定版本
[root@weipeng ~]yum list docker-ce --showduplicates |sort -r   
[root@weipeng ~]# yum -y install docker-ce-18.06.3.ce

1.5.9 启动与停止docker引擎服务

#启动docker引擎服务
[root@weipeng ~]#systemctl start docker
#配置docker服务的开机自启动
[root@weipeng ~]#systemctl enable docker
#查看docker相关信息
[root@weipeng ~]#docker version   
Client:
 Version:           18.06.3-ce
 API version:       1.38
 Go version:        go1.10.3
 Git commit:        d7080c1
 Built:             Wed Feb 20 02:26:51 2019
 OS/Arch:           linux/amd64
 Experimental:      false
Server:
 Engine:
  Version:          18.06.3-ce
  API version:      1.38 (minimum version 1.12)
  Go version:       go1.10.3
  Git commit:       d7080c1
  Built:            Wed Feb 20 02:28:17 2019
  OS/Arch:          linux/amd64
  Experimental:     false
 
 #查看docker引擎启动状态
[root@weipeng ~]#systemctl status docker
 
 #其他命令
[root@weipeng ~]#systemctl stop docker		//停止docker引擎服务
[root@weipeng ~]#systemctl restart docker	//重启docker引擎服务

1.5.10 配置中科大镜像加速器

docker的默认镜像仓库在国外,访问速度很慢。国内众多云厂商都提供了镜像加速器,使用加速器可以大大加快镜像获取的速度。使用中科大的镜像加速器不用注册账号,简单快捷,当然也可以选择阿里云等提供的加速器。

获取中科大镜像加速器地址的参考链接:Docker Hub 源使用帮助 — USTC Mirror Help 文档

在配置文件 /etc/docker/daemon.json 中加入:

{
  "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn/"]
}

重启docker引擎服务生效

[root@weipeng ~]#systemctl restart docker

注意:docker服务必须启动过,否则无/etc/docker目录

1.5.11 测试docker环境

查看docker信息:Docker info

[root@weipeng ~]# docker info 
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)
  scan: Docker Scan (Docker Inc., v0.8.0)

Server:
 Containers: 1
  Running: 0
  Paused: 0
  Stopped: 1
 Images: 2
 Server Version: 20.10.7
...内容较多,其他省略

运行hello-world镜像

由dockerhub镜像仓库下载hello-world镜像运行测试一下。

docker run指令后是镜像名称,如果本地无此镜像,将从dockerhub镜像仓库下载。

[root@weipeng ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete 
Digest: sha256:5122f6204b6a3596e048758cabba3c46b1c937a46b5be6225b835d091b90e46c
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
...

测试显示当前docker环境没有问题。

1.5.10 已普通用户身份管理Docker

#创建docker组,大多数情况下docker组已创建
[root@weipeng ~]#groupadd docker

#向docker组中添加账号,如已存在账号wp
[root@weipeng ~]#usermod  -aG docker wp

#一定要重新注销登录,否则不生效,然后验证docker命令是否可以正常执行
[wp@weipeng ~]$docker version

1.5.12 配置Docker Engine API的远程访问功能(除非需要,否则不要轻易开放)

Engine API可以由相关工具(如wget、curl等)通过HTTP协议远程管理Docker的相关操作。

[root@weipeng ~]# /usr/lib/systemd/system/docker.service
...
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -H tcp://0.0.0.0:2375	
ExecReload=/bin/kill -s HUP $MAINPID
...

#重启docker服务生效
[root@weipeng ~]#systemctl daemon-reload	//服务启动配置被修改后,必须使用此步骤
[root@weipeng ~]#systemctl restart docker.service		

注意:“-H tcp://0.0.0.0:2375”表示对所有IP开放docker engine的远程操作,注意安全防范。

开发人员非常喜欢在开发平台(如IntelliJ IDEA等)的docker插件中绑定docker宿主机的2375端口,这样可以很轻松实现代码的一键推送部署,大大节省了劳动量。

image-20210804160538420

posted on 2021-06-04 10:56  带米的笨老头  阅读(516)  评论(0编辑  收藏  举报