Linux Centos7 安装Docker环境
一、Docker介绍
Docker是一个开源的容器引擎,基于Go语言开发,同时基于Apache2.0协议开发。
对Docker简单的理解就是使用Docker可以把一台服务器隔离成一个个独立的容器,我们可以把这地方描述的容器理解成一个沙盒。在每个容器中运行一个程序,不同的容器之间相互隔离,容器的创建,停止,以及启动基本都是秒为单位,而且容器对资源的需求也是非常有限的,比起虚拟化的技术而言,可以节约很多的资源成本。在DevOps技术栈体系和可持续交付中占据了重要的位置。
二、环境准备
Docker支持以下的CentOS版本,目前,CentOS 仅发行版本中的内核支持 Docker。
-
Docker 运行在 CentOS 7 (64-bit)上,要求系统为64位、系统内核版本为 3.10 以上。
-
Docker 运行在 CentOS-6.5 或更高的版本的 CentOS 上,要求系统为64位、系统内核版本为 2.6.32-431 或者更高版本。
查看系统的版本,输入命令:
[root@centos7 ~]# cat /etc/centos-release CentOS Linux release 7.9.2009 (Core)
查看系统内核版本,输入命令:
[root@centos7 ~]# uname -a Linux centos7 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
查看后满足docker的环境要求CentOS7.4+系统内核版本为 3.10 以上,就可以接下来的安装操作了
三、Docker安装
1、 安装系统依赖工具
[root@centos7 ~]# sudo yum install -y yum-utils device-mapper-persistent-data lvm2
2、yum的配置管理,添加docker的软件源地址
[root@centos7 ~]# sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 已加载插件:fastestmirror, langpacks adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo grabbing file http://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
3、更新yum缓存
[root@centos7 ~]# sudo yum makecache fast 已加载插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirrors.163.com * extras: mirrors.163.com * updates: mirrors.ustc.edu.cn base | 3.6 kB 00:00:00 docker-ce-stable | 3.5 kB 00:00:00 extras | 2.9 kB 00:00:00 mysql-connectors-community | 2.6 kB 00:00:00 mysql-tools-community | 2.6 kB 00:00:00 mysql57-community | 2.6 kB 00:00:00 updates | 2.9 kB 00:00:00 (1/2): docker-ce-stable/7/x86_64/updateinfo | 55 B 00:00:00 (2/2): docker-ce-stable/7/x86_64/primary_db | 117 kB 00:00:00 元数据缓存已建立
4、查看所有仓库中所有docker版本
[root@centos7 ~]# yum list docker-ce --showduplicates | sort -r 已加载插件:fastestmirror, langpacks 可安装的软件包 * updates: mirrors.ustc.edu.cn Loading mirror speeds from cached hostfile * extras: mirrors.163.com docker-ce.x86_64 3:24.0.6-1.el7 docker-ce-stable docker-ce.x86_64 3:24.0.5-1.el7 docker-ce-stable docker-ce.x86_64 3:24.0.4-1.el7 docker-ce-stable docker-ce.x86_64 3:24.0.3-1.el7 docker-ce-stable docker-ce.x86_64 3:24.0.2-1.el7 docker-ce-stable docker-ce.x86_64 3:24.0.1-1.el7 docker-ce-stable docker-ce.x86_64 3:24.0.0-1.el7 docker-ce-stable docker-ce.x86_64 3:23.0.6-1.el7 docker-ce-stable docker-ce.x86_64 3:23.0.5-1.el7 docker-ce-stable docker-ce.x86_64 3:23.0.4-1.el7 docker-ce-stable docker-ce.x86_64 3:23.0.3-1.el7 docker-ce-stable docker-ce.x86_64 3:23.0.2-1.el7 docker-ce-stable docker-ce.x86_64 3:23.0.1-1.el7 docker-ce-stable docker-ce.x86_64 3:23.0.0-1.el7 docker-ce-stable docker-ce.x86_64 3:20.10.9-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.8-3.el7 docker-ce-stable 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.24-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.23-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.22-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.21-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.20-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.19-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.18-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.17-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.16-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.15-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.14-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.13-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.12-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.11-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.10-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.0-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.9-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.8-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.7-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.6-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.5-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.4-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.3-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.2-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.15-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.14-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.1-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.13-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.12-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.11-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.10-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.0-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.9-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.8-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.7-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.6-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.5-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.4-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.3-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.2-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable docker-ce.x86_64 18.06.3.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.06.2.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.03.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 18.03.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.12.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.12.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.09.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.09.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.06.2.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.06.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.06.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.03.3.ce-1.el7 docker-ce-stable docker-ce.x86_64 17.03.2.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable * base: mirrors.163.com
5、指定安装Docker某版本
[root@centos7 ~]# sudo yum install docker-ce-17.12.1.ce 已加载插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirrors.163.com * extras: mirrors.163.com * updates: mirrors.ustc.edu.cn 正在解决依赖关系 --> 正在检查事务 ---> 软件包 docker-ce.x86_64.0.17.12.1.ce-1.el7.centos 将被 安装 --> 正在处理依赖关系 container-selinux >= 2.9,它被软件包 docker-ce-17.12.1.ce-1.el7.centos.x86_64 需要 --> 正在检查事务 ---> 软件包 container-selinux.noarch.2.2.119.2-1.911c772.el7_8 将被 安装 --> 解决依赖关系完成 依赖关系解决 ================================================================================================================================================================================================================ Package 架构 版本 源 大小 ================================================================================================================================================================================================================ 正在安装: docker-ce x86_64 17.12.1.ce-1.el7.centos docker-ce-stable 30 M 为依赖而安装: container-selinux noarch 2:2.119.2-1.911c772.el7_8 extras 40 k 事务概要 ================================================================================================================================================================================================================ 安装 1 软件包 (+1 依赖软件包) 总下载量:30 M 安装大小:30 M Is this ok [y/d/N]: y Is this ok [y/d/N]: y Downloading packages: (1/2): container-selinux-2.119.2-1.911c772.el7_8.noarch.rpm | 40 kB 00:00:01 warning: /var/cache/yum/x86_64/7/docker-ce-stable/packages/docker-ce-17.12.1.ce-1.el7.centos.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY==============-] 2.7 MB/s | 30 MB 00:00:00 ETA docker-ce-17.12.1.ce-1.el7.centos.x86_64.rpm 的公钥尚未安装 (2/2): docker-ce-17.12.1.ce-1.el7.centos.x86_64.rpm | 30 MB 00:00:12 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 总计 2.5 MB/s | 30 MB 00:00:12 从 https://mirrors.aliyun.com/docker-ce/linux/centos/gpg 检索密钥 导入 GPG key 0x621E9F35: 用户ID : "Docker Release (CE rpm) <docker@docker.com>" 指纹 : 060a 61c5 1b55 8a7f 742b 77aa c52f eb6b 621e 9f35 来自 : https://mirrors.aliyun.com/docker-ce/linux/centos/gpg 是否继续?[y/N]:y Running transaction check Running transaction test Transaction test succeeded Running transaction 正在安装 : 2:container-selinux-2.119.2-1.911c772.el7_8.noarch 1/2 正在安装 : docker-ce-17.12.1.ce-1.el7.centos.x86_64 2/2 验证中 : docker-ce-17.12.1.ce-1.el7.centos.x86_64 1/2 验证中 : 2:container-selinux-2.119.2-1.911c772.el7_8.noarch 2/2 已安装: docker-ce.x86_64 0:17.12.1.ce-1.el7.centos 作为依赖被安装: container-selinux.noarch 2:2.119.2-1.911c772.el7_8 完毕!
6、启动Docker服务
[root@centos7 ~]# sudo systemctl start docker
7、开机启动Docker
[root@centos7 ~]# sudo systemctl enable docker Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
8、查看Docker状态
[root@centos7 ~]# sudo systemctl status docker ● docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled) Active: active (running) since 日 2023-10-08 21:30:52 CST; 3min 2s ago Docs: https://docs.docker.com Main PID: 2538 (dockerd) Tasks: 21 Memory: 88.0M CGroup: /system.slice/docker.service ├─2538 /usr/bin/dockerd └─2547 docker-containerd --config /var/run/docker/containerd/containerd.toml 10月 08 21:30:52 centos7 dockerd[2538]: time="2023-10-08T21:30:52+08:00" level=info msg=serving... address="/var/run/docker/containerd/docker-containerd.sock" module="containerd/grpc" 10月 08 21:30:52 centos7 dockerd[2538]: time="2023-10-08T21:30:52+08:00" level=info msg="containerd successfully booted in 0.028029s" module=containerd 10月 08 21:30:52 centos7 dockerd[2538]: time="2023-10-08T21:30:52.407148219+08:00" level=info msg="Graph migration to content-addressability took 0.00 seconds" 10月 08 21:30:52 centos7 dockerd[2538]: time="2023-10-08T21:30:52.421387627+08:00" level=info msg="Loading containers: start." 10月 08 21:30:52 centos7 dockerd[2538]: time="2023-10-08T21:30:52.661114599+08:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon option --bip ...ed IP address" 10月 08 21:30:52 centos7 dockerd[2538]: time="2023-10-08T21:30:52.768798625+08:00" level=info msg="Loading containers: done." 10月 08 21:30:52 centos7 dockerd[2538]: time="2023-10-08T21:30:52.840502391+08:00" level=info msg="Docker daemon" commit=7390fc6 graphdriver(s)=overlay2 version=17.12.1-ce 10月 08 21:30:52 centos7 dockerd[2538]: time="2023-10-08T21:30:52.841307781+08:00" level=info msg="Daemon has completed initialization" 10月 08 21:30:52 centos7 systemd[1]: Started Docker Application Container Engine. 10月 08 21:30:52 centos7 dockerd[2538]: time="2023-10-08T21:30:52.870031396+08:00" level=info msg="API listen on /var/run/docker.sock" Hint: Some lines were ellipsized, use -l to show in full.
9、查看Docker信息,显示Docker的版本等
[root@centos7 ~]# docker info Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 0 Server Version: 17.12.1-ce Storage Driver: overlay2 Backing Filesystem: xfs Supports d_type: true Native Overlay Diff: true Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge host macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog Swarm: inactive Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: 9b55aab90508bd389d7654c4baf173a981477d55 runc version: 9f9c96235cc97674e935002fc3d78361b696a69e init version: 949e6fa Security Options: seccomp Profile: default Kernel Version: 3.10.0-1160.el7.x86_64 Operating System: CentOS Linux 7 (Core) OSType: linux Architecture: x86_64 CPUs: 4 Total Memory: 972.4MiB Name: centos7 ID: 2Z5M:U4MM:M6Q4:WY7C:L2Z6:EF4K:RZN4:OGY4:KVBV:E2WY:6TFA:DBBL Docker Root Dir: /var/lib/docker Debug Mode (client): false Debug Mode (server): false Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: 127.0.0.0/8 Live Restore Enabled: false