使用 yum 安裝(CentOS 7下)
Docker 要求 CentOS 系統(tǒng)的內(nèi)核版本高于 3.10 嗦锐,查看本頁面的前提條件來驗證你的CentOS 版本是否支持 Docker 坚踩。
通過 **uname -r **命令查看你當(dāng)前的內(nèi)核版本
<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>
安裝 Docker
從 2017 年 3 月開始 docker 在原來的基礎(chǔ)上分為兩個分支版本: Docker CE 和 Docker EE李根。
Docker CE 即社區(qū)免費版溪北,Docker EE 即企業(yè)版笙蒙,強調(diào)安全昔驱,但需付費使用豁延。
本文介紹 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>
安裝一些必要的系統(tǒng)工具:
<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 后臺服務(wù)
<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>
由于本地沒有hello-world這個鏡像秘豹,所以會下載一個hello-world的鏡像携御,并在容器內(nèi)運行。
使用腳本安裝 Docker
1既绕、使用 sudo
或 root
權(quán)限登錄 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誓军、執(zhí)行 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>
執(zhí)行這個腳本會添加 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
是否安裝成功并在容器中執(zhí)行一個測試的鏡像壹甥。
<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 系統(tǒng)的安裝完成壶熏。
鏡像加速
鑒于國內(nèi)網(wǎng)絡(luò)問題句柠,后續(xù)拉取 Docker 鏡像十分緩慢,我們可以需要配置加速器來解決久橙,我使用的是網(wǎng)易的鏡像地址: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
執(zhí)行以下命令來刪除 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>