docker centos7下安装
使用 yum 安装(CentOS 7下)
Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker 。
通过 **uname -r **命令查看你当前的内核版本
<pre class="prettyprint prettyprinted" style="border-width: 1px 1px 1px 4px; border-style: solid; border-color: rgb(221, 221, 221); border-image: initial; margin: 15px auto; padding: 10px 15px; font: 400 12px/20px Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; white-space: pre-wrap; word-break: break-all; overflow-wrap: break-word; background: url("/images/codecolorer_bg.gif") center top rgb(251, 251, 251); color: rgb(51, 51, 51); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@runoob ~]# uname -r </pre>
r安装 Docker
从 2017 年 3 月开始 docker 在原来的基础上分为两个分支版本: Docker CE 和 Docker EE。
Docker CE 即社区免费版,Docker EE 即企业版,强调安全,但需付费使用。
本文介绍 Docker CE 的安装使用。
移除旧的版本:
<pre class="prettyprint prettyprinted" style="border-width: 1px 1px 1px 4px; border-style: solid; border-color: rgb(221, 221, 221); border-image: initial; margin: 15px auto; padding: 10px 15px; font: 400 12px/20px Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; white-space: pre-wrap; word-break: break-all; overflow-wrap: break-word; background: url("/images/codecolorer_bg.gif") center top rgb(251, 251, 251); color: rgb(51, 51, 51); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">$ sudo yum remove docker
docker-client
docker-client-latest
docker-common
docker-latest
docker-latest-logrotate
docker-logrotate
docker-selinux
docker-engine-selinux
docker-engine</pre>
安装一些必要的系统工具:
<pre class="prettyprint prettyprinted" style="border-width: 1px 1px 1px 4px; border-style: solid; border-color: rgb(221, 221, 221); border-image: initial; margin: 15px auto; padding: 10px 15px; font: 400 12px/20px Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; white-space: pre-wrap; word-break: break-all; overflow-wrap: break-word; background: url("/images/codecolorer_bg.gif") center top rgb(251, 251, 251); color: rgb(51, 51, 51); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">sudo yum install -y yum-utils device-mapper-persistent-data lvm2</pre>
添加软件源信息:
<pre class="prettyprint prettyprinted" style="border-width: 1px 1px 1px 4px; border-style: solid; border-color: rgb(221, 221, 221); border-image: initial; margin: 15px auto; padding: 10px 15px; font: 400 12px/20px Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; white-space: pre-wrap; word-break: break-all; overflow-wrap: break-word; background: url("/images/codecolorer_bg.gif") center top rgb(251, 251, 251); color: rgb(51, 51, 51); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo</pre>
更新 yum 缓存:
<pre class="prettyprint prettyprinted" style="border-width: 1px 1px 1px 4px; border-style: solid; border-color: rgb(221, 221, 221); border-image: initial; margin: 15px auto; padding: 10px 15px; font: 400 12px/20px Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; white-space: pre-wrap; word-break: break-all; overflow-wrap: break-word; background: url("/images/codecolorer_bg.gif") center top rgb(251, 251, 251); color: rgb(51, 51, 51); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">sudo yum makecache fast</pre>
安装 Docker-ce:
<pre class="prettyprint prettyprinted" style="border-width: 1px 1px 1px 4px; border-style: solid; border-color: rgb(221, 221, 221); border-image: initial; margin: 15px auto; padding: 10px 15px; font: 400 12px/20px Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; white-space: pre-wrap; word-break: break-all; overflow-wrap: break-word; background: url("/images/codecolorer_bg.gif") center top rgb(251, 251, 251); color: rgb(51, 51, 51); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">sudo yum -y install docker-ce</pre>
启动 Docker 后台服务
<pre class="prettyprint prettyprinted" style="border-width: 1px 1px 1px 4px; border-style: solid; border-color: rgb(221, 221, 221); border-image: initial; margin: 15px auto; padding: 10px 15px; font: 400 12px/20px Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; white-space: pre-wrap; word-break: break-all; overflow-wrap: break-word; background: url("/images/codecolorer_bg.gif") center top rgb(251, 251, 251); color: rgb(51, 51, 51); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">sudo systemctl start docker</pre>
测试运行 hello-world
<pre class="prettyprint prettyprinted" style="border-width: 1px 1px 1px 4px; border-style: solid; border-color: rgb(221, 221, 221); border-image: initial; margin: 15px auto; padding: 10px 15px; font: 400 12px/20px Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; white-space: pre-wrap; word-break: break-all; overflow-wrap: break-word; background: url("/images/codecolorer_bg.gif") center top rgb(251, 251, 251); color: rgb(51, 51, 51); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@runoob ~]# docker run hello-world</pre>
image由于本地没有hello-world这个镜像,所以会下载一个hello-world的镜像,并在容器内运行。
使用脚本安装 Docker
1、使用 sudo
或 root
权限登录 Centos。
2、确保 yum 包更新到最新。
<pre class="prettyprint prettyprinted" style="border-width: 1px 1px 1px 4px; border-style: solid; border-color: rgb(221, 221, 221); border-image: initial; margin: 15px auto; padding: 10px 15px; font: 400 12px/20px Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; white-space: pre-wrap; word-break: break-all; overflow-wrap: break-word; background: url("/images/codecolorer_bg.gif") center top rgb(251, 251, 251); color: rgb(51, 51, 51); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">$ sudo yum update</pre>
3、执行 Docker 安装脚本。
<pre class="prettyprint prettyprinted" style="border-width: 1px 1px 1px 4px; border-style: solid; border-color: rgb(221, 221, 221); border-image: initial; margin: 15px auto; padding: 10px 15px; font: 400 12px/20px Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; white-space: pre-wrap; word-break: break-all; overflow-wrap: break-word; background: url("/images/codecolorer_bg.gif") center top rgb(251, 251, 251); color: rgb(51, 51, 51); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> sudo sh get-docker.sh</pre>
执行这个脚本会添加 docker.repo
源并安装 Docker。
4、启动 Docker 进程。
<pre class="prettyprint prettyprinted" style="border-width: 1px 1px 1px 4px; border-style: solid; border-color: rgb(221, 221, 221); border-image: initial; margin: 15px auto; padding: 10px 15px; font: 400 12px/20px Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; white-space: pre-wrap; word-break: break-all; overflow-wrap: break-word; background: url("/images/codecolorer_bg.gif") center top rgb(251, 251, 251); color: rgb(51, 51, 51); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">sudo systemctl start docker</pre>
5、验证 docker
是否安装成功并在容器中执行一个测试的镜像。
<pre class="prettyprint prettyprinted" style="border-width: 1px 1px 1px 4px; border-style: solid; border-color: rgb(221, 221, 221); border-image: initial; margin: 15px auto; padding: 10px 15px; font: 400 12px/20px Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; white-space: pre-wrap; word-break: break-all; overflow-wrap: break-word; background: url("/images/codecolorer_bg.gif") center top rgb(251, 251, 251); color: rgb(51, 51, 51); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">$ sudo docker run hello-world
docker ps</pre>
到此,Docker 在 CentOS 系统的安装完成。
镜像加速
鉴于国内网络问题,后续拉取 Docker 镜像十分缓慢,我们可以需要配置加速器来解决,我使用的是网易的镜像地址:http://hub-mirror.c.163.com。
新版的 Docker 使用 /etc/docker/daemon.json(Linux) 或者 %programdata%\docker\config\daemon.json(Windows) 来配置 Daemon。
请在该配置文件中加入(没有该文件的话,请先建一个):
<pre class="prettyprint prettyprinted" style="border-width: 1px 1px 1px 4px; border-style: solid; border-color: rgb(221, 221, 221); border-image: initial; margin: 15px auto; padding: 10px 15px; font: 400 12px/20px Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; white-space: pre-wrap; word-break: break-all; overflow-wrap: break-word; background: url("/images/codecolorer_bg.gif") center top rgb(251, 251, 251); color: rgb(51, 51, 51); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">{ "registry-mirrors": ["http://hub-mirror.c.163.com"] }</pre>
删除 Docker CE
执行以下命令来删除 Docker CE:
<pre class="prettyprint prettyprinted" style="border-width: 1px 1px 1px 4px; border-style: solid; border-color: rgb(221, 221, 221); border-image: initial; margin: 15px auto; padding: 10px 15px; font: 400 12px/20px Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; white-space: pre-wrap; word-break: break-all; overflow-wrap: break-word; background: url("/images/codecolorer_bg.gif") center top rgb(251, 251, 251); color: rgb(51, 51, 51); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> sudo rm -rf /var/lib/docker</pre>