LoginSignup
1
0

More than 3 years have passed since last update.

Raspberry PiでDockerを使ってみた

Last updated at Posted at 2020-07-30

Raspberry PiでDockerを使ってみた備忘録
以下を参照
https://docs.docker.com/engine/install/debian/

環境

Raspberry Pi 4B(メモリ4GB)
Raspberry Pi OS 10.4

インストール

インストールして、ユーザー権限を付与します。
その後、再起動。

$ curl -sSL https://get.docker.com/ | sh
$ sudo usermod -aG docker pi
$ sudo systemctl isolate reboot.target

コンテナの起動

再起動後、dockerが動作するか確認します。

$ docker -v
Docker version 19.03.12, build 48a6621
$ 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/

dockerの自動起動の設定

$ sudo systemctl enable docker
Synchronizing state of docker.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable docker
$ systemctl is-enabled docker
enabled

次に、以下を実行してubuntuコンテナを起動してみます。

docker run -it ubuntu bash
-iはコンテナの標準入力に接続するオプション
-tはターミナルを割り当てるオプション

コンテナ名を指定する場合は、--nameオプションを使用。
指定しないとランダムな名前が割り当てられるようです。

初回はイメージの取得に時間を要します。

$ docker run -it ubuntu bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
86924fd76ad7: Pull complete
8e967235e6ad: Pull complete
fabf100c8ea1: Pull complete
344cec738d22: Pull complete
Digest: sha256:5d1d5407f353843ecf8b16524bc5565aa332e9e6a1297c73a92d3e754b8a636d
Status: Downloaded newer image for ubuntu:latest
root@6426a92ad57c:/# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0   2716  2148 pts/0    Ss    2020   0:00 bash
root        11  0.0  0.0   4376  1744 pts/0    R+    2020   0:00 ps aux

コンテナを終了するときは、コンテナ内でexitを実行します。

root@6426a92ad57c:/# exit
exit
pi@raspberrypi:~ $

取得したイメージは、dockerのimagesコマンドを使用して確認できます。

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              014028cfedab        5 days ago          50.7MB
hello-world         latest              851163c78e4a        6 months ago        4.85kB

2回目以降の起動時は高速

$ docker run -it ubuntu bash
root@0cb3b5aab220:/#

コンテナを終了せずに抜ける時は、CTRL+P CTRL+Qを押下。

root@0cb3b5aab220:/# pi@raspberrypi:~ $

再度接続する時は、dockerのattachコマンドを使用。
今回はコンテナIDを指定して接続。
コンテナIDは一部の指定だけでも良いようです。

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
0cb3b5aab220        ubuntu              "bash"              About a minute ago   Up About a minute                       festive_lamarr
$ docker attach 0cb3
root@0cb3b5aab220:/#                   

コンテナの再開

再開させるには、dockerのstartコマンドを使用。
終了させるには、dockerのstopコマンドを使用。

root@0cb3b5aab220:/# exit
exit
$ docker ps -a
CONTAINER ID        IMAGE                COMMAND               CREATED             STATUS                      PORTS               NAMES
0cb3b5aab220        ubuntu               "bash"                18 minutes ago      Exited (0) 10 seconds ago                       festive_lamarr
$ docker start 0cb3
0cb3
$ docker ps -a
CONTAINER ID        IMAGE                COMMAND               CREATED             STATUS                      PORTS               NAMES
0cb3b5aab220        ubuntu               "bash"                23 minutes ago      Up 11 seconds                                   festive_lamarr
$ docker stop 0cb3
0cb3
$ docker ps -a
CONTAINER ID        IMAGE                COMMAND               CREATED             STATUS                      PORTS               NAMES
0cb3b5aab220        ubuntu               "bash"                24 minutes ago      Exited (0) 2 seconds ago                        festive_lamarr
1
0
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
1
0