【Docker笔记】CentOS7安装Docker

docker是什么?
? Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。
Docker 包括三个基本概念:

  • 镜像(Image):Docker 镜像(Image),就相当于是一个 root 文件系统。比如官方镜像 ubuntu:16.04 就包含了完整的一套 Ubuntu16.04 最小系统的 root 文件系统。
  • 容器(Container):镜像(Image)和容器(Container)的关系,就像是面向对象程序设计中的类和实例一样,镜像是静态的定义,容器是镜像运行时的实体。容器可以被创建、启动、停止、删除、暂停等。
  • 仓库(Repository):仓库可看着一个代码控制中心,用来保存镜像。
docker的安装
  1. 查看内核(建议切换到root用户进行安装docker)
Docker 运行在CentOS 7 上,要求系统为64位、系统内核版本为 3.10 以上。
Docker 运行在 CentOS-6.5 或更高的版本的 CentOS 上,要求系统为64位、系统内核版本为 2.6.32-431 或者更高版本。
[root@localhost ~]# uname -r 3.10.0-957.el7.x86_64 [root@localhost ~]#

  1. 把yum包更新到最新
[root@localhost ~]# yum update

  1. 卸载旧版本(如果安装过旧版本的话,第一次安装不用管这一步)
    查看是否安装了docker [root@localhost ~]# yum list installed | grep docker较旧的 Docker 版本称为 docker 或 docker-engine 。如果已安装这些程序,请卸载它们以及相关的依赖项。从 2017 年 3 月开始 docker 在原来的基础上分为两个分支版本: Docker CE 和 Docker EE。 Docker CE 即社区免费版,Docker EE 即企业版,强调安全,但需付费使用。 [root@localhost ~]#yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine如果是较新版本采用下面的方式进行卸载 [root@localhost ~]#yum remove docker-ce删除镜像文件、容器、挂载目录、自定义配置文件等 [root@localhost ~]#rm -rf /var/lib/docker

  2. 安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个(device-mapper-persistent-data 和 lvm2)是devicemapper驱动依赖的
[root@localhost ~]#yum install -y yum-utils device-mapper-persistent-data lvm2

  1. 设置yum源(选一个源设置即可)
4.1 阿里云源 [root@localhost ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo4.1 官方源 [root@localhost ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo说明:上面阿里云源和官方源两者命令选其一执行即可。不要两个都执行!

  1. 查看所有仓库中所有docker版本
[root@localhost ~]# yum list docker-ce --showduplicates | sort -r docker-ce.x86_6418.06.3.ce-3.el7docker-ce-stable docker-ce.x86_6418.06.2.ce-3.el7docker-ce-stable docker-ce.x86_6418.06.1.ce-3.el7docker-ce-stable docker-ce.x86_6418.06.0.ce-3.el7docker-ce-stable docker-ce.x86_6418.03.1.ce-1.el7.centosdocker-ce-stable docker-ce.x86_6418.03.0.ce-1.el7.centosdocker-ce-stable docker-ce.x86_6417.12.1.ce-1.el7.centosdocker-ce-stable docker-ce.x86_6417.12.0.ce-1.el7.centosdocker-ce-stable docker-ce.x86_6417.09.1.ce-1.el7.centosdocker-ce-stable docker-ce.x86_6417.09.0.ce-1.el7.centosdocker-ce-stable docker-ce.x86_6417.06.2.ce-1.el7.centosdocker-ce-stable

  1. 选择相应版本进行安装(yun install docker-ce-版本号;下面版本17.12.0.ce是由17.12.0.ce-1.el7.centos得来)
    [root@localhost ~]# yum install docker-ce-17.12.0.ce 安装过程选y。当然也可以不指定版本安装,安装最新版本 [root@localhost ~]# yum install docker-ce

  2. 启动docker
    默认安装后,docker未启动 [root@localhost ~]# systemctl statusdocker ● docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled) Active: inactive (dead) Docs: https://docs.docker.com启动docker [root@localhost ~]# systemctl startdocker

  3. 查看docker版本(client和server都启动了则证明安装成功)
    [root@localhost ~]# docker version Client: Version:17.12.0-ce API version:1.35 Go version: go1.9.2 Git commit: c97c6d6 Built:Wed Dec 27 20:10:14 2017 OS/Arch:linux/amd64Server: Engine: Version:17.12.0-ce API version:1.35 (minimum version 1.12) Go version:go1.9.2 Git commit:c97c6d6 Built: Wed Dec 27 20:12:46 2017 OS/Arch:linux/amd64 Experimental:false [root@localhost ~]#

    1. 设置docke开机启动
    ``` [root@localhost ~]#systemctl enable docker [root@localhost ~]#chkconfig docker on```

国内镜像加速
国内从 DockerHub 拉取镜像有时会遇到困难,此时可以配置镜像加速器。Docker 官方和国内很多云服务商都提供了国内加速器服务。
  • Docker官方提供的中国镜像库(也不是很快):https://registry.docker-cn.com
  • 中国科技大学的镜像加速器: https://docker.mirrors.ustc.edu.cn
  • 其他加速器(阿里云)可点击了解详情:https://blog.csdn.net/M82_A1/article/details/91957886
  • 阿里云容器镜像 :https://help.aliyun.com/document_detail/60750.html?spm=5176.10695662.1996646101.searchclickresult.3ff91ef8PXqlUd
  • 阿里云镜像加速器(每一个用户一个自己的加速器地址):登录后可查看自己专属镜像加速器
    在/etc/docker/daemon.json添加下面镜像库。
{"registry-mirrors":["https://docker.mirrors.ustc.edu.cn","https://registry.docker-cn.com"]}

之后重新启动服务(划重点,很多资料都没有这一步,当我们新增了daemon.json文件后必须重载docker才能生效):
[root@localhost ~]#systemctl daemon-reload [root@localhost ~]#systemctl restart docker

检查加速器是否生效:
[root@localhost ~]# docker infodocker info返回内容包含下面信息,则代表镜像库设置成功: Registry Mirrors: https://docker.mirrors.ustc.edu.cn/ https://registry.docker-cn.com/

运行第一个容器 hello-world
第一次执行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:latestHello 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 bashShare 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/

其他的镜像可以在镜像中心找:https://hub.docker.com/
建立docker用户组
默认情况下,docker 命令会使用 Unix socket 与 Docker 引擎通讯。而只有 root 用户和 docker 组的用户才可以访问 Docker 引擎的 Unix socket。出于安全考虑,一般 Linux 系统上不会直接使用 root 用户。因此,更好地做法是将需要使用 docker 的用户加入 docker 用户组。
授权后,当前用户不需要每次进行执行docker命令时,都加上sudo。不操作也没不影响运行。
如果你当前的用户就是docker组,则不必操作下面2、3等操作了。 1、[docker@localhost ~]$ groups $USER docker : docker [docker@localhost ~]$ 2、[root@localhost ~]#groupadd docker--docker为新建组名(新建组名必须是docker) 3、[root@localhost ~]#gpasswd -a hadoop docker --将当前用户hadoop加入到docker用户组里面。用户名根据你当前的用户进行进行替换。4、设置-注销系统或重启系统使第二步生效,重新登录系统后,新开命令行窗口即可; 5、[root@localhost ~]#systemctl docker start 6、[hadoop@localhost ~] docker run hello-world

参考材料:
【【Docker笔记】CentOS7安装Docker】作者:陈集福
链接:https://www.jianshu.com/p/6bed6e37eb1c

    推荐阅读