ようへいの日々精進XP

よかろうもん

(ショロカレ 9 日目)Docker コンテナ内でちょっとしたアプリを立ち上げるのに Supervisord を使う

だいぶん体調が戻ってきたのでショロカレを続ける...ということで、これは「初老丸の独り Advent calendar 2015」の九日目の記事です。

tl;dr

Docker コンテナ内でちょっとしたアプリ(スクリプト)を立ちあげたかったので、以下の記事を参考に Supervisord を使ったのでメモ。

docs.docker.com


memo

作ったもの

github.com

S3 の Event 通知が SQS に入るように設定しているので、その通知を解析して S3 に保存されているログを取得して Elasticsearch にポストするスクリプト。詳細については別の記事で。

Dockerfile

$ cat Dockerfile
FROM python:2
MAINTAINER inokappa
RUN mkdir /app
ADD requirements.txt /app
ADD app.py /app
ADD supervisord.conf /app
RUN pip install -r /app/requirements.txt
CMD [ "/usr/local/bin/supervisord", "-c", "/app/supervisord.conf" ]

supervisord.conf

以下を参考に...

以下のよに作成。

$ cat supervisord.conf
[inet_http_server]
port=127.0.0.1:9001

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=http://127.0.0.1:9001

[supervisord]
nodaemon=true

[program:app]
command=/usr/local/bin/python /app/app.py
stdout_logfile=/app/app.log
stdout_logfile_maxbytes=1MB
stdout_logfile_backups=5
stdout_capture_maxbytes=1MB
redirect_stderr=true

requirements.txt

必要なモジュールは requirements.txt に書いておくと良いことを最近知った。

$ cat requirements.txt
boto3
elasticsearch
pytz
urllib3
supervisor

docker run すると...

docker run にオプションをたくさんくっつける時には Makefile にまとめておくのが最近の個人的なトレンド。

$ make run

でコンテナが起動する(ようにしている)。

$ docker ps
CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS              PORTS                    NAMES
ecf226637831        s3-access-log        "/usr/local/bin/super"   4 hours ago         Up 4 hours                                   s3-access-log

supervisorctl で確認する。

$ docker exec s3-access-log /usr/local/bin/supervisorctl -c /app/supervisord.conf status
app                              RUNNING   pid 8, uptime 0:02:01

標準出力の内容を確認する。

$ docker exec s3-access-log /usr/local/bin/supervisorctl -c /app/supervisord.conf tail app
-12-10 20:41:19,290 INFO Event does not exists...
2015-12-10 20:41:19,291 INFO Start polling...
2015-12-10 20:42:19,347 INFO Calling sqs:get_queue_url with {'QueueName': 's3-notification'}
2015-12-10 20:42:19,460 INFO Calling sqs:receive_message with {u'QueueUrl': 'https://ap-northeast-1.queue.amazonaws.com/xxxxxxxxxxxxx/s3-notification', 'MessageAttributeNames': ['*']}
2015-12-10 20:42:19,542 INFO Event does not exists...
2015-12-10 20:42:19,542 INFO Start polling...
2015-12-10 20:43:19,595 INFO Calling sqs:get_queue_url with {'QueueName': 's3-notification'}
2015-12-10 20:43:19,637 INFO Calling sqs:receive_message with {u'QueueUrl': 'https://ap-northeast-1.queue.amazonaws.com/xxxxxxxxxxxxx/s3-notification', 'MessageAttributeNames': ['*']}
2015-12-10 20:43:19,718 INFO Event does not exists...
2015-12-10 20:43:19,718 INFO Start polling...
2015-12-10 20:44:19,780 INFO Calling sqs:get_queue_url with {'QueueName': 's3-notification'}
2015-12-10 20:44:19,824 INFO Calling sqs:receive_message with {u'QueueUrl': 'https://ap-northeast-1.queue.amazonaws.com/xxxxxxxxxxxxx/s3-notification', 'MessageAttributeNames': ['*']}
2015-12-10 20:44:19,905 INFO Event does not exists...
2015-12-10 20:44:19,905 INFO Start polling...
2015-12-10 20:45:19,966 INFO Calling sqs:get_queue_url with {'QueueName': 's3-notification'}
2015-12-10 20:45:20,000 INFO Calling sqs:receive_message with {u'QueueUrl': 'https://ap-northeast-1.queue.amazonaws.com/xxxxxxxxxxxxx/s3-notification', 'MessageAttributeNames': ['*']}
2015-12-10 20:45:20,084 INFO Event does not exists...
2015-12-10 20:45:20,084 INFO Start polling...

おお、いい感じ。


ということで

Supervisord を引き続き、便利に使っていきたい。