LoginSignup
2
1

More than 3 years have passed since last update.

Dockerでsupervisor (docker execでsupervisorctl利用、docker logsでプロセスログ確認、Webインターフェース機能でブラウザ表示)

Last updated at Posted at 2020-11-23

はじめに

Dockerでsupervisorを起動し以下の項目の確認まで実施します。

  • docker execでsupervisorctl利用
  • docker logsでログ確認
  • supervisordのWebインターフェース機能でブラウザ表示

supervisor is 何?

Supervisor is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems.

用意するプログラム

Sleepで10秒に一度出力をするGoプログラム

構成

.
├── Dockerfile
├── go.mod
├── go.sum
├── main.go
└── supervisord.conf
supervisord.conf
[supervisord]
nodaemon=true ; Docker利用ではtrueにする必要あり

[unix_http_server]
file=/var/run/supervisor.sock ; このパスにしないとdocker execで入りsupervisorctlで操作できない

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock

[inet_http_server]
port=0.0.0.0:9001 ; HTTPでのプロセス監視機能。9001ポートで受ける。
username=user     ; ログインするのに必要なBasic認証のユーザーとパス
password=123

[program:mytool]
command=/usr/local/bin/mytool  ; プログラムはmytoolという名前にした
stdout_logfile=/dev/stdout     ; 今回は標準出力へ出すことでdocker logsで確認する
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
Dockerfile
FROM golang:1.15.5-buster as builder

WORKDIR /workspace

ENV GO111MODULE="on"
COPY go.mod go.sum ./
RUN go mod download

COPY . ./

RUN CGO_ENABLED=0 GOOS=linux go build -o mytool

FROM ubuntu:16.04

RUN apt-get update && apt-get install -y supervisor
RUN mkdir -p /var/log/supervisor # TODO: need?

COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf

COPY --from=builder /workspace/mytool /usr/local/bin/

CMD ["/usr/bin/supervisord"]
main.go
package main

import (
    "fmt"
    "time"

    "github.com/vikyd/zero"
)

func main() {
    for {
        var v int
        fmt.Println("hello supervisor!?", zero.IsZeroVal(v))
        time.Sleep(10 * time.Second)
    }
}

動かす

Docker起動

$ docker build -t supervisor-image .
$ docker run --name supervisor -p 9001:9001 -d supervisor-image

コンテナに入りsupervisorctlを使う

$ docker exec -it supervisor bash
root@e70e1f0c07ef:/# supervisorctl help

default commands (type help <topic>):
=====================================
add    exit      open  reload  restart   start   tail
avail  fg        pid   remove  shutdown  status  update
clear  maintail  quit  reread  signal    stop    version

root@e70e1f0c07ef:/# supervisorctl status
mytool                           RUNNING   pid 8, uptime 0:09:25

docker logsでプロセス確認

$ docker logs -f supervisor
hello supervisor!? true
hello supervisor!? true
hello supervisor!? true
hello supervisor!? true
hello supervisor!? true
hello supervisor!? true
hello supervisor!? true
hello supervisor!? true
hello supervisor!? true
hello supervisor!? true

Webインターフェース機能でブラウザ表示

http://localhost:9001 へアクセス

image.png

image.png

2
1
0

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
2
1