LoginSignup
118
109

More than 5 years have passed since last update.

Docker on Raspberry PiのインストールとLチカ

Last updated at Posted at 2016-09-04

はじめに

DockerがRaspberry Pi(Raspbian Jessie)をサポートしたみたいです。
参考:Git - docker/docker - Add support to install Docker on raspbian/jessie #24815

Dockerを使ってLチカをやってみましたので、その手順をまとめておきます。
参考:Get Started with Docker 1.12 on Raspberry Pi

環境

Raspberry Pi 3 MODEL B
Mac(Raspberry Piの初期設定で使用)

手順

Raspbianのインストール

まず、SDカードにインストールするためのイメージを準備します。
今回は、Raspbian Jessieを使いますので、ここから最新版のイメージをダウンロードします。
※ 記載時点の最新版は、2016-05-27-raspbian-jessie.zipでした。

ダウンロードしたRaspbian Jessieのzipファイルを解凍します。

$ tar zxvf 2016-05-27-raspbian-jessie.zip
x 2016-05-27-raspbian-jessie.img

ここからは、SDカードにイメージをインストールしていきます。
まず、イメージをインストールするSDカードを確認します。

$ df -ah
Filesystem      Size   Used  Avail Capacity   iused    ifree %iused  Mounted on
/dev/disk1s1    30Gi  2.5Mi   30Gi     1%         0        0  100%   /Volumes/UNTITLED

SDカードをFAT32でフォーマットします。

$ diskutil eraseDisk FAT32 RPI /dev/disk1
Started erase on disk1
Unmounting disk
Creating the partition map
Waiting for the disks to reappear
Formatting disk1s2 as MS-DOS (FAT32) with name RPI
512 bytes per physical sector
/dev/rdisk1s2: 62501024 sectors in 1953157 FAT32 clusters (16384 bytes/cluster)
bps=512 spc=32 res=32 nft=2 mid=0xf8 spt=32 hds=255 hid=411648 drv=0x80 bsec=62531584 bspf=15260 rdcl=2 infs=1 bkbs=6
Mounting disk
Finished erase on disk1

SDカードをアンマウントします。
※ SDカードはまだ抜かないで下さい!

$ diskutil unmountDisk /dev/disk1
Unmount of all volumes on disk1 was successful

先ほど準備しておいたイメージをSDカードに書き込みます。

$ sudo dd bs=1024m if=2016-05-27-raspbian-jessie.img of=/dev/rdisk1
Password:
3+1 records in
3+1 records out
4019191808 bytes transferred in 440.337320 secs (9127529 bytes/sec)

これでSDカードの準備は完了です!

Raspbianの接続と初期設定

先ほど準備したSDカードをRaspberry Piに挿して電源をONします。
Raspberry Piが起動後、SSHで接続します。
Raspberry PiのIPアドレスは、PINGやDHCPログなどから確認したものを入力します。

$ ssh pi@<Raspberry PiのIPアドレス>

初期設定として固定IPを設定します。
※ Dockerにはまったく関係ないです

pi@raspberrypi:~ $ sudo vi /etc/dhcpcd.conf
interface eth0
static ip_address=192.168.0.11/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1

Dockerのインストール

本題です!
Raspberry PiにDockerをインストールします。

pi@raspberrypi:~ $ curl -sSL https://get.docker.com/ | sh
+ sudo -E sh -c mkdir -p /etc/systemd/system/docker.service.d
+ sudo -E sh -c echo '[Service]\nExecStart=\nExecStart=/usr/bin/dockerd --storage-driver overlay -H fd://' > /etc/systemd/system/docker.service.d/overlay.conf
+ sudo -E sh -c sleep 3; apt-get update
Get:1 http://archive.raspberrypi.org jessie
(省略)
+ sudo -E sh -c docker version
Client:
 Version:      1.12.1
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   23cf638
 Built:        Thu Aug 18 05:31:15 2016
 OS/Arch:      linux/arm

Server:
 Version:      1.12.1
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   23cf638
 Built:        Thu Aug 18 05:31:15 2016
 OS/Arch:      linux/arm

If you would like to use Docker as a non-root user, you should now consider
adding your user to the "docker" group with something like:

  sudo usermod -aG docker pi

Remember that you will have to log out and back in for this to take effect!

Dockerがインストールできました。
バージョンは1.12.1です。

ためしにraspbian:jessieを起動してみます。

pi@raspberrypi:~ $ sudo docker run -ti resin/rpi-raspbian:jessie /bin/bash
Unable to find image 'resin/rpi-raspbian:jessie' locally
jessie: Pulling from resin/rpi-raspbian
2c21a521f21e: Pull complete 
d99cff50dc11: Pull complete 
c1c51ee47f0b: Pull complete 
e8b3c50e8e7e: Pull complete 
a3ed95caeb02: Pull complete 
7f56c2fb9a9c: Pull complete 
75010306b0df: Pull complete 
8837443d430d: Pull complete 
23dd9b04a2ec: Pull complete 
Digest: sha256:b6aef386a6da25bfbcba0e14a748e5473ff798233155fad960ae08fa4c94b146
Status: Downloaded newer image for resin/rpi-raspbian:jessie
root@f94112ff9ddc:/# 

起動しました!

DockerコンテナからLチカ

Raspberry Piということで、コンテナからLチカしてみます。

ハードウェア

Raspberry PiとLEDを接続します。
回路図の通りRaspberry PiにLEDをつなぎました。

Screen-Shot-2016-08-20-at-09-40-46-1.png
引用:Get Started with Docker 1.12 on Raspberry Pi

ソフトウェア

コンテナとプログラムを準備します。
まず、コンテナですが、Lチカでは、/dev/memにアクセスしますので、コンテナの起動方法として2つあるのかなと思います。

  • privilegedオプションを付けてコンテナを起動する → こちらを記載
  • deviceオプションで/dev/memを指定してコンテナを起動する

特にデバイスへのアクセスを制限する必要もありませんので、privilegedオプションを付けてコンテナを起動します。

pi@raspberrypi:~ $ sudo docker run -ti --privileged resin/rpi-raspbian:jessie /bin/bash
root@3bfcfa574e1e:/# 

今回は、pythonのrpi.gpioを使ってLチカします。
必要なパッケージとライブラリをインストールします。

root@3bfcfa574e1e:/# apt-get update
root@3bfcfa574e1e:/# apt-get install python python-pip python-dev gcc make vim
root@3bfcfa574e1e:/# pip install rpi.gpio

LEDを点滅させるプログラムを作成します。

chikachika.py
import RPi.GPIO as GPIO  
import time  
GPIO.setmode(GPIO.BCM)  
led_pin = 17  
GPIO.setup(led_pin, GPIO.OUT)

while(True):  
    GPIO.output(led_pin, GPIO.HIGH)
    time.sleep(1)
    GPIO.output(led_pin, GPIO.LOW)
    time.sleep(1)

チカチカ

プログラムを実行します。

root@3bfcfa574e1e:/# python chikachika.py

チカチカしました。

おわりに

Dockerでは、キレイな環境をクイックに準備できるので、Raspberry Piと親和性が高そうな気がしました。

118
109
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
118
109