树莓派Raspberry Pi OS(64位)安装Docker

备注

根据Docker官方说明,树莓派Raspberry Pi OS(64位)安装Docker的方法和Debian是一样的,所以本文也作为Debian安装Docker的参考

准备工作

防火墙

  • 如果 Ubuntu Linux / Debian 使用了 ufw防火墙服务 则需要设置防火墙允许访问docker输出的容器端口

  • docker只兼容 iptables-nftiptables-legacy ,通过 nft 创建的防火墙规则不被Docker安装的系统所支持。确保所有防火墙规则是通过 iptablesip6tables 创建,并且将规则添加到 DOCKER-USER 链路

操作系统

在64位Debian(Raspberry Pi OS)上支持以下版本:

  • Debian Bookworm 12 (stable)

  • Debian Bullseye 11 (oldstable)

Docker Engine for Debian 兼容 x86_64 (amd64), armhf, arm64, 以及 ppc64le (ppc64el) 架构

卸载旧版本

安装Docker官方的Docker Engine之前,务必卸载系统上任何冲突版本:

  • docker.io

  • docker-compose

  • docker-doc

  • podman-docker

卸载系统上的旧版本Docker
for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove $pkg; done

安装

Docker官方文档提供了多种安装途径,不过我的实践只选择通过官方 APT包管理 仓库安装

  • 设置 Debian(Raspberry Pi OS) 的Docker apt仓库:

设置Docker的apt仓库
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
设置操作系统环境配置了代理变量 /etc/environment
HTTP_PROXY="http://127.0.0.1:3128"
HTTPS_PROXY="http://127.0.0.1:3128"
NO_PROXY="*.baidu.com,192.168.0.0/16,10.0.0.0/8"
http_proxy="http://127.0.0.1:3128"
https_proxy="http://127.0.0.1:3128"
no_proxy="*.baidu.com,192.168.0.0/16,10.0.0.0/8"
  • 安装Docker最新版本:

安装Docker
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
将当前用户添加到 docker 用户组
sudo usermod -aG docker $USER

验证

按照官方文档提供的简单测试进行验证:

运行 hello world 验证docker安装
docker run hello-world

输出显示正确运行:

运行 hello world 验证docker安装的输出显示
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
478afc919002: Pull complete
Digest: sha256:91fb4b041da273d5a3273b6d587d62d518300a6ad268b28628f74997b93171b2
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.
    (arm64v8)
 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/

并且可以进一步按照上述提示实际测试一个 Ubuntu Linux 容器运行:

运行 Ubuntu Linux 容器
docker run -it ubuntu bash

参考