[雲原生]Docker - 安裝&解除安裝

SkyBiuBiu發表於2021-12-08

參考:

系統要求

(以CentOS為例)安裝Docker Engine,需要maintain version的CentOS 7 或者 8,Archived version無法支援。

centos-extras倉庫必須啟用,預設情況下這個倉庫是啟用的,如果之前禁用了的話,需要重新啟用它。

overlay2儲存驅動也是必要的。

解除安裝舊版本

舊版本的Docker叫做docker或者docker-engine。如果這些先前被安裝了,先解除安裝他們,以及相關的依賴包。

sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

/var/lib/docker目錄中,保留了映象、容器、卷和網路的一些相關檔案。Docker Engine軟體包現在叫做docker-ce

安裝Docker

可以通過不同的方式安裝Docker,取決於個人需求:

  • 大多數使用者會通過設定Docker倉庫的方式,便於安裝與升級,這是一種推薦的方式。

  • 還有的使用者會下載RPM包然後手動的安裝與升級,適用於一些場景,比如說在一些沒有聯網的環境中安裝Docker。

  • 在測試或者開發環境中,有的使用者還會選擇使用自動化指令碼來進行安裝。

方法一:通過repo安裝

在安裝Docker engine在一臺新機器之前,需要設定Docker repository。然後才能夠通過repo來進行安裝和升級。

設定Repository

安裝yum-utils包(提供yum-config-manager命令),然後設定穩定的倉庫。

sudo yum install -y yum-utils
sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
[root@localhost /]# yum repolist
repo id                                repo name
appstream                              CentOS Linux 8 - AppStream
baseos                                 CentOS Linux 8 - BaseOS
docker-ce-stable                       Docker CE Stable - x86_64
extras                                 CentOS Linux 8 - Extras

安裝Docker Engine

  1. 安裝最新版本的Docker Engine和containerd,或者到下一步去安裝特殊的版本。

    sudo yum install docker-ce docker-ce-cli containerd.io
    
  2. 安裝特殊版本的Docker Engine,先檢視倉庫列表中的可用版本,然後選擇安裝。

    [root@localhost /]# yum list docker-ce --showduplicates | sort -r
    docker-ce.x86_64                3:20.10.9-3.el8                 docker-ce-stable
    docker-ce.x86_64                3:20.10.8-3.el8                 docker-ce-stable
    docker-ce.x86_64                3:20.10.7-3.el8                 docker-ce-stable
    docker-ce.x86_64                3:20.10.6-3.el8                 docker-ce-stable
    docker-ce.x86_64                3:20.10.5-3.el8                 docker-ce-stable
    docker-ce.x86_64                3:20.10.4-3.el8                 docker-ce-stable
    docker-ce.x86_64                3:20.10.3-3.el8                 docker-ce-stable
    docker-ce.x86_64                3:20.10.2-3.el8                 docker-ce-stable
    docker-ce.x86_64                3:20.10.1-3.el8                 docker-ce-stable
    docker-ce.x86_64                3:20.10.11-3.el8                docker-ce-stable
    docker-ce.x86_64                3:20.10.10-3.el8                docker-ce-stable
    docker-ce.x86_64                3:20.10.0-3.el8                 docker-ce-stable
    docker-ce.x86_64                3:19.03.15-3.el8                docker-ce-stable
    docker-ce.x86_64                3:19.03.14-3.el8                docker-ce-stable
    docker-ce.x86_64                3:19.03.13-3.el8                docker-ce-stable
    Docker CE Stable - x86_64                        73 kB/s |  19 kB     00:00    
    Available Packages
    
    sudo yum install docker-ce-<version_string> docker-ce-cli-<version_string> containerd.io
    

​ 補充:如果這裡報錯,可以新增--allowerasing引數來移除一些衝突的包,如下:

image-20211208141905208

  1. 啟動Docker(開機啟動)

    systemctl enable --now docker
    
  2. 通過hello-world映象,檢驗是否成功安裝Docker。

    sudo docker run hello-world
    

升級Docker Engine

去看官網的安裝指導,選擇新版本安裝。Install Docker Engine on CentOS | Docker Documentation

方法二:通過package安裝

如果沒辦法通過Docker's repository安裝Docker,你可以通過下載你需要的版本的.rpm包來手動安裝Docker,在你需要更新的時候再下載新的包來更新。

  1. Index of linux/centos/ (docker.com)選擇CentOS對應版本的倉庫。然後檢視x86_64/stable/Packages目錄,下載相應的.rpm包來安裝。

  2. 安裝docker

    sudo yum install /path/to/package.rpm
    
  3. 啟動Docker

    sudo systemctl enable --now docker
    
  4. 檢查是否安裝成功

    sudo docker run hello-world
    

方法三:通過指令碼安裝

Docker官方提供了一個指令碼在https://get.docker.com,可以通過它去安裝Docker環境無需互動。這個指令碼不推薦在生產環境中使用,不過可以作為一個例子用於參考以量身定製屬於自己的指令碼。

curl -fsSL https://get.docker.com -o get-docker.sh
DRY_RUN=1 sh ./get-docker.sh

解除安裝Docker

  1. 解除安裝Docker Engine

    sudo yum remove docker-ce docker-ce-cli containerd.io
    
  2. 映象、容器、卷還有自定義配置的相關檔案還在主機上,沒有被自動清除,需要手動刪除,執行如下命令可手動刪除:

    sudo rm -rf /var/lib/docker
    sudo rm -rf /var/lib/containerd
    

hello-world映象

hello-world映象是官方提供用來測試docker環境是否搭建完成用的。

[root@localhost /]# sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:cc15c5b292d8525effc0f89cb299f1804f3a725c8d05e158653a563f15e4f685
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://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

</version_string></version_string>

相關文章