树莓派安装docker

警告:切勿在没有配置 Docker APT 源的情况下直接使用 apt 命令安装 Docker

参考内容

本文大部分内容参考至,在安装docker的时候其实应该是挺简单的,但是我个人就遇到了很多问题,很多其他人写的教案都不是很有效果,所以极力推荐大家查看我搬运的这篇

树莓派卡片电脑安装 Docker

Docker社区安装教程

什么是docker

请看我这篇文章 docker学习总结

为什么使用docker

树莓派的板子是ARM架构的芯片,很多软件不支持或者不兼容,但是使用docker的话就能统一环境,且能支持更多的软件;再者使用docker更便捷,只要写好脚本即可,无需其他繁琐的操作

系统要求

Docker CE 不仅支持 x86_64 架构的计算机,同时也支持 ARM 架构的计算机,本小节内容以树莓派单片电脑为例讲解 ARM 架构安装 Docker CE

Docker CE 支持以下版本的 Raspbian 操作系统:

  • Raspbian Stretch
  • Raspbian Jessie

Raspbian 是树莓派的开发与维护机构 树莓派基金会 推荐用于树莓派的首选系统,其基于 Debian

使用 APT 安装

准备工作

1. 更新源添加HTTPS证书等

由于 apt 源使用 HTTPS 以确保软件下载过程中不被篡改。因此,我们首先需要添加使用 HTTPS 传输的软件包以及 CA 证书。

1
2
3
4
5
6
7
8
9
10
11

$ sudo apt-get update

$ sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg2 \
lsb-release \
software-properties-common

鉴于国内网络问题,强烈建议使用国内源,官方源请在注释中查看。

2. 添加软件源的 GPG 密钥

为了确认所下载软件包的合法性,需要添加软件源的 GPG 密钥。

1
2
3
4
5
$ curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/raspbian/gpg | sudo apt-key add -


# 官方源
# $ curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -

验证GPG密钥,如果出现9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88 表示没问题

1
2
3
4
5
6
$ sudo apt-key fingerprint 0EBFCD88

pub 4096R/0EBFCD88 2017-02-22
Key fingerprint = 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88
uid Docker Release (CE deb) <docker@docker.com>
sub 4096R/F273FCD8 2017-02-22

3. 添加软件源

source.list 中添加 Docker CE 软件源

这里是最容易出问题的,我个人就是在这里失败了一整天,都不清楚为什么会失败,我用的树莓派4B用的是arm64 就可以

armhf
1
2
3
4
5
6
7
8
9
10
11
$ sudo add-apt-repository \
"deb [arch=armhf] https://mirrors.ustc.edu.cn/docker-ce/linux/raspbian \
$(lsb_release -cs) \
stable"


# 官方源
# $ sudo add-apt-repository \
# "deb [arch=armhf] https://download.docker.com/linux/raspbian \
# $(lsb_release -cs) \
# stable"
arm64
1
2
3
4
5
6
7
8
9
10
$ sudo add-apt-repository \
"deb [arch=arm64] https://mirrors.ustc.edu.cn/docker-ce/linux/raspbian \
$(lsb_release -cs) \
stable"

# 官方源
# $ sudo add-apt-repository \
"deb [arch=arm64] https://download.docker.com/linux/debian \
$(lsb_release -cs) \
stable"
我个人的添加方法是直接手动去添加的
1
2
3
4
5
$ sudo nano /etc/apt/sources.list

把下面这句添加进去不是把原来的删除

deb [arch=amd64] https://download.docker.com/linux/debian/ buster stable

以上命令会添加稳定版本的 Docker CE APT 源,如果需要最新版本的 Docker CE 请将 stable 改为 edge 或者 test。从 Docker 17.06 开始,edge test 版本的 APT 源也会包含稳定版本的 Docker CE。

安装 Docker CE

使用脚本自动安装 (推荐)

1
2
$ curl -fsSL get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh --mirror Aliyun

使用apt安装 (这里不推荐,容易失败且出问题)

更新 apt 软件包缓存,并安装 docker-ce

1
2
3
$ sudo apt-get update

$ sudo apt-get install docker-ce

搬运狗总结

我用apt命令试过无数次基本都是失败告终,所以推荐使用脚本自动安装,还有一点就是不要着急更新其他的软件源,用树莓派默认的比较好

启动 Docker CE

1
2
$ sudo systemctl enable docker
$ sudo systemctl start docker

测试 Docker 是否安装正确

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
$ docker run arm32v7/hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:be0cd392e45be79ffeffa6b05338b98ebb16c87b255f48e297ec7f98e123905c
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://cloud.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/

若能正常输出以上信息,则说明安装成功。

注意: ARM 平台不能使用 x86 镜像,查看 Raspbian 可使用镜像请访问 arm32v7

树莓派docker的使用

请查看后续的文章