准备
基于前一篇:树莓派系统的安装及常用设置
系统版本:2021-01-11-raspios-buster-armhf
pi@raspberrypi:~ $ lsb_release -a
No LSB modules are available.
Distributor ID: Raspbian
Description: Raspbian GNU/Linux 10 (buster)
Release: 10
Codename: buster
安装
1.先更新和升级系统:
sudo apt-get update && sudo apt-get upgrade
2.下载安装脚本
curl -fsSL https://get.docker.com -o get-docker.sh
使用命令执行脚本,指定aliyun的源安装,不然慢死:
sudo sh get-docker.sh --mirror Aliyun
安装错误
按网上的教程,到上一步就安装完成了,但是我在安装过程中出现不少错误,记录一下踩过的坑。
1.首先错误如下:
pi@raspberrypi:~/Downloads $ sudo sh get-docker.sh
# Executing docker install script, commit: 3d8fe77c2c46c5b7571f94b42793905e5b3e42e4
+ sh -c apt-get update -qq >/dev/null
+ [ -n ]
+ sh -c apt-get install -y -qq --no-install-recommends docker-ce >/dev/null
E: Sub-process /usr/bin/dpkg returned an error code (1)
2.查看服务信息:
pi@raspberrypi:/var/log $ systemctl status docker.service
docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: activating (auto-restart) (Result: exit-code) since Sat 2021-02-27 20:06:10 CST; 1s ago
Docs: https://docs.docker.com
Process: 9836 ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock (code=exited, status=1/FAILURE)
Main PID: 9836 (code=exited, status=1/FAILURE)
3.发现是ExecStart这一行报错,打开/lib/systemd/system/docker.service文件,修改下面行:
# ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
# 改为
ExecStart=/usr/bin/dockerd
重新加载配置:systemctl daemon-reload
再次启动,发现依然报同样的错误。
网上几乎没有相关内容,只能自己摸索了。
4.鉴于没有什么有用的相关信息,试试手动去执行一下dockerd,果然信息多了不少:
pi@raspberrypi:/usr/bin $ sudo ./dockerd
INFO[2021-02-27T20:11:29.217180709+08:00] Starting up
INFO[2021-02-27T20:11:29.220263481+08:00] parsed scheme: "unix" module=grpc
INFO[2021-02-27T20:11:29.220333406+08:00] scheme "unix" not registered, fallback to default scheme module=grpc
INFO[2021-02-27T20:11:29.220749472+08:00] ccResolverWrapper: sending update to cc: {[{unix:///run/containerd/containerd.sock <nil> 0 <nil>}] <nil> <nil>} module=grpc
INFO[2021-02-27T20:11:29.220915635+08:00] ClientConn switching balancer to "pick_first" module=grpc
INFO[2021-02-27T20:11:29.224076388+08:00] parsed scheme: "unix" module=grpc
INFO[2021-02-27T20:11:29.224144108+08:00] scheme "unix" not registered, fallback to default scheme module=grpc
INFO[2021-02-27T20:11:29.224211718+08:00] ccResolverWrapper: sending update to cc: {[{unix:///run/containerd/containerd.sock <nil> 0 <nil>}] <nil> <nil>} module=grpc
INFO[2021-02-27T20:11:29.224254032+08:00] ClientConn switching balancer to "pick_first" module=grpc
ERRO[2021-02-27T20:11:29.241437859+08:00] failed to mount overlay: no such device storage-driver=overlay2
ERRO[2021-02-27T20:11:29.241636300+08:00] exec: "fuse-overlayfs": executable file not found in $PATH storage-driver=fuse-overlayfs
ERRO[2021-02-27T20:11:29.254323160+08:00] AUFS was not found in /proc/filesystems storage-driver=aufs
ERRO[2021-02-27T20:11:29.261245209+08:00] failed to mount overlay: no such device storage-driver=overlay
WARN[2021-02-27T20:11:29.275614628+08:00] Your kernel does not support cgroup memory limit
WARN[2021-02-27T20:11:29.275708515+08:00] Your kernel does not support CPU realtime scheduler
WARN[2021-02-27T20:11:29.275765903+08:00] Your kernel does not support cgroup blkio weight
WARN[2021-02-27T20:11:29.276013361+08:00] Your kernel does not support cgroup blkio weight_device
INFO[2021-02-27T20:11:29.276738125+08:00] Loading containers: start.
WARN[2021-02-27T20:11:29.289942975+08:00] Running modprobe bridge br_netfilter failed with message: modprobe: ERROR: ../libkmod/libkmod.c:586 kmod_search_moddep() could not open moddep file '/lib/modules/5.4.83-v7l+/modules.dep.bin'
主要是ERROR这几行,好像是文件系统驱动的相关错误,根据这个查找,发现在stackexchange上有遇到同样的错误,
https://raspberrypi.stackexchange.com/questions/114665/failed-to-mount-overlay-no-such-device-when-start-docker-in-raspberry-pi-4
5.看解决方案说是需要在Raspberry pi的内核级别打开cgroup,也就是需要在cmdline.txt文件中添加以下内容:
cgroup_enable=memory cgroup_memory=1 swapaccount=1
这是我修改后的cmdline.txt完整内容:
pi@raspberrypi:~ $ cat /boot/cmdline.txt
console=serial0,115200 console=tty1 root=PARTUUID=2d4b0811-02 rootfstype=ext4 elevator=deadline fsck.repair=yes cgroup_enable=memory cgroup_memory=1 swapaccount=1 rootwait quiet splash plymouth.ignore-serial-consoles
修改完后重启,再次进行安装。
6.我这里因为折腾时有改过不少配置的东西,所以再次安装之前进行了卸载:
sudo apt-get purge docker-ce
再次运行,会提示:
下列软件包是自动安装的并且现在不需要了: containerd.io docker-ce-cli 使用'sudo apt autoremove'来卸载它(它们)。
卸载:
sudo apt autoremove
查看是否有残留:
dpkg -l | grep docker
可能会有带rc标记的包残留,用以下命令删除:
dpkg -l | grep ^rc | cut -d' ' -f3 | sudo xargs dpkg --purge
7.再次安装启动,发现依然报步骤1、2的错误,但是在用步骤3进行修改之后,一切就正常了。
启动关闭docker:
systemctl start docker
systemctl stop docker
8.后面发现再把步骤3的改回去也能成功运行,但是在使用docker命令时会报:
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
ps:在我重装系统之后,步骤1之后先内核级别打开cgroup,之后步骤2发现一次成功了!
将用户添加到docker组
默认情况下,只有具有管理权限的用户( root用户)才能执行docker命令,如果不是root用户每次都要使用sudo比较麻烦。
我们可以将用户添加到Docker组,该组将允许执行Docker命令。
将用户添加到Docker组的语法为:
sudo usermod -aG docker [user_name]
这里将树莓派默认的pi用户添加到docker组:
sudo usermod -aG docker pi
注意操作后,需要重新登录pi用户才能起效。
设置docker源及存储目录
因为树莓派的系统是在TF卡上,所以空间有限,需要把镜像等存储目录设置为挂载的硬盘上。如何挂载参见上一篇。
另外指定一下使用国内的源。
在 /etc/docker/ 目录下新建 daemon.json 文件,添加如下内容,当然需要其它配置也可以加入:
{
"registry-mirrors": ["https://registry.docker-cn.com"],
"data-root": "/home/pi/data/docker"
}
这里要注意 data-root 使用的硬盘挂载目录 /home/pi/data/docker 文件系统格式最好跟树莓派的TF卡一致,一般情况下为ext4。
如果不一致或是docker不支持的格式,就会导致docker无法启动。