前言

在Windows非伺服器版本安裝Docker可以透過msi安裝檔,在圖形化介面上按下一步就可以裝好放。在macOS上也有Homebrew可以快速安裝,連同VM環境都一起裝好。反而在Docker原生的Linux上比較麻煩點,官方的安裝教學看起來很長一串,因此本篇文章簡化教學內容,基本上跟著步驟複製貼上就可以完成囉。另外現在似乎也挺流行PodMan,有幾會也來試試看。

安裝教學

參考Docker官方的教學:https://docs.docker.com/engine/install/debian/

移除非官方Docker

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

安裝相關工具包

正式安裝Docker前要先安裝相關的工具,依照你使用Debian還是Ubuntu選擇對應的指令即可。

Debian安裝指令

sudo apt-get update
sudo apt-get install -y ca-certificates curl gnupg lsb-release

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

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Ubuntu安裝指令

sudo apt-get update
sudo apt-get install -y ca-certificates curl gnupg lsb-release

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

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

從這邊開始就不分Debian還是Ubuntu,直接複製就好囉。如果出現錯誤通常是上一步安裝相關工具時沒安裝好,要再檢查看看哪裡漏掉了。

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

可選:將目前使用者加入Docker群組

加入後就不用每個Docker指令都要加上sudo了。

sudo usermod -aG docker $(whoami)

測試安裝結果

sudo docker run hello-world

安裝成功後就可以執行Docker的Hello world Image了,順利的話可以看到類似以下結果,恭喜完成安裝!

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
(略...)

參考連結

發佈留言