がべーじこれくしょん

技術系とかいろいろ

RaspbianにDockerをインストールする

執筆・創作活動への支援をぜひお願いします🙏
Buy Me A Coffee

TL;DR

  • RaspbianにDocker CEをインストールする。
  • 公式Docは当てにならない。

検証環境

$ cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
NAME="Raspbian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

インストール

以下コマンドを実行することにより、動作環境ごとにDockerのインストールを自動で行ってくれます。

$ curl -sSL https://get.docker.com/ | sh

実行すると、以下のようにインストールスクリプト内で様々なコマンドが実行されている様子を確認できます。

$ curl -sSL https://get.docker.com/ | sh
# Executing docker install script, commit: cfba462
+ sudo -E sh -c apt-get update -qq >/dev/null
+ sudo -E sh -c apt-get install -y -qq apt-transport-https ca-certificates curl >/dev/null
+ sudo -E sh -c curl -fsSL "https://download.docker.com/linux/raspbian/gpg" | apt-key add -qq - >/dev/null
Warning: apt-key output should not be parsed (stdout is not a terminal)
+ sudo -E sh -c echo "deb [arch=armhf] https://download.docker.com/linux/raspbian stretch stable" > /etc/apt/sources.list.d/docker.list
+ sudo -E sh -c apt-get update -qq >/dev/null
+ sudo -E sh -c apt-get install -y -qq --no-install-recommends docker-ce >/dev/null
+ sudo -E sh -c docker version
Client:
 Version:           18.09.0
 API version:       1.39
 Go version:        go1.10.4
 Git commit:        4d60db4
 Built:             Wed Nov  7 00:57:21 2018
 OS/Arch:           linux/arm
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          18.09.0
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.4
  Git commit:       4d60db4
  Built:            Wed Nov  7 00:17:57 2018
  OS/Arch:          linux/arm
  Experimental:     false
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 kmusa

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

WARNING: Adding a user to the "docker" group will grant the ability to run
         containers which can be used to obtain root privileges on the
         docker host.
         Refer to https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface
         for more information.

** DOCKER ENGINE - ENTERPRISE **

If you’re ready for production workloads, Docker Engine - Enterprise also includes:

  * SLA-backed technical support
  * Extended lifecycle maintenance policy for patches and hotfixes
  * Access to certified ecosystem content

** Learn more at https://dockr.ly/engine2 **

ACTIVATE your own engine to Docker Engine - Enterprise using:

  sudo docker engine activate

このコマンドは以下記事にて紹介されていたものです。 https://qiita.com/kohbis/items/53c27ce6c62a7039aaa2#raspberrypi-%E3%81%AB-docker-%E3%82%92%E3%81%88%E3%81%84%E3%82%84

インストールスクリプト自体は公式が用意しているものなので、全く問題ありません。

諸設定

初期設定では、dockerコマンドを使用できるのはrootのみです。そのため、root以外のユーザーがdockerコマンドを使用するためには、対象ユーザーをdockerというグループに追加する必要があります。

以下コマンドを実行することにより、<username>という名前のユーザが、dockerコマンドを使用することができます。

$ sudo usermod -aG docker <username>

また、RasPiを起動した際に、自動的にdockerのデーモンも立ち上がってほしいので、以下コマンドで自動起動設定を行います。

$ sudo systemctl enable docker

以上の設定が完了したら、念の為再起動を行ってください。

$ sudo reboot

動作確認

ちゃんとインストールが完了しているかどうか、動作確認を行ってみましょう。

公式が、hello-worldという動作確認用のイメージを公開しているため、これを使って動作確認を行います。

以下コマンドを実行し、同じような出力が得られれば、動作確認は完了です。お疲れ様でした。

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1eda109e4da: Pull complete
Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
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.
    (arm32v7)
 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/