3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Honchoを使ってマルチプロセスのDockerコンテナを作る

Last updated at Posted at 2016-07-16

Dockerの公式ドキュメントにはSupervisorを使うマルチプロセスの例が紹介されていますが、最近「Honchoも良いよ」という内容のポスト(例えばこちら)をいくつか見かけたので試してみました。

僕のポストでは、ApacheとMonitをHonchoで起動して、Apacheのリソース消費をMonitがログに残してくれる、というシンプルな例を紹介します。

必要な設定ファイルを準備する

下記の通りDockerfileProcfilemonitrcの3つのファイルを準備します。

Dockerfile

FROM ubuntu:16.04
MAINTAINER examples@docker.com

RUN apt-get update && \
    apt-get install -y apache2 monit python-pip && \
    apt-get clean && pip install honcho && \
    mkdir -p /var/lock/apache2 /var/run/apache2

COPY Procfile /Procfile
COPY monitrc /etc/monit/monitrc
RUN chown root:root /etc/monit/monitrc && chmod 700 /etc/monit/monitrc

EXPOSE 80
CMD ["/usr/local/bin/honcho", "start"]

Procfile

apache2: /bin/bash -c "source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND"
monit: /usr/bin/monit -vI

monitrc

set daemon 10             # check services at 10-second intervals
set logfile /var/log/monit.log
set idfile /var/lib/monit/id
set statefile /var/lib/monit/state
set eventqueue
    basedir /var/lib/monit/events # set the base directory where events will be stored
    slots 100                     # optionally limit the queue size

check process apache with pidfile /var/run/apache2/apache2.pid
    if cpu > 90% for 2 cycles then alert
    if memory > 50 MB then alert

イメージをビルドしてコンテナを起動する

docker build -t honcho .
docker run --rm -p 80:80 -it honcho

デモ用なのでMonitをverboseモードで起動して、監視のインターバルも10秒と短めに設定してあります。うまく起動していれば下記のように10秒毎にCPUとメモリの消費量が表示されます。

 :
 :
 :
00:14:23 monit.1   | -------------------------------------------------------------------------------
00:14:23 monit.1   | pidfile '/run/monit.pid' does not exist
00:14:23 monit.1   | Starting Monit 5.16 daemon
00:14:23 monit.1   | 'bfa699f1b07f' Monit 5.16 started
00:14:23 monit.1   | Processing postponed events queue
00:14:23 monit.1   | 'apache' process is running with pid 12
00:14:23 monit.1   | 'apache' zombie check succeeded
00:14:23 monit.1   | 'apache' mem amount check succeeded [current mem amount=4.8 MB]
00:14:23 monit.1   | 'apache' cpu usage check succeeded [current cpu usage=0.0%]

Honchoって名前が良いですね。

3
3
1

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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?