Skip to content

Instantly share code, notes, and snippets.

@buyoh
Last active June 16, 2018 04:28
Show Gist options
  • Save buyoh/07669c2d56c02152fa5723c776565b20 to your computer and use it in GitHub Desktop.
Save buyoh/07669c2d56c02152fa5723c776565b20 to your computer and use it in GitHub Desktop.
dockerに関する実験記録

dockerに関するメモ

実験環境

  • raspberry pi zero WH
  • raspbian umane -r => 4.14.34+
  • 実行時間が遅くてものんびりお茶を嗜むことができる精神

ダメだった環境

  • paiza cloud
  • そもそもpaiza cloudがdockerで動いている
  • dockerの上にdockerをインストールするのは不可能?
  • dockerの上でdockerを動かすことは出来るらしい(docker on docker)

install

  • raspbian dockerで検索

参考資料

やったこと

hello-world

pi@raspberrypi-WH:~ $ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
61ddb93a5f93: Pull complete
Digest: sha256:f5233545e43561214ca4891fd1157e1c3c563316ed8e237750d59bde73361e77
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.
    (arm32v5)
 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/engine/userguide/

bash

pi@raspberrypi-WH:~ $ sudo docker run -it ubuntu /bin/bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
253c7c36e8ff: Pull complete
fe7c768a65c7: Pull complete
ba8d0d2ce80e: Pull complete
28492cd57e46: Pull complete
bdfd257d89dc: Pull complete
Digest: sha256:5f4bdc3467537cbbe563e80db2c3ec95d548a9145d64453b06939c4592d67b6d
Status: Downloaded newer image for ubuntu:latest
pi@raspberrypi-WH:~ $
  • 失敗した?
  • 別のターミナルを開き(新しいSSH接続),docker psする.
pi@raspberrypi-WH:~ $ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
pi@raspberrypi-WH:~ $
  • 動いていない.
  • logは/var/lib/docker/containers/(コンテナID)/(コンテナID)-json.logにあるらしい
pi@raspberrypi-WH:~ $ sudo tail /var/lib/docker/containers/3fd2a8722b5618a462d85
cc80ff0d30b64cadf077a1d8103d1f7e506eced1609/3fd2a8722b5618a462d85cc80ff0d30b64ca
df077a1d8103d1f7e506eced1609-json.log
pi@raspberrypi-WH:~ $
  • うーん raspbian で ubuntu は無理っぽい?
  • 別記事を参照したところ,resin/rpi-raspbian:jessieが書いてあったのでこちらを試す.
pi@raspberrypi-WH:~ $ sudo docker run -it resin/rpi-raspbian:jessie
Unable to find image 'resin/rpi-raspbian:jessie' locally
jessie: Pulling from resin/rpi-raspbian
c501f230acc4: Pull complete
aa071414833d: Pull complete
2481f6e9005f: Pull complete
836f99bda836: Pull complete
832c6340306e: Pull complete
6e3fd44d11a3: Pull complete
bc74cb21f366: Pull complete
e30faf952f39: Pull complete
05e5dd2c9da3: Pull complete
a437ee3aff85: Pull complete
2d0e908a2824: Pull complete
Digest: sha256:78826c381d5aee01fcd36d8c1d2470b22ff92effb817a22ce352ad48e2aed327
Status: Downloaded newer image for resin/rpi-raspbian:jessie
root@1e2462f99c53:/#
  • 入れた.
  • 別ターミナルにて,docker ps
pi@raspberrypi-WH:~ $ sudo docker ps
CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS              PORTS               NAMES
1e2462f99c53        resin/rpi-raspbian:jessie   "/usr/bin/entry.sh /…"   3 minutes ago       Up 3 minutes                            modest_noether
pi@raspberrypi-WH:~ $

コマンド

  • ファイル操作してみる.独立した環境のように見える
root@1e2462f99c53:/# ls
bin   dev  home  media  opt   root  sbin  sys  usr
boot  etc  lib   mnt    proc  run   srv   tmp  var
root@1e2462f99c53:/# cd home
root@1e2462f99c53:/home# ls
root@1e2462f99c53:/home# cd ../
root@1e2462f99c53:/# mkdir foo
root@1e2462f99c53:/# ls
bin   dev  foo   lib    mnt  proc  run   srv  tmp  var
boot  etc  home  media  opt  root  sbin  sys  usr
root@1e2462f99c53:/# touch foo/bar.baz
root@1e2462f99c53:/# ls foo
bar.baz
root@1e2462f99c53:/#
  • 別ターミナルにて.もちろんこちらの環境に変化は無い.
pi@raspberrypi-WH:~ $ cd /
pi@raspberrypi-WH:/ $ ls
bin   boot.bak  etc   lib         media  opt   root  sbin  sys  usr
boot  dev       home  lost+found  mnt    proc  run   srv   tmp  var
pi@raspberrypi-WH:/ $
  • 他のコマンドを打ってみる.aptとか.
root@1e2462f99c53:/# nano
bash: nano: command not found
root@1e2462f99c53:/# sl
bash: sl: command not found
root@1e2462f99c53:/# nano
bash: nano: command not found
root@1e2462f99c53:/# apt install nano
Reading package lists... Done
Building dependency tree... Done
E: Unable to locate package nano
root@1e2462f99c53:/# apt install cowsay
Reading package lists... Done
Building dependency tree... Done
E: Unable to locate package cowsay
root@1e2462f99c53:/# apt-get install nano
Reading package lists... Done
Building dependency tree... Done
E: Unable to locate package nano
  • 5分ぐらい考えて気づきましたが,apt updateしてなかった.
root@1e2462f99c53:/# apt update
Get:1 http://archive.raspbian.org jessie InRelease [14.9 kB]
Get:2 http://archive.raspberrypi.org jessie InRelease [22.9 kB]
Get:3 http://archive.raspbian.org jessie/main armhf Packages [13.3 MB]
Get:4 http://archive.raspberrypi.org jessie/main armhf Packages [171 kB]
Get:5 http://archive.raspbian.org jessie/contrib armhf Packages [49.5 kB]
Get:6 http://archive.raspbian.org jessie/non-free armhf Packages [101 kB]
Get:7 http://archive.raspbian.org jessie/rpi armhf Packages [1297 B]
Get:8 http://archive.raspbian.org jessie/firmware armhf Packages [1208 B]
Fetched 13.7 MB in 2min 20s (97.6 kB/s)
Reading package lists... Done
Building dependency tree... Done
9 packages can be upgraded. Run 'apt list --upgradable' to see them.
root@1e2462f99c53:/# apt-get install nano
^C
root@1e2462f99c53:/# apt install nano
Reading package lists... Done
Building dependency tree... Done
Suggested packages:
  spell
The following NEW packages will be installed:
  nano
0 upgraded, 1 newly installed, 0 to remove and 9 not upgraded.
Need to get 352 kB of archives.
After this operation, 1356 kB of additional disk space will be used.
Get:1 http://archive.raspbian.org/raspbian/ jessie/main nano armhf 2.2.6-3 [352 kB]
Fetched 352 kB in 1s (193 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package nano.
(Reading database ... 8548 files and directories currently installed.)
Preparing to unpack .../nano_2.2.6-3_armhf.deb ...
Unpacking nano (2.2.6-3) ...
Setting up nano (2.2.6-3) ...
update-alternatives: using /bin/nano to provide /usr/bin/editor (editor) in auto mode
update-alternatives: warning: skip creation of /usr/share/man/man1/editor.1.gz because associated file /usr/share/man/man1/nano.1.gz (of link group editor) doesn't exist
update-alternatives: using /bin/nano to provide /usr/bin/pico (pico) in auto mode
update-alternatives: warning: skip creation of /usr/share/man/man1/pico.1.gz because associated file /usr/share/man/man1/nano.1.gz (of link group pico) doesn't exist
root@1e2462f99c53:/#
  • nanoが使えるようになった.
root@1e2462f99c53:/# curl example.com | tail -
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1270  100  1270    0     0   3762      0 --:--:-- --:--:-- --:--:--  3779

<body>
<div>
    <h1>Example Domain</h1>
    <p>This domain is established to be used for illustrative examples in documents. You may use this
    domain in examples without prior coordination or asking for permission.</p>
    <p><a href="http://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>
root@1e2462f99c53:/#
  • ネット接続も出来る.

退出・再入場

  • docker環境を維持したまま元の環境(ホストマシン)に戻るにはctrl+P,ctrl+Qを入力する.
  • もう一度そのコンテナ環境に入るにはdocker attach <CONTAINER ID>する.
  • docker環境に戻っても何も表示されないので,enterを押してやる.
root@1e2462f99c53:/# pi@raspberrypi-WH:~ $
pi@raspberrypi-WH:~ $
pi@raspberrypi-WH:~ $
pi@raspberrypi-WH:~ $
pi@raspberrypi-WH:~ $ sudo docker ps
CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS              PORTS               NAMES
1e2462f99c53        resin/rpi-raspbian:jessie   "/usr/bin/entry.sh /…"   29 minutes ago      Up 29 minutes                           modest_noether
pi@raspberrypi-WH:~ $ sudo docker attach 1e2462f99c53
root@1e2462f99c53:/#
root@1e2462f99c53:/#
root@1e2462f99c53:/#
  • 終了
root@1e2462f99c53:/# exit
exit



pi@raspberrypi-WH:~ $
pi@raspberrypi-WH:~ $
pi@raspberrypi-WH:~ $ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
pi@raspberrypi-WH:~ $
  • どちらかと言えば,サスペンド状態に似ている.終了しただけで消えた訳ではない.再稼働させるには,
pi@raspberrypi-WH:~ $ sudo docker attach 1e2462f99c53
You cannot attach to a stopped container, start it first
pi@raspberrypi-WH:~ $ sudo docker start -i 1e2462f99c53
root@1e2462f99c53:/# ls
bin   dev  foo   lib    mnt  proc  run   srv  tmp  var
boot  etc  home  media  opt  root  sbin  sys  usr
root@1e2462f99c53:/# cat foo/bar.baz
hoge
root@1e2462f99c53:/#
  • 先程nanoで書き込んだhogeが残っている.start -i-iを消すと,入るためにattachする必要が出てくる.

削除

  • 停止状態のコンテナを含む一覧を表示
pi@raspberrypi-WH:~ $ sudo docker ps -a
CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS                           PORTS               NAMES
1e2462f99c53        resin/rpi-raspbian:jessie   "/usr/bin/entry.sh /…"   About an hour ago   Exited (0) 5 minutes ago                             modest_noether
f38b9ec92d07        ubuntu                      "/bin/bash"              About an hour ago   Exited (139) About an hour ago                       zealous_saha
83e54cee3612        ubuntu                      "/bin/bash"              About an hour ago   Exited (139) About an hour ago                       distracted_kepler
7e5a736d6243        ubuntu                      "/bin/bash"              About an hour ago   Exited (139) About an hour ago                       flamboyant_thompson
3fd2a8722b56        ubuntu                      "/bin/bash"              About an hour ago   Exited (139) About an hour ago                       blissful_chandrasekhar
a990c20a7186        ubuntu                      "/bin/bash"              About an hour ago   Exited (139) About an hour ago                       inspiring_euclid
6d2a2d456724        ubuntu                      "/bin/bash"              About an hour ago   Exited (139) About an hour ago                       gracious_meitner
405c1903fda3        ubuntu                      "/bin/bash"              About an hour ago   Exited (139) About an hour ago                       goofy_aryabhata
3aa6a20abd28        ubuntu                      "/bin/bash"              About an hour ago   Exited (139) About an hour ago                       objective_cori
56f759d0b685        hello-world                 "/hello"                 About an hour ago   Exited (0) About an hour ago                         jovial_einstein
  • -qオプションを加えると,IDのみになる
pi@raspberrypi-WH:~ $ sudo docker ps -a -q
1e2462f99c53
f38b9ec92d07
83e54cee3612
7e5a736d6243
3fd2a8722b56
a990c20a7186
6d2a2d456724
405c1903fda3
3aa6a20abd28
56f759d0b685
  • よって,一括削除のコマンドは,
pi@raspberrypi-WH:~ $ sudo docker ps -a -q | xargs cowsay
 ________________________________________
/ 1e2462f99c53 f38b9ec92d07 83e54cee3612 \
| 7e5a736d6243 3fd2a8722b56 a990c20a7186 |
| 6d2a2d456724 405c1903fda3 3aa6a20abd28 |
\ 56f759d0b685                           /
 ----------------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
pi@raspberrypi-WH:~ $ sudo docker ps -a -q | xargs sudo docker rm
1e2462f99c53
f38b9ec92d07
83e54cee3612
7e5a736d6243
3fd2a8722b56
a990c20a7186
6d2a2d456724
405c1903fda3
3aa6a20abd28
56f759d0b685
pi@raspberrypi-WH:~ $ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
  • docker rmi でイメージを削除する.
pi@raspberrypi-WH:/ $ sudo docker rmi ubuntu
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:5f4bdc3467537cbbe563e80db2c3ec95d548a9145d64453b06939c4592d67b6d
Deleted: sha256:1b648fb900a0758211ecf236d0500583ec9e54b84e1555475a44a293c7eb6f10
Deleted: sha256:9b3ac6034fd6378e1d5e16409e09c058580cf5aec9a179c5c2c28b069761cd2a
Deleted: sha256:a7de3b7b78f510337b3512286d41e4faac688e9c072b358849adcc1aa5841f9e
Deleted: sha256:a93ff0c220772449fb6a263db4b9511120ca2506937887807ce92b15de74b706
Deleted: sha256:2f8b98b28aa9242c9d879f93c09178a743dfadcf3af577db7943ba9dcb3d3ab6
Deleted: sha256:446b57d04aa0f55f00c52b8d51c32e5ac0fb3021b04ed8baa5a9391d4c821c37
  • コンテナを削除してもボリュームが残ることがある.
  • docker volume ls -f "dangling=true" -q | xargs docker volume rm で削除出来る.

コンテナイメージ作成(ハンドメイド)

  • docker commit <CONTAINER ID> <IMAGE NAME> でコンテナをイメージ化出来る.
pi@raspberrypi-WH:~ $ sudo docker run -it resin/rpi-raspbian:jessie
root@e4d50d7c7dfe:/# apt update && apt install -y nano ruby gcc g++ python3
...
略
...
root@e4d50d7c7dfe:/# pi@raspberrypi-WH:~ $
pi@raspberrypi-WH:~ $ sudo docker commit e4d50d7c7dfe test/aptget
sha256:367278683599f16abe253a482247cfacd74b818d108905ae8d7a750b4b81bec7
pi@raspberrypi-WH:~ $ sudo docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
test/aptget          latest              367278683599        16 seconds ago      263MB
resin/rpi-raspbian   jessie              f542b3baa5b3        33 hours ago        126MB
resin/rpi-raspbian   latest              f542b3baa5b3        33 hours ago        126MB
hello-world          latest              b42af14d7f0b        2 months ago        1.69kB
pi@raspberrypi-WH:~ $

コンテナイメージ作成(dockerfile)

TODO

コンテナとホストマシン間のデータの同期

  • コンテナは独立した環境なので,コンテナを削除したらデータも消える.データを永続化するには,コンテナ外にデータを移動させる必要がある
  • 特定のディレクトリを同期する機能がある.
  • https://qiita.com/onokatio/items/fcc9f8f94f8533bb030a が詳しい
pi@raspberrypi-WH:~ $ sudo docker run -it -v /box_host:/box_container resin/rpi-
raspbian:jessie
root@df1741566044:/# ls
bin   box_container  etc   lib    mnt  proc  run   srv  tmp  var
boot  dev            home  media  opt  root  sbin  sys  usr
root@df1741566044:/# cd box_container/
root@df1741566044:/box_container# echo hogehoge > hoge.txt
root@df1741566044:/box_container# pi@raspberrypi-WH:~ $
pi@raspberrypi-WH:~ $ ls
work
pi@raspberrypi-WH:~ $ cd /box_host
pi@raspberrypi-WH:/box_host $ la
-bash: la: command not found
pi@raspberrypi-WH:/box_host $ ls
hoge.txt
pi@raspberrypi-WH:/box_host $ cat hoge.txt
hogehoge
pi@raspberrypi-WH:/box_host $
  • ホスト以外にも,隔離された領域(volume)を作成して,そこに退避させる事も出来る.データベースの永続化ならこちらが良さそう.
  • データを渡したいが書き換えられたくない時,ro(readonly)を使う.

dockerのネットワーク設定

  • 一覧表示
pi@raspberrypi-WH:~ $ sudo docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
ed3565e2b4d7        bridge              bridge              local
2a4dab899dfb        host                host                local
4cf74edf0266        none                null                local

外部ネットワーク遮断

  • curl example.comを失敗させたい
  • none に繋げば良い
pi@raspberrypi-WH:~ $ sudo docker run -it --net=none resin/rpi-raspbian:jessie
root@f998dd832ab4:/# curl example.com | tail -
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (6) Could not resolve host: example.com
root@f998dd832ab4:/#

?

telnet

  • guestアカウントを作る.apt install telnet telnetd して,ipv4に固定する.イメージファイルを作る.test/aptgetとした.
    • 記録忘れ.telnetは恐らく要らない.
pi@raspberrypi-WH:~ $ sudo docker run -itd --name sunaba test/aptget
d92da72e3332a47617100e880f08e09ee71a8180c7cba089f678bee3ffe3af00
pi@raspberrypi-WH:~ $ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
d92da72e3332        test/aptget         "/usr/bin/entry.sh /…"   13 seconds ago      Up 6 seconds                            sunaba
pi@raspberrypi-WH:~ $ arp -a
web.setup (192.168.0.1) at 98:f1:99:36:58:00 [ether] on wlan0
? (172.17.0.2) at <incomplete> on docker0
? (192.168.0.3) at b0:99:28:d5:fc:ab [ether] on wlan0
pi@raspberrypi-WH:~ $ telnet 172.17.0.2
Trying 172.17.0.2...
telnet: Unable to connect to remote host: Connection refused
## この時点でtelnet, telnetdインストール済み.
root@86259964be62:/# apt install inetutils-inetd
...略...
root@86259964be62:/# /etc/init.d/inetutils-inetd restart
[ ok ] Restarting internet superserver: inetd.
root@86259964be62:/# read escape sequence
pi@raspberrypi-WH:~ $ telnet 172.17.0.2
Trying 172.17.0.2...
Connected to 172.17.0.2.
Escape character is '^]'.
Raspbian GNU/Linux 8
86259964be62 login: guest
Password:

Login incorrect
86259964be62 login:
Login timed out after 60 seconds.
Connection closed by foreign host.
pi@raspberrypi-WH:~ $
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment