LoginSignup
68

More than 3 years have passed since last update.

Raspberry Pi4をDocker環境にするため、AlpineLinuxをインストール

Last updated at Posted at 2020-08-10

はじめに

RaspberryPi4(4GB)にDockerホストOSをインストールし、コンテナを実行できる環境を作成する。
Dockerを実行するなら、ベースOSは軽量でないとと考え、Alpine Linuxを選んだ。

作成環境

  • software
    Windows10 Home 1909
    Raspberry Pi Imager1.4

対象環境

  • Hardware
    RaspberrPi 4GB
  • software
    OS:Alpine Linux 3.12.0 aarch64

OSインストール

事前準備

Alpine Linuxのチュートリアルに従い、インストールを行う。
https://wiki.alpinelinux.org/wiki/Raspberry_Pi

OSイメージは以下のリンクから、「RASPBERRY PIのaarch64」をダウンロード
https://alpinelinux.org/downloads/

0.フォーマット

balenaEtcherでImageを書き込もうとしたけど出来なかったため、おとなしくチュートリアルに従い、SDのフォーマットを実施。
特別なツールは使わずに、Windows標準のフォーマットで実施。ファイルシステムは「FAT32」。

ラズベリーパイ財団が出している「Raspberry Pi Imager」を使ってみることに。
※Raspberry Pi Imagerは対応しているラズパイ用OSを選択したらインストールできるという優れもの
 選択肢になくても、ローカル上のイメージから選ぶこともできるため、今回はローカル上のイメージからインストールを実施した。
 https://www.raspberrypi.org/downloads/

Raspberry Pi ImagerからOperating Systemを「ERASE」にし、フォーマットを実行。(もちろんファイルシステムはFAT32となる)

1.インストール

Raspberry Pi ImagerからOperating Systemを「Use custom」にし、ダウンロードしてきた「alpine-rpi-3.12.0-aarch64.tar.gz」を選択し、書き込みを実行。
Raspberry Pi Imagerでは書き込みできなかったため、.tar.gzファイルを素直に7-zipで解凍しD&DでSDに書き込み。

2.起動

microSDをRaspberry Pi4に差し込み無事起動を確認。

3.初期設定

起動したら、"root"でログイン、PWは不要

初期設定のため、以下のコマンドを実行。

$ setup-alpine

キーボードとタイムゾーンは設定するが、他は基本的にデフォルト設定としたため、何も入力せずにEnter押下。

WiFi設定もできたため、SSID、PWを入力。
IPは個別にまだ振っていないため、DHCPとした。

設定項目の詳細は参考リンクを参照。

設定が終わったら、以下コマンドで設定を保存し再起動。

$ lbu commit -d

(4.ディスクレスモードからsysモードへ切り替え)

※この作業はお好みで。ディスクレスモードのほうが、ストレージへの負担が少なくメリットが多いかも。
以下の「Installation」「Post-installation」を実行してくだけ。
https://wiki.alpinelinux.org/wiki/Classic_install_or_sys_mode_on_Raspberry_Pi
上記の設定の中で、追加したUserでSSHログインも可能になる。

結局sysモードは辞めて、ディスクレスモードで実行してます。

4.初期設定後のもろもろ設定

「4.ディスクレスモードからsysモードへ切り替え」をしなかった場合に設定する内容。

4.1.Userの追加

$ apk add sudo
$ adduser foo
$ adduser foo wheel
$ visudo #82行目"wheel ALL=(ALL) ALL"のコメントアウトを外す。 保存はEsc :x

以後の設定は追加したUserからSSHで操作可能になる。
(root権限ログインを有効にしたらできると思うけど、こっちのが方がおそらく簡単)

4.2.WiFi自動起動

rc-update add wpa_supplicant boot
/etc/init.d/wpa_supplicant start

4.3.固定IPアドレス化

"setup-alpine","setup-interfaces"でDHCPでなくIP設定をすると、うまくネットワークにつながらなかった。
"setup-interfaces"でDHCPでいいので接続した後に、interfacesで設定を行う。

/etc/network/interfaces
auto wlan0
iface wlan0 inet static
        hostname alpine
        address 192.168.0.199 #←設定するIPアドレス
        netmask 255.255.255.0
        gateway 192.168.0.1

Dockerインストール

以下でdockerのリポジトリが「community」であることを確認。
https://pkgs.alpinelinux.org/packages?name=docker&branch=edge&arch=aarch64

・communityを追加

/etc/apk/repositories
http://dl-cdn.alpinelinux.org/alpine/v3.12/main
#http://dl-cdn.alpinelinux.org/alpine/v3.12/community
#http://dl-cdn.alpinelinux.org/alpine/edge/main
http://dl-cdn.alpinelinux.org/alpine/edge/community #<-コメントアウトを外す
#http://dl-cdn.alpinelinux.org/alpine/edge/testing

・インストール

$ apk update
$ apk add docker

・Serviceの登録

$ rc-update add docker boot
$ service docker start

・Docker-composeのインストール

$ apk add docker-compose
$ apk add py-pip python3-dev libffi-dev openssl-dev gcc libc-dev make 
$ pip3 install docker-compose

忘れずにコミット

$ lbu commit -d

Docker起動確認

定番の「hello-world」を実行し確認する。

$ docker run hello-world

以下が出力されればOK。
imageをpullし実行ができている。

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
256ab8fe8778: Pull complete
Digest: sha256:49a1c8800c94df04e9658809b006fd8a686cab8028d33cfba2cc049724254202
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/

さいごに

今回、初めてAlipneLinuxを触ってみましたが、導入方法などについてはwiki(参考リンク)が充実していて、だいぶ助かりました。

apkコマンドに慣れておらずaptと入力しエラーがでたり、コミットを忘れてリブートして設定が保存されてなかったり、トラブルは多々ありましたが、これからこういったルーティンには慣れていきます。

参考リンク

AlipneLinux設定項目
https://wiki.alpinelinux.org/wiki/Installation
AlpineLinux Dockerセットアップ
https://wiki.alpinelinux.org/wiki/Docker

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
68