Docker

2019/12/13更新

対応バージョン: Docker 18.09.7

Dockerで使用するUbuntuイメージ(18.04)には最低限のパッケージしか同梱されていないので、syslogを使いたい場合は以下の手順で別途インストールする。

Dockerfileとrsyslogデーモンを起動するスクリプトを用意

Dockerfile
FROM ubuntu:18.04

RUN apt-get update && \
  apt-get install -y rsyslog

COPY startup.sh /startup.sh
RUN chmod 744 /startup.sh

CMD ["/startup.sh"]
startup.sh
#!/usr/bin/env bash

service rsyslog start

Dockerイメージビルド & 実行

ビルド
$ docker image build -t test .
実行
$ docker run --privileged test

--privilegedはコンテナ内の全てのデバイスへのアクセスを可能にするオプションであるが、これを付けないとdocker run時に以下の権限エラーになり、rsyslogデーモンが起動できない。

rsyslogd: imklog: cannot open kernel log (/proc/kmsg): Operation not permitted.
rsyslogd: activation of module imklog failed [v8.32.0 try http://www.rsyslog.com/e/2145 ]

参考サイト

2018/07/10更新

対応バージョン: 17.12.1-ce

DockerでAlpine Linuxイメージ上にnginxを導入する手順を示す。

Alpine Linuxイメージ入手

% docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
8e3ba11ec2a2: Pull complete
Digest: sha256:7043076348bf5040220df6ad703798fd8593a0918d06d3ce30c6c93be117e430
Status: Downloaded newer image for alpine:latest

% docker images
REPOSITORY      TAG             IMAGE ID        CREATED         SIZE
alpine          latest          11cd0b38bc3c    3 days ago      4.41MB

デーモンとして起動

ここではローカルの8080ポートをDockerコンテナの80ポートに接続する。

% docker run -itd -p 8080:80 11cd0b38bc3c
692b51724e157c4bc85fa87dd8df9393fbecc8c22e192b63e8b52fa7ef8b87cb

事前準備(update & rc-status導入)

% docker ps -a
CONTAINER ID    IMAGE         COMMAND     CREATED           STATUS          PORTS                  NAMES
58ff8b32b34a    11cd0b38bc3c  "/bin/sh"   56 seconds ago    Up 54 seconds   0.0.0.0:8080->80/tcp   youthful_joliot

% docker exec -it 58ff8b32b34a sh

# apk update
fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/community/x86_64/APKINDEX.tar.gz
v3.8.0-7-gae564a61dc [http://dl-cdn.alpinelinux.org/alpine/v3.8/main]
v3.8.0-4-gc5ede23c7e [http://dl-cdn.alpinelinux.org/alpine/v3.8/community]
OK: 9532 distinct packages available

# apk add openrc
(1/1) Installing openrc (0.35.5-r4)
Executing openrc-0.35.5-r4.post-install
Executing busybox-1.28.4-r0.trigger
OK: 8 MiB in 16 packages

nginx導入

# apk add nginx
(1/2) Installing pcre (8.42-r0)
(2/2) Installing nginx (1.14.0-r0)
Executing nginx-1.14.0-r0.pre-install
Executing busybox-1.28.4-r0.trigger
OK: 6 MiB in 15 packages

パッケージをインストールしただけではnginxは動作しないので、以下いくつかのファイルを変更する。

/etc/rc.conf
# sed -i'.bak' 's/^#rc_sys=""/rc_sys="lxc"/' /etc/rc.conf
# sed -i 's/^#rc_provide="!net"/rc_provide="loopback net"/' /etc/rc.conf
/etc/inittab
# sed -i'.bak' '/getty/d' /etc/inittab
/lib/rc/sh/init.sh
# sed -i'.bak' 's/mount -t tmpfs/# mount -t tmpfs/' /lib/rc/sh/init.sh
/etc/init.d/hostname
# sed -i'.bak' 's/hostname $opts/# hostname $opts/' /etc/init.d/hostname
/lib/rc/sh/openrc-run.sh
# sed -i'.bak' 's/cgroup_add_service$/# cgroup_add_service/' /lib/rc/sh/openrc-run.sh
openrc用のファイルを用意
# mkdir /run/openrc
# touch /run/openrc/softlevel

最後にnginxの設定ファイルを変更し、/にアクセスした際にindex.htmlを返すようにする。

# sed -i'.bak' 's/return 404;/root html;\n\t\tindex index.html;/' /etc/nginx/conf.d/default.conf

設定変更が終わったらnginxを起動する。

# rc-status
 * Caching service dependencies ...                      [ ok ]
Runlevel: sysinit
Dynamic Runlevel: hotplugged
Dynamic Runlevel: needed/wanted
Dynamic Runlevel: manual

# rc-service nginx checkconfig
 * Checking nginx configuration ...
 * /run/nginx: creating directory
 * /run/nginx: correcting owner                          [ ok ]

# rc-service nginx start
 * Starting nginx ...                                    [ ok ]

# ps -ef | grep nginx
  207 root   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
  208 nginx  0:00 nginx: worker process
  209 nginx  0:00 nginx: worker process
  210 nginx  0:00 nginx: worker process
  211 nginx  0:00 nginx: worker process

nginxが起動したらブラウザでlocalhost:8080にアクセスして以下のページが表示されればよい。

Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working.
Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.