本期通過在樹莓派上配置 Docker,來介紹 Docker 應用,歡迎來到“萬物皆可 Docker”的時代!

什麼是 Docker ?

Docker 最初是 dotCloud 公司創始人 Solomon Hykes 在法國期間發起的一個公司內部專案,它是基於 dotCloud 公司多年雲服務技術的一次革新,並於 2013 年 3 月以 Apache 2.0 授權協議開源,主要專案程式碼在 GitHub 上進行維護。Docker 專案後來還加入了 Linux 基金會,併成立推動 開放容器聯盟(OCI)。

Docker 是一個開放原始碼軟體,是一個開放平臺,用於開發應用、交付(shipping)應用、執行應用。 Docker 允許使用者將基礎設施(Infrastructure)中的應用單獨分割出來,形成更小的顆粒(容器),從而提高交付軟體的速度。

Docker 容器與虛擬機器類似,但二者在原理上不同。容器是將作業系統層虛擬化,虛擬機器則是虛擬化硬體,因此容器更具有便攜性、高效地利用伺服器。 容器更多的用於表示 軟體的一個標準化單元。由於容器的標準化,因此它可以無視基礎設施(Infrastructure)的差異,部署到任何一個地方。另外,Docker 也為容器提供更強的業界的隔離相容。

這裡可以請成龍大哥來代言 Docker,“我用完之後是什麼樣子,你用完之後就是什麼樣子”,相比洗髮水廣告代言來說,代言 Docker 可以說是更能把握到精髓。

Docker 利用 Linux 核心中的資源分離機制,例如 cgroups,以及 Linux 核心名字空間(namespaces),來建立獨立的容器(containers)。這可以在單一 Linux 實體下運作,避免引導一個虛擬機器造成的額外負擔。Linux 核心對名字空間的支援完全隔離了工作環境中應用程式的視野,包括行程樹、網路、使用者ID與掛載檔案系統,而核心的 cgroup 提供資源隔離,包括 CPU、儲存器、block I/O與網路。從 0.9 版本起,Dockers 在使用抽象虛擬是經由 libvirt 的 LXC 與 systemd - nspawn 提供介面的基礎上,開始包括libcontainer 庫做為以自己的方式開始直接使用由 Linux 核心提供的虛擬化的設施。

Docker 指令碼化安裝

#更新系統源
pi@raspberrypi:~ $ sudo apt-get update
命中:1 http://mirrors.aliyun.com/raspbian/raspbian buster InRelease
命中:2 http://packages.microsoft.com/repos/code stable InRelease
正在讀取軟體包列表... 完成 #指令碼化安裝
pi@raspberrypi:~ $ sudo curl -sSL https://get.docker.com | sh
# Executing docker install script, commit: 7cae5f8b0decc17d6571f9f52eb840fbc13b2737
+ sudo -E sh -c apt-get update -qq >/dev/null
+ sudo -E sh -c DEBIAN_FRONTEND=noninteractive apt-get install -y -qq apt-transport-https ca-certificates curl >/dev/null
+ sudo -E sh -c curl -fsSL "https://download.docker.com/linux/raspbian/gpg" | apt-key add -qq - >/dev/null
Warning: apt-key output should not be parsed (stdout is not a terminal)
+ sudo -E sh -c echo "deb [arch=armhf] https://download.docker.com/linux/raspbian buster stable" > /etc/apt/sources.list.d/docker.list
+ sudo -E sh -c apt-get update -qq >/dev/null
+ [ -n ]
+ sudo -E sh -c apt-get install -y -qq --no-install-recommends docker-ce >/dev/null
+ [ -n 1 ]
+ sudo -E sh -c DEBIAN_FRONTEND=noninteractive apt-get install -y -qq docker-ce-rootless-extras >/dev/null
+ sudo -E sh -c docker version
Client: Docker Engine - Community
Version: 20.10.7
API version: 1.41
Go version: go1.13.15
Git commit: f0df350
Built: Wed Jun 2 11:57:27 2021
OS/Arch: linux/arm
Context: default
Experimental: true Server: Docker Engine - Community
Engine:
Version: 20.10.7
API version: 1.41 (minimum version 1.12)
Go version: go1.13.15
Git commit: b0f5bc3
Built: Wed Jun 2 11:55:25 2021
OS/Arch: linux/arm
Experimental: false
containerd:
Version: 1.4.8
GitCommit: 7eba5930496d9bbe375fdf71603e610ad737d2b2
runc:
Version: 1.0.0
GitCommit: v1.0.0-0-g84113ee
docker-init:
Version: 0.19.0
GitCommit: de40ad0 ================================================================================ To run Docker as a non-privileged user, consider setting up the
Docker daemon in rootless mode for your user: dockerd-rootless-setuptool.sh install Visit https://docs.docker.com/go/rootless/ to learn about rootless mode. To run the Docker daemon as a fully privileged service, but granting non-root
users access, refer to https://docs.docker.com/go/daemon-access/ WARNING: Access to the remote API on a privileged Docker daemon is equivalent
to root access on the host. Refer to the 'Docker daemon attack surface'
documentation for details: https://docs.docker.com/go/attack-surface/ ================================================================================

測試 Docker

執行 hello-world 映象來測試 Docker 是否安裝成功。

pi@raspberrypi:~ $ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2c7ed585684a: Pull complete
Digest: sha256:df5f5184104426b65967e016ff2ac0bfcd44ad7899ca3bbcf8e44e4461491a9e
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.
(arm32v7)
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 版本
docker -v
sudo docker pull 倉庫/映象:版本(留空的話預設為 latest)
sudo docker run 加引數,用來建立容器
#檢視執行容器
sudo docker ps
#檢視所有下載的映象
sudo docker images
#進入容器終端
sudo docker exec -i -t ha /bin/bash
#實時檢視10行的 ha 日誌
sudo docker logs -f -t --tail 10 ha
#重啟 systemctl 守護程序
sudo systemctl daemon-reload
#設定 Docker 開機啟動
sudo systemctl enable docker
#開啟 Docker 服務
sudo systemctl start docker

安裝 Docker 圖形化介面

下面簡要介紹安裝 Docker 圖形化介面 Portainer,通過圖形化介面對本地的 Dcoker 進行管理。

#下載 Docker 圖形化介面 portainer
pi@raspberrypi:~ $ sudo docker pull portainer/portainer
Using default tag: latest
latest: Pulling from portainer/portainer
94cfa856b2b1: Pull complete
49d59ee0881a: Pull complete
1101392a3cc7: Pull complete
Digest: sha256:fb45b43738646048a0a0cc74fcee2865b69efde857e710126084ee5de9be0f3f
Status: Downloaded newer image for portainer/portainer:latest
docker.io/portainer/portainer:latest #建立 portainer 容器
pi@raspberrypi:~ $ sudo docker volume create portainer_data
portainer_data #執行 portainer
pi@raspberrypi:~ $ sudo docker run -d -p 9000:9000 --name portainer --restart always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer
fac6e689051b35dfb7993af096f116ae673be69252ac2661cf7b8a8ebbc0932d

安裝完成後就可以通過訪問瀏覽器 http://樹莓派ip:9000 來登入 portainer 介面。

結 語

後續我將介紹一些 Docker 應用的配置,通過 Docker 應用,我們可以將樹莓派實現多個應用共存,而不是過一段時間就刷一個新的系統映象,提高樹莓派的利用率。

Enjoy!

歡迎關注我的公眾號,持續更新中~~~