Carlos Aguni

Highly motivated self-taught IT analyst. Always learning and ready to explore new skills. An eternal apprentice.


Supervisord in Docker

15 Jun 2022 »

https://advancedweb.hu/supervisor-with-docker-lessons-learned/

Using Supervisor with Docker

https://gdevillele.github.io/engine/admin/using_supervisord/

Dockerfile

FROM python:3.7.4


RUN pip3 install flask flask_cors supervisor

COPY supervisord.conf /etc/supervisord.conf

COPY file /file

COPY test-flask.py /test-flask.py
COPY test-write.py /test-write.py

CMD ["/usr/local/bin/supervisord"]

supervisord.conf

[unix_http_server]
file=/tmp/supervisor.sock ; the path to the socket file

[supervisord]
nodaemon=true
logfile=/tmp/supervisord.log
logfile_maxbytes=5MB

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

[supervisorctl]
serverurl=unix://tmp/supervisor.sock ; use a unix:// URL for a unix socket

[program:p-write-to-file]
command=/usr/local/bin/python3 /test-write.py

[program:p-flask]
command=/usr/local/bin/python3 /test-flask.py

supervisord.conf # output to docker container

[unix_http_server]
file=/tmp/supervisor.sock ; the path to the socket file

[supervisord]
nodaemon=true
logfile=/tmp/supervisord.log
logfile_maxbytes=5MB

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

[supervisorctl]
serverurl=unix://tmp/supervisor.sock ; use a unix:// URL for a unix socket

[program:p-write-to-file]
command=/usr/local/bin/python3 /test-write.py
stdout_logfile=/proc/1/fd/1
redirect_stderr=true
stdout_logfile_maxbytes = 0
environment=PYTHONUNBUFFERED=1

[program:p-flask]
command=/usr/local/bin/python3 /test-flask.py
stdout_logfile=/proc/1/fd/1
redirect_stderr=true
stdout_logfile_maxbytes = 0
environment=PYTHONUNBUFFERED=1

test-write.py

import time



idx = 0
while True:
    with open("/file", "a+") as f:
        f.write(f"{idx}\n")
        idx+=1
    time.sleep(1)

test-flask.py

from flask import Flask, request, jsonify, json, abort, redirect, url_for, render_template
from flask_cors import CORS, cross_origin
import os
import re
import subprocess
import traceback

app = Flask(__name__, template_folder='template')
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'

@app.route('/', methods=['GET', 'POST'])
@cross_origin()
def main():
    txt = open("/file").read()
    return txt
    return "hello world"

# gunicorn --workers=2 'app:create_app()' --bind=0.0.0.0:<port>
def create_app():
    return app

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080)
    
    #test 
    #with app.test_client() as c:
    #    rs = c.get("/")
    #    print(rs.data)