Alpine Linux 上で Docker を動かす

Dokcer のイメージとして ALpine Linux を使うことが多いが、ホスト OS として Alpine Linux を使い Docker コンテナを起動したことがなかったので試してみた。

Alpine Linux をインストールする

Alpine Linux の公式サイトからISOをダウンロードし、VirtualBox で起動した。

ログインプロンプトが表示されたら、root と入力し、Enter キーを押下する。

1
localhost login: root

Alpine Linux の基本設定

ネットワーク設定

とりあえず、eth0 を DHCP で IP アドレスを取得するように設定する。
設定後はネットワークを再起動する。

1
2
3
4
$ vi /etc/network/interfaces
auto eth0
iface eth0 inet dhcp
# rc-service networking restart

SSH 設定

まず、apk のリポジトリを追加する。

1
2
3
# vi /etc/apk/repositories
http://dl-cdn.alpinelinux.org/alpine/v3.12/main
http://dl-cdn.alpinelinux.org/alpine/v3.12/community

追加したリポジトリを使って openssh をインストールする。

1
2
# apk update
# apk add openssh

セキュリティ的は問題があるが、Alpine Linux の現在の設定で SSH を使用するために、

  1. root でログインできるようにする
  2. パスワードなしでログインできるようにする

2点を設定変更しておく。

1
2
3
# vi /etc/ssh/sshd_config
PermitRootLogin yes
PermitEmptyPasswords yes

設定を反映するために、sshd を再起動する。

1
2
3
# rc-service sshd restart
* Stopping sshd ... [ ok ]
* Starting sshd ... [ ok ]

Docker をインストールする

community リポジトリが有効になっていることを確認する。

1
2
3
# cat /etc/apk/repositories
http://dl-cdn.alpinelinux.org/alpine/v3.12/main
http://dl-cdn.alpinelinux.org/alpine/v3.12/community

リポジトリを更新する。

1
2
3
4
5
# apk update
fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/community/x86_64/APKINDEX.tar.gz
v3.12.0-185-gb4b2a5e482 [http://dl-cdn.alpinelinux.org/alpine/v3.12/main]
v3.12.0-186-g2cd07a16f7 [http://dl-cdn.alpinelinux.org/alpine/v3.12/community]

Docker をインストールする。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# apk add docker
(1/14) Installing ca-certificates (20191127-r4)
(2/14) Installing libseccomp (2.4.3-r0)
(3/14) Installing runc (1.0.0_rc10-r1)
(4/14) Installing containerd (1.3.4-r1)
(5/14) Installing libmnl (1.0.4-r0)
(6/14) Installing libnftnl-libs (1.1.6-r0)
(7/14) Installing iptables (1.8.4-r1)
(8/14) Installing iptables-openrc (1.8.4-r1)
(9/14) Installing tini-static (0.19.0-r0)
(10/14) Installing device-mapper-libs (2.02.186-r1)
(11/14) Installing docker-engine (19.03.12-r0)
(12/14) Installing docker-openrc (19.03.12-r0)
(13/14) Installing docker-cli (19.03.12-r0)
(14/14) Installing docker (19.03.12-r0)
Executing docker-19.03.12-r0.pre-install
Executing busybox-1.29.3-r10.trigger
Executing ca-certificates-20191127-r4.trigger
OK: 316 MiB in 43 packages

Docker をサービスとして登録する。

1
2
# rc-update add docker boot
* service docker added to runlevel boot

Docker を起動するとエラーになる。

1
2
3
4
5
6
7
8
9
10
# service docker start
* Caching service dependencies ... [ ok ]
* Mounting cgroup filesystem ... [ ok ]
ulimit: unrecognized option: u
* /var/log/docker.log: creating file
* /var/log/docker.log: correcting owner
* Starting Docker Daemon ...
* supervise-daemon: --pidfile must be specified
* failed to start Docker Daemon [ !! ]
* ERROR: docker failed to start

pidファイル名を指定しろ、と言われているようなので、/etc/init.d/docker に1行追加する。

1
pidfile="/var/run/your-service-name.pid"

再度、Docker を起動する。

1
2
3
4
# service docker start
* Caching service dependencies ... [ ok ]
ulimit: unrecognized option: u
* Starting Docker Daemon ... [ ok ]

起動に成功したので、hello-world を試してみる。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: 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.
(amd64)
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/