ラズパイにDockerをインストールする

# Dockerをインストールする

参考サイト：https://www.raspberrypi.com/news/docker-comes-to-raspberry-pi/

```shell-session
$ curl -sSL https://get.docker.com | sh
# Executing docker install script, commit: 93d2499759296ac1f9c510605fef85052a2c32be
+ sudo -E sh -c apt-get update -qq >/dev/null
+ sudo -E sh -c DEBIAN_FRONTEND=noninteractive 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" | gpg --dearmor --yes -o /usr/share/keyrings/docker-archive-keyring.gpg
+ sudo -E sh -c echo "deb [arch=armhf signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/raspbian bullseye stable" > /etc/apt/sources.list.d/docker.list
+ sudo -E sh -c apt-get update -qq >/dev/null
+ sudo -E sh -c DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends  docker-ce-cli docker-ce >/dev/null
+ version_gte 20.10
+ [ -z  ]
+ return 0
+ sudo -E sh -c DEBIAN_FRONTEND=noninteractive apt-get install -y -qq docker-ce-rootless-extras >/dev/null
+ sudo -E sh -c docker version
Client: Docker Engine - Community
 Version:           20.10.11
 API version:       1.41
 Go version:        go1.16.9
 Git commit:        dea9396
 Built:             Thu Nov 18 00:36:48 2021
 OS/Arch:           linux/arm
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.11
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.9
  Git commit:       847da18
  Built:            Thu Nov 18 00:35:05 2021
  OS/Arch:          linux/arm
  Experimental:     false
 containerd:
  Version:          1.4.12
  GitCommit:        7b11cfaabd73bb80907dd23182b9347b4245eb5d
 runc:
  Version:          1.0.2
  GitCommit:        v1.0.2-0-g52b36a2
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

================================================================================

To run Docker as a non-privileged user, consider setting up the
Docker daemon in rootless mode for your user:

    dockerd-rootless-setuptool.sh install

Visit https://docs.docker.com/go/rootless/ to learn about rootless mode.


To run the Docker daemon as a fully privileged service, but granting non-root
users access, refer to https://docs.docker.com/go/daemon-access/

WARNING: Access to the remote API on a privileged Docker daemon is equivalent
         to root access on the host. Refer to the 'Docker daemon attack surface'
         documentation for details: https://docs.docker.com/go/attack-surface/

================================================================================
```

## Dockerのバージョン確認

```shell-session:
$ docker -v
Docker version 20.10.11, build dea9396
```

## hello-worldのイメージを取得

権限がないと言われてエラーになってしまいました。

```shell-session:
$ docker pull hello-world
Using default tag: latest
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/images/create?fromImage=hello-world&tag=latest": dial unix /var/run/docker.sock: connect: permission denied
```

ユーザー（pi）をdockerグループに追加します。
グループに追加したら、ログインし直します。

```shell-session:
$ sudo usermod -aG docker pi
```

再度、hello-worldをPullしてみます。

```shell-session:
$ docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
9b157615502d: Pull complete 
Digest: sha256:cc15c5b292d8525effc0f89cb299f1804f3a725c8d05e158653a563f15e4f685
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
```

hello-worldが取得できたか確認します。

```shell-session
$ docker images
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
hello-world   latest    1ec996c686eb   7 weeks ago   4.85kB
```

## hello-worldイメージの実行

```shell-session:
$ docker run hello-world

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/
```

## hello-worldイメージのコンテナの確認

起動しているコンテナは``docker ps``で確認できるのですが、hello-worldコンテナはメッセージ出力してしまうので、全てのコンテナを表示するように``-a``オプションをつけて、コンテナの確認をしています。

```shell-session:
$ docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED         STATUS                     PORTS     NAMES
66790f3d780a   hello-world   "/hello"   4 minutes ago   Exited (0) 4 minutes ago             agitated_fermi
```

## hello-worldコンテナの削除

dockerの簡単な動作確認が取れたので、hello-worldコンテナを削除します。
``docker rm``の後に先ほどの``docker ps -a``で確認したコンテナIDを指定して削除します。

```shell-session:
$ docker rm 66790f3d780a
66790f3d780a
pi@raspberrypi:~ $ docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
```

ただし、この状態ではhello-worldのイメージは残ったままになります。

```shell-session:
$ docker images
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
hello-world   latest    1ec996c686eb   7 weeks ago   4.85kB
```

イメージが残ったままなので、``docker run hello-world``を実行すると、コンテナが起動できます。

## hello-worldイメージの削除

イメージを削除するには、``docker rmi``コマンドを実行します。

```shell-session:
$ docker rmi hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:cc15c5b292d8525effc0f89cb299f1804f3a725c8d05e158653a563f15e4f685
Deleted: sha256:1ec996c686eb87d8f091080ec29dd1862b39b5822ddfd8f9a1e2c9288fad89fe
Deleted: sha256:a380e59f1cab56a8fc398216d032afdd9e9da1121f0cf2d812a1a25a989226c3
$ docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
```

hello-worldのイメージが削除されました。


## Docker composeのインストール

ラズパイ+Docker上で、WordPressの環境を構築する予定です。
その場合、WordPressとMariaDBの２つのコンテナを利用するのでDocker composeもインストール

DockerのドキュメントのDocker Compse概要の部分（https://docs.docker.jp/compose/overview.html）には概要して以下のように書いてあります。

> Compose とは、複数のコンテナを定義し実行する Docker アプリケーションのためのツールです。

```shell-session:
$ sudo pip3 install docker-compose
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting docker-compose
  Downloading https://www.piwheels.org/simple/docker-compose/docker_compose-1.29.2-py2.py3-none-any.whl (114 kB)
     |████████████████████████████████| 114 kB 155 kB/s 
Collecting docker[ssh]>=5
  Downloading https://www.piwheels.org/simple/docker/docker-5.0.3-py2.py3-none-any.whl (146 kB)
     |████████████████████████████████| 146 kB 437 kB/s 
Collecting docopt<1,>=0.6.1
  Downloading https://www.piwheels.org/simple/docopt/docopt-0.6.2-py2.py3-none-any.whl (13 kB)
Requirement already satisfied: distro<2,>=1.5.0 in /usr/lib/python3/dist-packages (from docker-compose) (1.5.0)
Collecting python-dotenv<1,>=0.13.0
  Downloading https://www.piwheels.org/simple/python-dotenv/python_dotenv-0.19.2-py2.py3-none-any.whl (17 kB)
Requirement already satisfied: requests<3,>=2.20.0 in /usr/lib/python3/dist-packages (from docker-compose) (2.25.1)
Collecting texttable<2,>=0.9.0
  Downloading https://www.piwheels.org/simple/texttable/texttable-1.6.4-py2.py3-none-any.whl (10 kB)
Collecting PyYAML<6,>=3.10
  Downloading https://www.piwheels.org/simple/pyyaml/PyYAML-5.4.1-cp39-cp39-linux_armv7l.whl (45 kB)
     |████████████████████████████████| 45 kB 1.8 MB/s 
Collecting websocket-client<1,>=0.32.0
  Downloading https://www.piwheels.org/simple/websocket-client/websocket_client-0.59.0-py2.py3-none-any.whl (67 kB)
     |████████████████████████████████| 67 kB 296 kB/s 
Collecting dockerpty<1,>=0.4.1
  Downloading https://www.piwheels.org/simple/dockerpty/dockerpty-0.4.1-py3-none-any.whl (16 kB)
Collecting jsonschema<4,>=2.5.1
  Downloading https://www.piwheels.org/simple/jsonschema/jsonschema-3.2.0-py2.py3-none-any.whl (56 kB)
     |████████████████████████████████| 56 kB 1.6 MB/s 
Collecting paramiko>=2.4.2
  Downloading https://www.piwheels.org/simple/paramiko/paramiko-2.8.0-py2.py3-none-any.whl (208 kB)
     |████████████████████████████████| 208 kB 230 kB/s 
Requirement already satisfied: six>=1.3.0 in /usr/lib/python3/dist-packages (from dockerpty<1,>=0.4.1->docker-compose) (1.16.0)
Requirement already satisfied: setuptools in /usr/lib/python3/dist-packages (from jsonschema<4,>=2.5.1->docker-compose) (52.0.0)
Collecting attrs>=17.4.0
  Downloading https://www.piwheels.org/simple/attrs/attrs-21.2.0-py2.py3-none-any.whl (53 kB)
     |████████████████████████████████| 53 kB 888 kB/s 
Collecting pyrsistent>=0.14.0
  Downloading https://www.piwheels.org/simple/pyrsistent/pyrsistent-0.18.0-cp39-cp39-linux_armv7l.whl (102 kB)
     |████████████████████████████████| 102 kB 147 kB/s 
Collecting bcrypt>=3.1.3
  Downloading https://www.piwheels.org/simple/bcrypt/bcrypt-3.2.0-cp39-cp39-linux_armv7l.whl (53 kB)
     |████████████████████████████████| 53 kB 971 kB/s 
Requirement already satisfied: cryptography>=2.5 in /usr/lib/python3/dist-packages (from paramiko>=2.4.2->docker[ssh]>=5->docker-compose) (3.3.2)
Collecting pynacl>=1.0.1
  Downloading https://www.piwheels.org/simple/pynacl/PyNaCl-1.4.0-cp39-cp39-linux_armv7l.whl (599 kB)
     |████████████████████████████████| 599 kB 452 kB/s 
Collecting cffi>=1.1
  Downloading https://www.piwheels.org/simple/cffi/cffi-1.15.0-cp39-cp39-linux_armv7l.whl (369 kB)
     |████████████████████████████████| 369 kB 156 kB/s 
Collecting pycparser
  Downloading https://www.piwheels.org/simple/pycparser/pycparser-2.21-py2.py3-none-any.whl (119 kB)
     |████████████████████████████████| 119 kB 3.5 MB/s 
Installing collected packages: pycparser, cffi, websocket-client, pynacl, bcrypt, pyrsistent, paramiko, docker, attrs, texttable, PyYAML, python-dotenv, jsonschema, docopt, dockerpty, docker-compose
Successfully installed PyYAML-5.4.1 attrs-21.2.0 bcrypt-3.2.0 cffi-1.15.0 docker-5.0.3 docker-compose-1.29.2 dockerpty-0.4.1 docopt-0.6.2 jsonschema-3.2.0 paramiko-2.8.0 pycparser-2.21 pynacl-1.4.0 pyrsistent-0.18.0 python-dotenv-0.19.2 texttable-1.6.4 websocket-client-0.59.0
```

### 参考

https://www.raspberrypi.com/news/docker-comes-to-raspberry-pi/

https://docs.docker.jp/engine/userguide/containers/dockerizing.html

https://snowsystem.net/container/docker/hello-world/

https://tech.librastudio.co.jp/entry/index.php/2018/07/14/post-1924/

https://blog.hgrs.me/20200602120051