在树莓派2上创建自己的 Docker 镜像-不需从 docker.com pull

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

在树莓派2[raspbian系统]上创建自己的 Docker 镜像-不需要从 docker.com pull

因内容可能会根据情况更新, 所以给出原始链接 本文原始链接

话说昨天费了半天劲终于在我的树莓派2上成功安装了一个 docker.io (主要是 docker 版本变化, 导致各个教程中描述不一, 而树莓派上可供 apt-get 安装的又不是最新版本), 具体过程可参考此文树莓派2下安装 docker 记录, 本以为接下来就能很简单地按照各种教程所说的 pull 一个镜像, 运行一个容器, 结果才发现, 乐观过头了!

看看我们当前安装的 docker 的版本, 不太高, 现在官方好像出了 1.8 的.

pi@rpi ~/notebooks $ sudo docker version
Client version: 1.3.3
Client API version: 1.15
Go version (client): go1.3.2
Git commit (client): d344625
OS/Arch (client): linux/arm
Server version: 1.3.3
Server API version: 1.15
Go version (server): go1.3.2
Git commit (server): d344625
pi@rpi ~/notebooks $   sudo docker info
Containers: 6
Images: 4
Storage Driver: devicemapper
 Pool Name: docker-179:2-73614-pool
 Pool Blocksize: 65.54 kB
 Data file: /var/lib/docker/devicemapper/devicemapper/data
 Metadata file: /var/lib/docker/devicemapper/devicemapper/metadata
 Data Space Used: 1.208 GB
 Data Space Total: 107.4 GB
 Metadata Space Used: 1.516 MB
 Metadata Space Total: 2.147 GB
 Library Version: 1.02.90 (2014-09-01)
Execution Driver: native-0.2
Kernel Version: 4.1.13-v7+
Operating System: Raspbian GNU/Linux 8 (jessie)
WARNING: No swap limit support
pi@rpi ~/notebooks $

先执行这条命令 sudo docker -d 把守护进程跑起来.

开始尝试下载官方的镜像, 基本上每次 pull 都会报错, 国内那些公司提供的貌似也没一个能正常 pull 下来, 这时看到有家公司专门为树莓派搞了个内置 docker 的发行版, 是这个Getting started with Docker on your Raspberry Pi, 不过我觉得我的 raspbian 运行得好好的, 而且上面装满了各种软件, 仅仅为了尝试下 docker 就全部换掉有些不太划算. 所以这种方案暂时不予考虑.

这时就开始考虑自己创建克用于 docker 的镜像了, 搜索了半天, 发现基本上有两种方法, 如下:

  • 1 用 debootstrap, 不幸的是在我的树莓派2上执行会返回错误
    
  • 2 自己用 `tar` 命令把当前使用的 `raspbian` 系统打包, 然后用第3种办法, 试了一半, 发现打出来的包太大, 中途放弃了
    
  • 3 找一个Linux系统 `ISO`, 使用 `sudo docker import` 命令导入为 `docker image`
    
  • 4 找一个Linux系统 `tar` 包(就好像安装包一样), 使用 `sudo docker import` 命令导入为 `docker image`
    

那么现在就只有第4种办法可行了, 有个网站OpenVZ专门提供这种系统包模板, 在这里下载了几个系统模板回来导入成 docker image, 可以成功导入, 但是没办法执行命令, 猜测可能是因为树莓派2需要特别的ARMV7版本, 而这个网站提供的都是 x86 的版本.

所以还得想办法从其他地方找系统包, 最直接的就是到各个 Linux 官网去下载, 继续尝试.....

在接连失败之后, 终于等来了胜利的曙光: 带有 rootfsArchLinux 不负众望地成功了!

先取得 ArchLinux, 这个版本同时支持树莓派1代和2代:

pi@rpi ~/notebooks $ wget http://212.187.212.74/bt/ab301ea7ea245c12ea9babf5235d75b04890bbd4/data/ArchLinuxARM-2014.10-rpi-rootfs.tar.gz
--2015-12-30 17:02:23--  http://212.187.212.74/bt/ab301ea7ea245c12ea9babf5235d75b04890bbd4/data/ArchLinuxARM-2014.10-rpi-rootfs.tar.gz
Connecting to 212.187.212.74:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 220637379 (210M) [application/x-gzip]
Saving to: `ArchLinuxARM-2014.10-rpi-rootfs.tar.gz'

74% [================================================================================>                             ] 163,600,832 --.-K/s   in 55m 51s

2015-12-30 17:58:15 (47.7 KB/s) - Read error at byte 163600832/220637379 (Connection timed out). Retrying.

--2015-12-30 17:58:16--  (try: 2)  http://212.187.212.74/bt/ab301ea7ea245c12ea9babf5235d75b04890bbd4/data/ArchLinuxARM-2014.10-rpi-rootfs.tar.gz
Connecting to 212.187.212.74:80... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 220637379 (210M), 57036547 (54M) remaining [application/x-gzip]
Saving to: `ArchLinuxARM-2014.10-rpi-rootfs.tar.gz'

100%[+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++============================>] 220,637,379  837K/s   in 79s

2015-12-30 17:59:36 (703 KB/s) - `ArchLinuxARM-2014.10-rpi-rootfs.tar.gz' saved [220637379/220637379]

pi@rpi ~/notebooks $

再生成镜像, 运行 /bin/bash 命令, 相当于创建容器

pi@rpi ~/notebooks $ cat ArchLinuxARM-2014.10-rpi-rootfs.tar.gz |sudo  docker import - archlinux
46b927e18f3bb69337c58ae1e195f34eea7b17d0c71ef5dad63d6cbce102f844
pi@rpi ~/notebooks $ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
archlinux           latest              46b927e18f3b        5 minutes ago       445.6 MB
raring              latest              e0168fc30ed0        About an hour ago   0 B
alpine              3.3.0               0a0aa665bfa2        2 hours ago         85.02 MB
ubuntu              14.04               723d8ba23c53        4 hours ago         204.8 MB
pi@rpi ~/notebooks $ sudo docker run -ti archlinux /bin/bash
[root@d7e0d53e291c /]# ls
bin  boot  dev  etc  home  lib  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@d7e0d53e291c /]#

看看后台的打印信息:

[info] POST /v1.15/containers/create
[254b957f] +job create()
[254b957f] +job log(create, d7e0d53e291c9d8bf149588313439cedc49bf8c00f9c4095cb6fc5ef6ecd2c37, archlinux:latest)
[254b957f] -job log(create, d7e0d53e291c9d8bf149588313439cedc49bf8c00f9c4095cb6fc5ef6ecd2c37, archlinux:latest) = OK (0)
[254b957f] -job create() = OK (0)
[info] POST /v1.15/containers/d7e0d53e291c9d8bf149588313439cedc49bf8c00f9c4095cb6fc5ef6ecd2c37/attach?stderr=1&stdin=1&stdout=1&stream=1
[254b957f] +job container_inspect(d7e0d53e291c9d8bf149588313439cedc49bf8c00f9c4095cb6fc5ef6ecd2c37)
[254b957f] -job container_inspect(d7e0d53e291c9d8bf149588313439cedc49bf8c00f9c4095cb6fc5ef6ecd2c37) = OK (0)
[254b957f] +job attach(d7e0d53e291c9d8bf149588313439cedc49bf8c00f9c4095cb6fc5ef6ecd2c37)
[info] POST /v1.15/containers/d7e0d53e291c9d8bf149588313439cedc49bf8c00f9c4095cb6fc5ef6ecd2c37/start
[254b957f] +job start(d7e0d53e291c9d8bf149588313439cedc49bf8c00f9c4095cb6fc5ef6ecd2c37)
[254b957f] +job allocate_interface(d7e0d53e291c9d8bf149588313439cedc49bf8c00f9c4095cb6fc5ef6ecd2c37)
[254b957f] -job allocate_interface(d7e0d53e291c9d8bf149588313439cedc49bf8c00f9c4095cb6fc5ef6ecd2c37) = OK (0)
[254b957f] +job log(start, d7e0d53e291c9d8bf149588313439cedc49bf8c00f9c4095cb6fc5ef6ecd2c37, archlinux:latest)
[254b957f] -job log(start, d7e0d53e291c9d8bf149588313439cedc49bf8c00f9c4095cb6fc5ef6ecd2c37, archlinux:latest) = OK (0)
[254b957f] -job start(d7e0d53e291c9d8bf149588313439cedc49bf8c00f9c4095cb6fc5ef6ecd2c37) = OK (0)
[info] POST /v1.15/containers/d7e0d53e291c9d8bf149588313439cedc49bf8c00f9c4095cb6fc5ef6ecd2c37/resize?h=47&w=151
[254b957f] +job resize(d7e0d53e291c9d8bf149588313439cedc49bf8c00f9c4095cb6fc5ef6ecd2c37, 47, 151)
[254b957f] -job resize(d7e0d53e291c9d8bf149588313439cedc49bf8c00f9c4095cb6fc5ef6ecd2c37, 47, 151) = OK (0)
[info] GET /v1.15/containers/json?all=1
[254b957f] +job containers()
[254b957f] -job containers() = OK (0)

看看系统磁盘占用情况:

pi@rpi / $ dfc
WARNING: /var/lib/docker/devicemapper was skipped because it cannot be stated : Permission denied
FILESYSTEM     (=) USED      FREE (-) %USED AVAILABLE     TOTAL MOUNTED ON
/dev/root      [=============-------]   64%    10.7G    29.6G /
devtmpfs       [--------------------]    0%    458.7M    458.7M /dev
tmpfs          [=-------------------]    0%    92.3M    92.6M /run
tmpfs          [=-------------------]    0%      5.0M      5.0M /run/lock
tmpfs          [--------------------]    0%    185.2M    185.2M /run/shm
/dev/mmcblk0p1 [========------------]   36%    36.1M    55.9M /boot
tmpfs          [--------------------]    0%    100.0M    100.0M /tmp
cgroup         [--------------------]    0%    12.0K    12.0K /sys/fs/cgroup
cgmfs          [--------------------]    0%    100.0K    100.0K /run/cgmanager/fs
tmpfs          [--------------------]    0%    92.6M    92.6M /run/user/1000
pi@rpi / $ dfc
WARNING: /var/lib/docker/devicemapper was skipped because it cannot be stated : Permission denied
FILESYSTEM     (=) USED      FREE (-) %USED AVAILABLE     TOTAL MOUNTED ON
/dev/root      [============--------]   59%    12.2G    29.6G /
devtmpfs       [--------------------]    0%    458.7M    458.7M /dev
tmpfs          [=-------------------]    0%    92.3M    92.6M /run
tmpfs          [=-------------------]    0%      5.0M      5.0M /run/lock
tmpfs          [--------------------]    0%    185.2M    185.2M /run/shm
/dev/mmcblk0p1 [========------------]   36%    36.1M    55.9M /boot
tmpfs          [--------------------]    0%    100.0M    100.0M /tmp
cgroup         [--------------------]    0%    12.0K    12.0K /sys/fs/cgroup
cgmfs          [--------------------]    0%    100.0K    100.0K /run/cgmanager/fs
tmpfs          [--------------------]    0%    92.6M    92.6M /run/user/1000
pi@rpi / $ dfc
WARNING: /var/lib/docker/devicemapper was skipped because it cannot be stated : Permission denied
WARNING: /var/lib/docker/devicemapper/mnt/d7e0d53e291c9d8bf149588313439cedc49bf8c00f9c4095cb6fc5ef6ecd2c37 was skipped because it cannot be stated : Permission denied
FILESYSTEM     (=) USED      FREE (-) %USED AVAILABLE     TOTAL MOUNTED ON
/dev/root      [=============-------]   61%    11.4G    29.6G /
devtmpfs       [--------------------]    0%    458.7M    458.7M /dev
tmpfs          [=-------------------]    0%    92.3M    92.6M /run
tmpfs          [=-------------------]    0%      5.0M      5.0M /run/lock
tmpfs          [--------------------]    0%    185.2M    185.2M /run/shm
/dev/mmcblk0p1 [========------------]   36%    36.1M    55.9M /boot
tmpfs          [--------------------]    0%    100.0M    100.0M /tmp
cgroup         [--------------------]    0%    12.0K    12.0K /sys/fs/cgroup
cgmfs          [--------------------]    0%    100.0K    100.0K /run/cgmanager/fs
tmpfs          [--------------------]    0%    92.6M    92.6M /run/user/1000
pi@rpi / $

毕竟我们的镜像比较大, 后面慢慢瘦个身.

有了 docker容器, 我们后续就可以在树莓派上试验另一种集群 swarm

另外, 之前 docker 官方对树莓派的支持不太有力, 不过看2015年9月的消息, 感觉以后会对树莓派提供更多支持, 毕竟是个宣传的噱头:

https://blog.docker.com/2015/09/update-raspberry-pi-dockercon-challenge/

September 15, 2015

Update: Raspberry Pi DockerCon Challenge

By Adam Herzog - Posted in DockerCon, Guest blog posts - Tagged with dockercon, DockerCon 2015, DockerCon Europe, hackathon, hacks, hypriot, raspberry pi

In the DockerCon closing keynote Dieter Reuter from Hypriot presented a demo running 500 Docker containers on a Raspberry Pi 2 device. Convinced that this number of containers could be at least doubled, Dieter then challenged the Docker community to beat his personal record. As of now, the current record stands at 2334 web servers running in containers on a single Raspberry Pi 2.

You can see the original demo here:

[youtube:”https://youtu.be/MHJmNZSRve0?t=1m48s”]

转载于:https://my.oschina.net/freeblues/blog/593638

你可能感兴趣的:(在树莓派2上创建自己的 Docker 镜像-不需从 docker.com pull)