天天看點

Ubuntu安裝 Docker前提條件解除安裝舊版本使用 APT 安裝安裝 Docker使用腳本自動安裝啟動 Docker建立 docker 使用者組測試 Docker 是否安裝正确

前提條件

Docker 需要在64位版本的Ubuntu上安裝。此外,你還需要保證你的 Ubuntu 核心的最小版本不低于 3.10,其中3.10 小版本和更新維護版也是可以使用的。

在低于3.10版本的核心上運作 Docker 會丢失一部分功能。在這些舊的版本上運作 Docker 會出現一些BUG,這些BUG在一定的條件裡會導緻資料的丢失,或者報一些嚴重的錯誤。

Docker 可以安裝在 64 位的 x86 平台或 ARM 平台上。Ubuntu 發行版中,LTS(Long-Term-Support)長期支援版本,會獲得 5 年的更新維護支援,這樣的版本會更穩定,是以在生産環境中推薦使用 LTS 版本。

打開控制台使用 uname -r指令來檢視你目前的核心版本。

uname -r 
           

解除安裝舊版本

舊版本的 Docker 稱為

docker

或者

docker-engine

,使用以下指令解除安裝舊版本:

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

使用 APT 安裝

由于

apt

源使用 HTTPS 以確定軟體下載下傳過程中不被篡改。是以,我們首先需要添加使用 HTTPS 傳輸的軟體包以及 CA 證書。

sudo apt-get update


sudo apt-get install 
    apt-transport-https 
    ca-certificates 
    curl 
    gnupg 
    lsb-release
           

鑒于國内網絡問題,強烈建議使用國内源,官方源請在注釋中檢視。

為了确認所下載下傳軟體包的合法性,需要添加軟體源的

GPG

密鑰。

$ curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg


# 官方源
# $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
           

然後,我們需要向

sources.list

中添加 Docker 軟體源

$ echo 
  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu 
           

$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# 官方源
# $ echo 
#   "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu 
#   $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
           

以上指令會添加穩定版本的 Docker APT 鏡像源,如果需要測試版本的 Docker 請将 stable 改為 test。

安裝 Docker

更新 apt 軟體包緩存,并安裝

docker-ce

$ sudo apt-get update

$ sudo apt-get install docker-ce docker-ce-cli containerd.io
           

使用腳本自動安裝

在測試或開發環境中 Docker 官方為了簡化安裝流程,提供了一套便捷的安裝腳本,Ubuntu 系統上可以使用這套腳本安裝,另外可以通過

--mirror

選項使用國内源進行安裝:

若你想安裝測試版的 Docker, 請從 test.docker.com 擷取腳本
# $ curl -fsSL test.docker.com -o get-docker.sh
$ curl -fsSL get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh --mirror Aliyun
# $ sudo sh get-docker.sh --mirror AzureChinaCloud
           

執行這個指令後,腳本就會自動的将一切準備工作做好,并且把 Docker 的穩定(stable)版本安裝在系統中。

啟動 Docker

sudo systemctl enable docker
 sudo systemctl start docker
           

建立 docker 使用者組

預設情況下,docker 指令會使用 Unix socket 與 Docker 引擎通訊。而隻有 root 使用者和 docker 組的使用者才可以通路 Docker 引擎的

Unix socket

。出于安全考慮,一般 Linux 系統上不會直接使用

root

使用者。是以,更好地做法是将需要使用

docker

的使用者加入

docker

使用者組。

建立 docker 組:

$ sudo groupadd docker
           

将目前使用者加入 docker 組:

$ sudo usermod -aG docker $USER
           

退出目前終端并重新登入,進行如下測試。

測試 Docker 是否安裝正确

[email protected]:~$ sudo docker run --rm hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:37a0b92b08d4919615c3ee023f7ddb068d12b8387475d64c622ac30f45c29c51
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/
           

若能正常輸出以上資訊,則說明安裝成功

參考文檔

Docker 官方 Ubuntu 安裝文檔