(四)Centos7 下安装 最新版docker

1.基本要求

Docker官方建议将Docker运行在Linux系统上,当然也可以在其他平台运行,本片博客只介绍在linux系统上的安装方法。
Docker运行在CentOS7.X 版本以上,本文使用 CentOS 7.7 版本。
Docker需要安装在64位平台。
Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker

1.1 虚拟机与Docker的不同

虚拟机与Docker有何不同 https://www.cnblogs.com/kex1n/p/6933039.htm

那Docker由什么组成呢, 包括三个基本概念:

  • 仓库(Repository)
  • 镜像(Image)
  • 容器(Container)

1.其中Registry是Docker用于存放镜像文件的仓库,Docker 仓库的概念跟Git 类似(就像商店存放所有的英雄,只是更改英雄的权限在某些非程序员手里)。
2.所谓镜像就是构建容器的源代码,是一个只读的模板,由一层一层的文件系统组成的,类似于虚拟机的镜像(英雄也是只读的,有自己的技能被动,你也不能进行操作)。
3.那么容器就是由Docker镜像创建的运行实例,类似于虚拟机,容器之间是相互隔离的,包含特定的应用及其所需的依赖文件(好比每个英雄都是隔离的,都有自己的皮肤,技能以及走的路线)。
注:Docker Hub是Docker公司提供的一个注册服务器(Register)来保存多个仓库,每个仓库又可以包含多个具备不同tag的镜像。

2 安装Docker步骤

2.1 root账户登录,查看内核版本如下

uname -a

我是虚拟机装的Centos7,linux 3.10 内核

[root@localhost ~]# uname -a
Linux localhost.localdomain 3.10.0-1062.el7.x86_64 #1 SMP Wed Aug 7 18:08:02 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

2.1 把yum包更新到最新

yum update
[root@localhost ~]# yum update
已加载插件:fastestmirror
Determining fastest mirrors
 * base: mirrors.cqu.edu.cn
 * extras: mirrors.ustc.edu.cn
 * updates: mirrors.neusoft.edu.cn
base                                                                              | 3.6 kB  00:00:00     
extras                                                                            | 2.9 kB  00:00:00     
updates                                                                           | 2.9 kB  00:00:00     
(1/4): extras/7/x86_64/primary_db                                                 | 159 kB  00:00:00     
(2/4): base/7/x86_64/group_gz                                                     | 165 kB  00:00:00     
(3/4): base/7/x86_64/primary_db                                                   | 6.0 MB  00:00:03     
(4/4): updates/7/x86_64/primary_db                                                | 6.7 MB  00:00:09     
正在解决依赖关系
--> 正在检查事务
---> 软件包 NetworkManager.x86_64.1.1.18.0-5.el7 将被 升级
---> 软件包 NetworkManager.x86_64.1.1.18.0-5.el7_7.2 将被 更新

(期间要选择确认,输入 y 即可)

2.2 卸载旧版本(如果安装过旧版本的话)

检查软件包是否安装了Docker命令

yum list installed | grep docker

若是有的话,卸载docker,卸载命令如下

yum -y remove docker docker-common docker-selinux docker-engine
  (旧版名称是docker , 最新社区版 docker-engine, 目前已改名为docker-ce ) 

2.3 安装需要的软件包

yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的

yum install -y yum-utils device-mapper-persistent-data lvm2
[root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.cqu.edu.cn
 * extras: mirrors.ustc.edu.cn
 * updates: mirrors.neusoft.edu.cn
软件包 device-mapper-persistent-data-0.8.5-1.el7.x86_64 已安装并且是最新版本
软件包 7:lvm2-2.02.185-2.el7_7.2.x86_64 已安装并且是最新版本
正在解决依赖关系
--> 正在检查事务
---> 软件包 yum-utils.noarch.0.1.1.31-52.el7 将被 安装
--> 正在处理依赖关系 python-kitchen,它被软件包 yum-utils-1.1.31-52.el7.noarch 需要
--> 正在处理依赖关系 libxml2-python,它被软件包 yum-utils-1.1.31-52.el7.noarch 需要

2.4 添加docker的yum源

选择其中一个命令执行

yum-config-manager --add-repo http://download.docker.com/linux/centos/docker-ce.repo(中央仓库)

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo(阿里仓库)

使用中央仓库,出现以下内容,表示安装成功

[root@localhost ~]# yum-config-manager --add-repo http://download.docker.com/linux/centos/docker-ce.repo
已加载插件:fastestmirror
adding repo from: http://download.docker.com/linux/centos/docker-ce.repo
grabbing file http://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo

此处可能会报错 (原因是国内访问不到docker官方镜像的缘故),那就使用阿里仓库(第二个命令)

 [Errno 14] curl#35 - "TCP connection reset by peer

2.5 查看所有仓库中所有docker版本,并选择特定版本安装

yum list docker-ce --showduplicates | sort -r
[root@localhost ~]# yum list docker-ce --showduplicates | sort -r
已加载插件:fastestmirror
已安装的软件包
可安装的软件包
 * updates: mirrors.neusoft.edu.cn
Loading mirror speeds from cached hostfile
 * extras: mirrors.ustc.edu.cn
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.1-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.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.cqu.edu.cn

2.6 配置镜像加速器

(必须要配置,否则会出现访问镜像并不稳定 ,而且下载缓慢等问题)

注册阿里云账号 ,登陆该网址自行注册 ( https://www.aliyun.com/?utm_content=se_1010377 )
在这里插入图片描述
进入阿里云控制台如图
在这里插入图片描述
执行命令以下3条命令

mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{"registry-mirrors": ["输入自己的加速器地址"]}
EOF

导入操作

systemctl daemon-reload

结果如下
在这里插入图片描述

2.7 安装Docker

  $ yum install docker-ce (这样写默认安装最新版本)
  $ yum install  docker-ce-<VERSION_STRING> (指定安装版本) 

执行如下命令

yum install docker-ce-18.03.1.ce

(此处若报错,一般还是无法官方镜像,请检查上一步文件中字母是否有错误)

安装成功,如下

[root@localhost ~]# yum install docker-ce-18.03.1.ce
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.cqu.edu.cn
 * extras: mirrors.ustc.edu.cn
 * updates: mirrors.neusoft.edu.cn
正在解决依赖关系
--> 正在检查事务
---> 软件包 docker-ce.x86_64.0.18.03.1.ce-1.el7.centos 将被 安装
--> 正在处理依赖关系 libseccomp >= 2.3,它被软件包 docker-ce-18.03.1.ce-1.el7.centos.x86_64 需要
--> 正在处理依赖关系 container-selinux >= 2.9,它被软件包 docker-ce-18.03.1.ce-1.el7.centos.x86_64 需要
--> 正在处理依赖关系 pigz,它被软件包 docker-ce-18.03.1.ce-1.el7.centos.x86_64 需要
--> 正在处理依赖关系 libcgroup,它被软件包 docker-ce-18.03.1.ce-1.el7.centos.x86_64 需要
--> 正在处理依赖关系 libltdl.so.7()(64bit),它被软件包 docker-ce-18.03.1.ce-1.el7.centos.x86_64 需要
--> 正在处理依赖关系 libseccomp.so.2()(64bit),它被软件包 docker-ce-18.03.1.ce-1.el7.centos.x86_64 需要
--> 正在检查事务

2.8 启动并加入开机启动

启动和开启启动命令

systemctl start docker    (重启命令 	systemctl restart docker) 
systemctl enable docker   (开机启动)
docker version            (查看docker版本号)

结果如下

[root@localhost ~]# systemctl start docker
[root@localhost ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@localhost ~]# docker version
Client:
 Version:      18.03.1-ce
 API version:  1.37
 Go version:   go1.9.5
 Git commit:   9ee9f40
 Built:        Thu Apr 26 07:20:16 2018
 OS/Arch:      linux/amd64
 Experimental: false
 Orchestrator: swarm

Server:
 Engine:
  Version:      18.03.1-ce
  API version:  1.37 (minimum version 1.12)
  Go version:   go1.9.5
  Git commit:   9ee9f40
  Built:        Thu Apr 26 07:23:58 2018
  OS/Arch:      linux/amd64
  Experimental: false

2.9 验证是否安装成功

执行命令

docker run hello-world 

结果如下

[root@localhost ~]# docker run hello-world 
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:9572f7cdcee8591948c2963463447a53466950b3fc15a247fcad1917ca215a2f
Status: Downloaded newer image for hello-world:latest

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

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

若进行到这一步没有问题的话就说明docker已经安装成功了 。

接下来我们就可以访问并下载你所需要的镜像,或者使用dockerfile自行构建Docker镜像,部署安装你的应用程序到docker容器中了。

3 docker的其它命令

3.1 查看docker的状态

service docker status

结果如下

[root@centos7-00 ~]# service docker status
Redirecting to /bin/systemctl status docker.service
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since 日 2020-02-16 16:54:35 CST; 10min ago
     Docs: https://docs.docker.com
 Main PID: 1093 (dockerd)
    Tasks: 18
   Memory: 128.3M
   CGroup: /system.slice/docker.service
           ├─1093 /usr/bin/dockerd
           └─2418 docker-containerd --config /var/run/docker/containerd/containerd.toml

2月 16 16:54:33 centos7-00 dockerd[1093]: time="2020-02-16T16:54:33+08:00" level=info msg="contai...nerd
2月 16 16:54:33 centos7-00 dockerd[1093]: time="2020-02-16T16:54:33.840191618+08:00" level=info m...ay2"
2月 16 16:54:34 centos7-00 dockerd[1093]: time="2020-02-16T16:54:34.089454686+08:00" level=info m...nds"
2月 16 16:54:34 centos7-00 dockerd[1093]: time="2020-02-16T16:54:34.091321598+08:00" level=info m...rt."
2月 16 16:54:35 centos7-00 dockerd[1093]: time="2020-02-16T16:54:35.298481220+08:00" level=info m...ess"
2月 16 16:54:35 centos7-00 dockerd[1093]: time="2020-02-16T16:54:35.627563756+08:00" level=info m...ne."
2月 16 16:54:35 centos7-00 dockerd[1093]: time="2020-02-16T16:54:35.928880127+08:00" level=info m...1-ce
2月 16 16:54:35 centos7-00 dockerd[1093]: time="2020-02-16T16:54:35.952860792+08:00" level=info m...ion"
2月 16 16:54:35 centos7-00 dockerd[1093]: time="2020-02-16T16:54:35.986619303+08:00" level=info m...ock"
2月 16 16:54:35 centos7-00 systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.

3.2 停止docker

service docker stop
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章