1. 程式人生 > 其它 >CentOS 7 安裝 Docker

CentOS 7 安裝 Docker

一. Docker 版本

隨著 Docker 的飛速發展,企業級功能的上線,更好的服務意味著需要支付一定的費用,目前 Docker 被分為兩個版本:

  • community-edition 社群版
  • enterprise-edition 企業版

Docker 企業版(EE)專為企業開發和 IT 團隊設計,可在大規模生產中構建,運送和執行關鍵業務應用程式。Docker EE 整合,認證和支援,為企業提供業界最安全的容器平臺,實現所有應用程式的現代化。作為一個以應用為中心的平臺,DockerEE 旨在加速和保護整個軟體供應鏈,從開發到在任何基礎設施上執行的生產。

我們學習 Docker 使用 CE 社群版即可。

二. 在 CentOS 上安裝 Docker 引擎

官方文件

1. 使用官方安裝指令碼自動安裝

安裝命令如下:

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

也可以使用國內 daocloud 一鍵安裝命令:

curl -sSL https://get.daocloud.io/docker | sh

2. 手動安裝

2.1 解除安裝舊版本
-- 較舊的 Docker 版本稱為 docker 或 docker-engine 。如果已安裝這些程式,請解除安裝它們以及相關的依賴項。
$ sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
2.2 設定 Docker 倉庫 yum 源
-- 在新主機上首次安裝 Docker Engine-Community 之前,需要設定 Docker 倉庫。之後,您可以從倉庫安裝和更新 Docker。
-- 安裝所需的軟體包。yum-utils 提供了 yum-config-manager 程式,並且 device mapper 儲存驅動程式需要 device-mapper-persistent-data 和 lvm2。
$ sudo yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2

使用以下命令來設定穩定的倉庫。

-- 使用官方源地址(比較慢)
$ sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

可以選擇國內的一些源地址:

-- 阿里雲
$ sudo yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

-- 清華大學源
$ sudo yum-config-manager \
    --add-repo \
    https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
2.3 安裝 Docker

安裝最新版本的 Docker Engine-Community 和 containerd,或者轉到下一步安裝特定版本:

$ sudo yum install docker-ce docker-ce-cli containerd.io
2.4 Docker 的啟動和停止
# 啟動 docker
sudo systemctl start docker
# 停止 docker
sudo systemctl stop docker
# 重啟 docker
sudo systemctl restart docker
# 設定開機啟動
sudo systemctl enable docker
# 檢視 docker 狀態
sudo systemctl status docker
# 檢視 docker 內容器的執行狀態
sudo docker stats
# 檢視 docker 概要資訊
sudo docker info
# 檢視 docker 幫助文件
sudo docker --help
2.5 安裝校驗
[root@localhost ~]# docker -v
Docker version 19.03.12, build 48a66213fe

[root@localhost ~]# docker version
Client: Docker Engine - Community
  Version: 19.03.12
  API version: 1.40
  Go version: go1.13.10
  Git commit: 48a66213fe
  Built: Mon Jun 22 15:46:54 2020
  OS/Arch: linux/amd64
  Experimental: false
Server: Docker Engine - Community
  Engine:
    Version: 19.03.12
    API version: 1.40 (minimum version 1.12)
    Go version: go1.13.10
    Git coBuilt: Mon Jun 22 15:45:28 2020
    OS/Arch: linux/amd64
    Experimental: false
  containerd:
    Version: 1.2.13
    GitCommit: 7ad184331fa3e55e52b890ea95e65ba581ae3429
  runc:
    Version: 1.0.0-rc10
    GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
  docker-init:
    Version: 0.18.0
    GitCommit: fec3683mmit: 48a66213fe

3. 配置映象加速

Docker 從 Docker Hub 拉取映象,因為是從國外獲取,所以速度較慢,會出現以下情況:

[root@localhost ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
docker: Error response from daemon: Get https://registry1.docker.io/v2/library/hello-world/manifests/latest: net/http: TLS
handshake timeout.
See 'docker run --help'.

可以通過配置國內映象源的方式,從國內獲取映象,提高拉取速度。
中國科學技術大學(LUG@USTC)的開源映象:https://docker.mirrors.ustc.edu.cn
網易的開源映象:http://hub-mirror.c.163.com
USTC 是老牌的 Linux 映象服務提供者了,USTC 的 Docker 映象加速器速度很快。USTC 和網易的優勢之一就是不需要註冊,屬於真正的公共服務。(也可以使用阿里等其他服務商的映象加速服務)

# 編輯檔案 /daemon.json
vi /etc/docker/daemon.json

# 在檔案中輸入以下內容並儲存
{"registry-mirrors": ["http://hub-mirror.c.163.com","https://docker.mirrors.ustc.edu.cn"]}

重新載入配置資訊及重啟 Docker 服務

# 重新載入某個服務的配置檔案
sudo systemctl daemon-reload
# 重新啟動 docker
sudo systemctl restart docker

4. hello-word

通過執行 hello-world 映象來驗證 Docker Engine 是否已正確安裝。

[root@localhost ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally # 本地找不到 helloworld 映象
latest: Pulling from library/hello-world # 拉取最新版本的 hello-world 映象
0e03bdcc26d7: Pull complete
Digest:
sha256:49a1c8800c94df04e9658809b006fd8a686cab8028d33cfba2cc049724254202
Status: Downloaded newer image for hello-world:latest

# 看到此訊息表示您已正常安裝。
Hello from Docker!
This message shows that your installation appears to be workingcorrectly.
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://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started