## CentOS 安裝 Docker CE
>警告:切勿在沒有配置 Docker YUM 源的情況下直接使用 yum 命令安裝 Docker.
### 準備工作
#### 系統要求
Docker CE 支持 64 位版本 CentOS 7,并且要求內核版本不低于 3.10。 CentOS 7 滿足最低內核的要求,但由于內核版本比較低,部分功能(如 `overlay2` 存儲層驅動)無法使用,并且部分功能可能不太穩定。
#### 卸載舊版本
舊版本的 Docker 稱為 `docker` 或者 `docker-engine`,使用以下命令卸載舊版本:
```bash
$ 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
```
### 使用 yum 安裝
執行以下命令安裝依賴包:
```bash
$ sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
```
鑒于國內網絡問題,強烈建議使用國內源,官方源請在注釋中查看。
執行下面的命令添加 `yum` 軟件源:
```bash
$ sudo yum-config-manager \
--add-repo \
https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
# 官方源
# $ sudo yum-config-manager \
# --add-repo \
# https://download.docker.com/linux/centos/docker-ce.repo
```
如果需要測試版本的 Docker CE 請使用以下命令:
```bash
$ sudo yum-config-manager --enable docker-ce-test
```
如果需要每日構建版本的 Docker CE 請使用以下命令:
```bash
$ sudo yum-config-manager --enable docker-ce-nightly
```
#### 安裝 Docker CE
更新 `yum` 軟件源緩存,并安裝 `docker-ce`。
```bash
$ sudo yum makecache fast
$ sudo yum install docker-ce
```
### 使用腳本自動安裝
在測試或開發環境中 Docker 官方為了簡化安裝流程,提供了一套便捷的安裝腳本,CentOS 系統上可以使用這套腳本安裝:
```bash
$ curl -fsSL get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh --mirror Aliyun
```
執行這個命令后,腳本就會自動的將一切準備工作做好,并且把 Docker CE 的 Edge 版本安裝在系統中。
### 啟動 Docker CE
```bash
$ sudo systemctl enable docker
$ sudo systemctl start docker
```
### 建立 docker 用戶組
默認情況下,`docker` 命令會使用 [Unix socket](https://en.wikipedia.org/wiki/Unix_domain_socket) 與 Docker 引擎通訊。而只有 `root` 用戶和 `docker` 組的用戶才可以訪問 Docker 引擎的 Unix socket。出于安全考慮,一般 Linux 系統上不會直接使用 `root` 用戶。因此,更好地做法是將需要使用 `docker` 的用戶加入 `docker` 用戶組。
建立 `docker` 組:
```bash
$ sudo groupadd docker
```
將當前用戶加入 `docker` 組:
```bash
$ sudo usermod -aG docker $USER
```
退出當前終端并重新登錄,進行如下測試。
### 測試 Docker 是否安裝正確
```bash
$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:be0cd392e45be79ffeffa6b05338b98ebb16c87b255f48e297ec7f98e123905c
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://cloud.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/
```
若能正常輸出以上信息,則說明安裝成功。
### 鏡像加速
鑒于國內網絡問題,后續拉取 Docker 鏡像十分緩慢,強烈建議安裝 Docker 之后配置 [國內鏡像加速](mirror.md)。
### 添加內核參數
默認配置下,如果在 CentOS 使用 Docker CE 看到下面的這些警告信息:
```bash
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
```
請添加內核配置參數以啟用這些功能。
```bash
$ sudo tee -a /etc/sysctl.conf <<-EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
```
然后重新加載 `sysctl.conf` 即可
```bash
$ sudo sysctl -p
```
### 參考文檔
* [Docker 官方 CentOS 安裝文檔](https://docs.docker.com/engine/installation/linux/docker-ce/centos/)。
- 前言
- 修訂記錄
- 如何貢獻
- Docker 簡介
- 什么是 Docker
- 為什么要用 Docker
- 基本概念
- 鏡像
- 容器
- 倉庫
- 安裝 Docker
- Ubuntu
- Debian
- CentOS
- Raspberry Pi
- macOS
- Windows PC
- 鏡像加速器
- 使用鏡像
- 獲取鏡像
- 列出鏡像
- 刪除本地鏡像
- 利用 commit 理解鏡像構成
- 使用 Dockerfile 定制鏡像
- Dockerfile 指令詳解
- COPY 復制文件
- ADD 更高級的復制文件
- CMD 容器啟動命令
- ENTRYPOINT 入口點
- ENV 設置環境變量
- ARG 構建參數
- VOLUME 定義匿名卷
- EXPOSE 暴露端口
- WORKDIR 指定工作目錄
- USER 指定當前用戶
- HEALTHCHECK 健康檢查
- ONBUILD 為他人作嫁衣裳
- 參考文檔
- Dockerfile 多階段構建
- 其它制作鏡像的方式
- 實現原理
- 操作容器
- 啟動
- 守護態運行
- 終止
- 進入容器
- 導出和導入
- 刪除
- 訪問倉庫
- Docker Hub
- 私有倉庫
- 私有倉庫高級配置
- Nexus 3
- 數據管理
- 數據卷
- 掛載主機目錄
- 使用網絡
- 外部訪問容器
- 容器互聯
- 配置 DNS
- 高級網絡配置
- 快速配置指南
- 容器訪問控制
- 端口映射實現
- 配置 docker0 網橋
- 自定義網橋
- 工具和示例
- 編輯網絡配置文件
- 實例:創建一個點到點連接
- Docker 三劍客之 Compose 項目
- 簡介
- 安裝與卸載
- 使用
- 命令說明
- Compose 模板文件
- 實戰 Django
- 實戰 Rails
- 實戰 WordPress
- Docker 三劍客之 Machine 項目
- 安裝
- 使用
- Docker 三劍客之 Docker Swarm
- Swarm mode
- 基本概念
- 創建 Swarm 集群
- 部署服務
- 使用 compose 文件
- 管理敏感數據
- 管理配置信息
- 滾動升級
- 安全
- 內核命名空間
- 控制組
- 服務端防護
- 內核能力機制
- 其它安全特性
- 總結
- 底層實現
- 基本架構
- 命名空間
- 控制組
- 聯合文件系統
- 容器格式
- 網絡
- Etcd 項目
- 簡介
- 安裝
- 集群
- 使用 etcdctl
- CoreOS 項目
- 簡介
- 工具
- 快速搭建 CoreOS 集群
- Kubernetes 項目
- 簡介
- 快速上手
- 基本概念
- kubectl 使用
- 架構設計
- Mesos - 優秀的集群資源調度平臺
- Mesos 簡介
- 安裝與使用
- 原理與架構
- Mesos 配置項解析
- 日志與監控
- 常見應用框架
- 本章小結
- 容器與云計算
- 簡介
- 亞馬遜云
- 騰訊云
- 阿里云
- 小結
- 實戰案例-操作系統
- Busybox
- Alpine
- Debian Ubuntu
- CentOS Fedora
- 本章小結
- 實戰案例-CI/CD
- Drone
- Docker 開源項目
- LinuxKit
- 附錄
- 附錄一:常見問題總結
- 附錄二:熱門鏡像介紹
- Ubuntu
- CentOS
- MySQL
- MongoDB
- Redis
- Nginx
- WordPress
- Node.js
- 附錄三:Docker 命令查詢
- 附錄四:Dockerfile 最佳實踐
- 附錄五:資源鏈接
- 附錄六:Docker 中文資源