使用 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>
安裝 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>
由于本地沒有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>