本文摘選自『Docker-從入門到實踐』,該書籍較詳細地記錄了 Docker 的知識,強烈推薦。 警告:切勿在沒有配置 Docker APT 源的情況下直接使用 apt 命令安裝 Docker. 準備工作 系統要求 Docker 支持以下版本的 Ubuntu 操作系統: Ubuntu Hirsute ...
本文摘選自『Docker-從入門到實踐』,該書籍較詳細地記錄了 Docker 的知識,強烈推薦。
警告:切勿在沒有配置 Docker APT 源的情況下直接使用 apt 命令安裝 Docker.
準備工作
系統要求
Docker 支持以下版本的 Ubuntu 操作系統:
- Ubuntu Hirsute 21.04
- Ubuntu Groovy 20.10
- Ubuntu Server 22.04 (LTS)
- Ubuntu Focal 20.04 (LTS)
- Ubuntu Bionic 18.04 (LTS)
Ubuntu 發行版中,LTS(Long-Term-Support)長期支持版本,會獲得 5 年的升級維護支持,這樣的版本會更穩定,因此在生產環境中推薦使用 LTS 版本。
卸載舊版本
舊版本的 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 是否安裝正確
$ docker run --rm hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:308866a43596e83578c7dfa15e27a73011bdd402185a84c5cd7f32a88b501a24
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 鏡像十分緩慢,可以配置 Docker 國內鏡像加速。國內很多雲服務商都提供了國內加速器服務,例如:
目前主流 Linux 發行版均已使用 systemd 進行服務管理,這裡介紹如何在使用 systemd 的 Linux 發行版中配置鏡像加速器。
請首先執行以下命令,查看是否在 docker.service
文件中配置過鏡像地址。
$ systemctl cat docker | grep '\-\-registry\-mirror'
如果該命令有輸出,那麼請執行 $ systemctl cat docker
查看 ExecStart=
出現的位置,修改對應的文件內容去掉 --registry-mirror
參數及其值,並按接下來的步驟進行配置。
如果以上命令沒有任何輸出,那麼就可以在 /etc/docker/daemon.json
中寫入如下內容(如果文件不存在請新建該文件):
{
"registry-mirrors": [
"https://hub-mirror.c.163.com",
"https://mirror.baidubce.com"
]
}
註意,一定要保證該文件符合 json 規範,否則 Docker 將不能啟動。
之後重新啟動服務。
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker