Ubuntu:安裝 Docker 至伺服器之完整教學

2021/10/14 1,059 3 作業系統 , 伺服器 , Linux , Ubuntu

本文要教大家在 Ubuntu 伺服器版本下安裝 Docker,參考自官方文檔。Docker 是一個開放原始碼軟體,是一個開放平台,用於開發、交付、執行應用,有人稱其為「新一代的虛擬化技術」,傳統上我們將硬體層面實現虛擬化,但需要有額外虛擬機管理軟體及作業系統,而 Docker 能做到作業系統上的虛擬化,這能讓我們直接使用本機上的作業系統進行虛擬化作業。它有三大核心概念,分別是映像檔(Image)、容器(Container)及倉庫(Repository),我們透過將唯讀的映像檔,再用該映像檔創立一個容器,容器可以說是一個輕量的沙箱,每個都是彼此獨立運作,互不影響,也能說容器是映像檔所創造的執行實例,最後我們還能透過倉庫來集中存放及管理映像檔,這可以在本機上做,也能在雲端上做,雲端上最大公開倉庫就是 Docker 官方提供的 Docker Hub

首先先檢查有沒有舊版的已安裝,請先解除安裝它們:docker、docker.io、docker-engine

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

接著就能開始安裝全新的 Docker 了!

一、設置安裝會用到的存放庫

請更新軟體包目錄與安裝某些軟體包以允許透過 HTTPS 使用存放庫:

sudo apt-get update

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

再來新增 Docker 官方的 GPG 密鑰:

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

使用以下命令設定穩定版(stable)存放庫。這邊還可以選擇加上每夜(nightly)版本或測試(test)版本。

echo \
  "deb [arch=$(dpkg --print-architecture) 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 引擎

請更新軟體包目錄,接著透過指令安裝最新版本的 Docker 引擎和容器,或者到下方安裝特定版本。

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

安裝特定版本

以下指令會列出您存放庫中可用的版本:

apt-cache madison docker-ce

列出範例如下:

docker-ce | 5:18.09.1~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu  xenial/stable amd64 Packages
docker-ce | 5:18.09.0~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu  xenial/stable amd64 Packages
docker-ce | 18.06.1~ce~3-0~ubuntu       | https://download.docker.com/linux/ubuntu  xenial/stable amd64 Packages
docker-ce | 18.06.0~ce~3-0~ubuntu       | https://download.docker.com/linux/ubuntu  xenial/stable amd64 Packages

使用上方列出的版本字串安裝特定版本,舉例來說 5:18.09.1~3-0~ubuntu-xenial

sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io

最後使用 hello-world 映像檔確認 Docker 引擎是否已安裝:

sudo docker run hello-world

小提醒!如果看到以下資訊:

docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.

請直接用以下指令重啟動 Docker:

sudo systemctl restart docker

或者用這個指令啟動 Docker 可能也行:

sudo service docker start

這樣就可以再次測試 hello-world 映像檔囉!成功的話會自動下載與執行,並顯示以下資訊:

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 了!另外,若要升級 Docker 引擎,請先執行 sudo apt-get update,然後按照剛剛的安裝說明選擇要安裝的新版本。


▲ 上方為 Ubuntu 的 LOGO,下方為 Docker 的 LOGO。

贊助廣告 ‧ Sponsor advertisements

留言區 / Comments

萌芽論壇