ubuntu 20.04 LTS 安裝docker


ubuntu 20.04 LTS 安裝docker


2021年八月更新,移除了過時內容

零:卸載舊版本

Docker 的舊版本被稱為 docker,docker.io 或 docker-engine 。如果已安裝,請卸載它們:

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

當前稱為 Docker Engine-Community 軟件包 docker-ce 。

一:設置倉庫

在新主機上首次安裝 Docker Engine-Community 之前,需要設置 Docker 倉庫。之后,您可以從倉庫安裝和更新 Docker 。

更新 apt 包索引。

$ sudo apt update

安裝依賴包:

sudo apt install apt-transport-https ca-certificates curl gnupg2 software-properties-common

添加GPG密鑰

添加 Docker 的官方 GPG 密鑰:

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

如果你在阿里雲上部署,可能使用阿里雲的鏡像源(在下面),添加阿里雲的證書會更好一些。添加阿里雲證書:

$ curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

添加軟件源

對於 amd64 架構的計算機,添加清華大學TUNA軟件倉庫:

sudo add-apt-repository \
   "deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

你也可以使用阿里雲的軟件倉庫:

sudo add-apt-repository \
    "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \
    $(lsb_release -cs) \
    stable"

如果你對你的網絡比較有信心,也可以使用官方的軟件倉庫:

sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) \
  stable"

如果你是ARM架構,請運行:

echo "deb [arch=armhf] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \
     $(lsb_release -cs) stable" | \
    sudo tee /etc/apt/sources.list.d/docker.list

二:安裝 Docker Engine-Community

更新 apt 包索引。

$ sudo apt update

安裝最新版本的 Docker Engine-Community

$ sudo apt install docker-ce

三:測試

注意:如果你的網絡和我一樣不好使,請先行進行鏡像加速再測試。

輸入:

$ sudo docker run hello-world

如果docker和你問好——或者說出現類似以下輸出:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Already exists 
Digest: sha256:7d91b69e04a9029b99f3585aaaccae2baa80bcf318f4a5d2165a9898cd2dc0a1
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/

就說明安裝完事了。

四:免sudo運行&鏡像加速

免sudo

參考如下方法將用戶添加到docke組

$ sudo usermod -aG docker [你的用戶名]

重啟docker

$ sudo systemctl restart docker

(然而我的機器上發生了很詭異的事——我重啟docker服務后依然不能以普通用戶啟動docker。重啟機器后才能免sudo運行。)

docker鏡像加速

我用了阿里雲的免費加速

https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors

后續設置按照阿里雲網頁內提示即可,不再贅述。

五:參考資料

-Windows10 安裝 WSL2 Ubuntu20.04 並設置 docker 環境
-Ubuntu Docker 安裝
-Docker CE 鏡像 阿里雲開發者社區
-Docker Community Edition 鏡像使用幫助 清華大學開源鏡像站


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM