天天看點

Docker系列教程02-Docker安裝(CentOS7/Ubuntu/macOS/Windows)

原文: http://www.itmuch.com/docker/02-docker-install/

2.1 CentOS

2.1.1 系統要求

2.1.2 yum安裝

2.1.2.1 解除安裝老版本的Docker

在CentOS中,老版本Docker名稱是

docker

docker-engine

,而Docker CE的軟體包名稱是

docker-ce

。是以,如已安裝過老版本的Docker,需使用如下指令解除安裝。

sudo yum remove docker \
                  docker-common \
                  docker-selinux \
                  docker-engine
           

需要注意的是,執行該指令隻會解除安裝Docker本身,而不會删除Docker存儲的檔案,例如鏡像、容器、卷以及網絡檔案等。這些檔案儲存在

/var/lib/docker

目錄中,需要手動删除。

2.1.2.2 安裝倉庫

  1. 執行以下指令,安裝Docker所需的包。其中,

    yum-utils

    提供了

    yum-config-manager

    工具;

    device-mapper-persistent-data

    lvm2

    則是

    devicemapper

    存儲驅動所需的包。
    sudo yum install -y yum-utils device-mapper-persistent-data lvm2
               
  2. 執行如下指令,安裝

    stable

    倉庫。必須安裝

    stable

    倉庫,即使你想安裝

    edge

    test

    倉庫中的Docker建構版本。
    sudo yum-config-manager \
        --add-repo \
        https://download.docker.com/linux/centos/docker-ce.repo
               
  3. [可選] 執行如下指令,啟用

    edge

    test

    倉庫。edge/test倉庫其實也包含在了

    docker.repo

    檔案中,但預設是禁用的,可使用以下指令來啟用。
    sudo yum-config-manager --enable docker-ce-edge    # 啟用edge倉庫
    sudo yum-config-manager --enable docker-ce-test    # 啟用test倉庫
               
    如需再次禁用,可加上

    --disable

    标簽。例如,執行如下指令即可禁用edge倉庫。
    sudo yum-config-manager --disable docker-ce-edge
               
    TIPS:從Docker 17.06起,stable版本也會釋出到edge以及test倉庫中。

2.1.2.3 安裝Docker CE

  1. 執行以下指令,更新

    yum

    的包索引
    sudo yum makecache fast
               
  2. 執行如下指令即可安裝最新版本的Docker CE
    sudo yum install docker-ce
               
  3. 在生産環境中,可能需要指定想要安裝的版本,此時可使用如下指令列出目前可用的Docker版本。
    yum list docker-ce.x86_64  --showduplicates | sort -r
               
    這樣,列出版本後,可使用如下指令,安裝想要安裝的Docker CE版本。
    sudo yum install docker-ce-<VERSION>
               
  4. 啟動Docker
    sudo systemctl start docker
               
  5. 驗證安裝是否正确。
    sudo docker run hello-world
               
    這樣,Docker将會下載下傳測試鏡像,并使用該鏡像啟動一個容器。如能夠看到類似如下的輸出,則說明安裝成功。
    Unable to find image 'hello-world:latest' locally
    latest: Pulling from library/hello-world
    b04784fba78d: Pull complete
    Digest: sha256:f3b3b28a45160805bb16542c9531888519430e9e6d6ffc09d72261b0d26ff74f
    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.
     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/
               

2.1.2.4 更新Docker CE

如需更新Docker CE,隻需執行如下指令:

sudo yum makecache fast
           

然後按照安裝Docker的步驟,即可更新Docker。

2.1.2.5 參考文檔

CentOS 7安裝Docker官方文檔:

https://docs.docker.com/engine/installation/linux/docker-ce/centos/

,文檔中還講解了在CentOS 7中安裝Docker CE的其他方式,本文不作贅述。

2.1.3 shell一鍵安裝

curl -fsSL get.docker.com -o get-docker.sh
sudo sh get-docker.sh
           

搞定一切。

2.2 Ubuntu

2.2.1 系統要求

  • Docker支援以下版本的Ubuntu,要求64位。
    • Zesty 17.04
    • Xenial 16.04 (LTS)
    • Trusty 14.04 (LTS)
  • 支援運作的平台:

    x86_64

    armhf

    s390x(IBM Z)

    。其中,如選擇IBM Z,那麼隻支援Ubuntu Xenial以及Zesty。
  • 本文使用Ubuntu 16.04 LTS,下載下傳位址: http://cn.ubuntu.com/download/

2.2.2 安裝步驟

2.2.2.1 解除安裝老版本Docker

在Ubuntu中,老版本的軟體包名稱是

docker

或者

docker-engine

docker-ce

。是以,如已安裝過老版本的Docker,需要先解除安裝掉。執行以下指令,即可解除安裝老版本的Docker及其依賴。

sudo apt-get remove docker docker-engine docker.io
           

需要注意的是,執行該指令隻會解除安裝Docker本身,而不會删除Docker内容,例如鏡像、容器、卷以及網絡。這些檔案儲存在

/var/lib/docker

2.2.2.2 Ubuntu Trusty 14.04 額外建議安裝的包

除非你有不得已的苦衷,否則強烈建議安裝

linux-image-extra-*

軟體包,以便于Docker使用

aufs

存儲驅動。執行如下指令,即可安裝

linux-image-extra-*

sudo apt-get update

sudo apt-get install \
    linux-image-extra-$(uname -r) \
    linux-image-extra-virtual
           

對于Ubuntu 16.04或更高版本,Linux核心包含了對OverlayFS的支援,Docker CE預設會使用

overlay2

存儲驅動。

2.2.2.3 安裝倉庫

  1. 執行如下指令,更新

    apt

    的包索引。
    sudo apt-get update
               
  2. 執行如下指令,進而允許

    apt

    使用HTTPS倉庫。
    sudo apt-get install \
        apt-transport-https \
        ca-certificates \
        curl \
        software-properties-common
               
  3. 添加Docker官方的GPG key
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
               
    确認指紋是

    9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88

    sudo apt-key fingerprint 0EBFCD88
               
  4. stable

    倉庫。無論如何都必須安裝

    stable

    edge

    test

    倉庫中的Docker建構。如需添加

    edge

    test

    倉庫,可在如下指令中的“stable" 後,添加

    edge

    test

    或兩者。請視自己Ubuntu所運作的平台來執行如下指令。

    NOTE:如下指令中的

    lsb_release -cs

    子指令用于傳回您Ubuntu的發行版名稱,例如

    xenial

    。有時,在例如Linux Mint這樣的發行版中,您可能需要将如下指令中的

    $(lsb_release -cs)

    更改為系統的父級Ubuntu發行版。例如,如果您使用的是Linux Mint Rafaela,則可以使用

    trusty

    amd64:
    $ sudo add-apt-repository \
       "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
       $(lsb_release -cs) \
       stable"
               
    armhf:
    $ sudo add-apt-repository \
       "deb [arch=armhf] https://download.docker.com/linux/ubuntu \
       $(lsb_release -cs) \
       stable"
               
    s390x:
    $ sudo add-apt-repository \
       "deb [arch=s390x] https://download.docker.com/linux/ubuntu \
       $(lsb_release -cs) \
       stable"
               
    NOTE:從Docker 17.06起,stable版本也會釋出到edge以及test倉庫中。

2.2.2.4 安裝Docker CE

  1. apt

    包索引。
    sudo apt-get update
               
  2. 執行如下指令,即可安裝最新版本的Docker CE。任何已存在的Docker将會被覆寫安裝。
    sudo apt-get install docker-ce
               
    WARNING:如啟用了多個Docker倉庫,使用指令apt-get install 或apt-get update 指令安裝或更新時,如未指定版本,那麼将會安裝最新的版本。這可能不适合您的穩定性要求。
  3. 在生産環境中,我們可能需要指定想要安裝的版本,此時可使用如下指令列出目前可用的Docker版本。
    apt-cache madison docker-ce
               
    sudo apt-get install docker-ce=<VERSION>
               
    Docker daemon會自動啟動。
  4. sudo docker run hello-world
               

2.2.2.5 更新Docker CE

sudo apt-get update
           

2.2.2.6 參考文檔

Ubuntu安裝Docker官方文檔:

https://docs.docker.com/engine/installation/linux/docker-ce/ubuntu/

,文檔還講解了在Ubuntu中安裝Docker CE的其他方式,本文不作贅述。

2.3 macOS

2.3.1 系統要求

macOS Yosemite 10.10.3或更高版本

2.3.2 安裝步驟

2.4 Windows(docker for windows)

2.4.1 系統要求

Windows 10 Professional 或 Windows 10 Enterprise X64

對于Win 7,可使用Docker Toolbox(不建議使用)

2.4.2 安裝步驟

2.5 其他系統

詳見官方文檔:

https://docs.docker.com/engine/installation/

2.6 加速安裝

注冊阿裡雲,參考該頁面的内容安裝即可:

https://cr.console.aliyun.com/#/accelerator