一, 簡介
Dockerfile 運行只支持一條命令,當在Docker里要運行多條命令,用supervisor來管理就比較合適了。
Supervisor是一個 Python 開發的 client/server 系統,可以管理和監控類 UNIX 操作系統上面的進程。它可以同時啟動,關閉多個進程,使用起來特別的方便。
組成部分
supervisor 主要由兩部分組成:
supervisord(server 部分):主要負責管理子進程,響應客戶端命令以及日志的輸出等;
supervisorctl(client 部分):命令行客戶端,用戶可以通過它與不同的 supervisord 進程聯系,獲取子進程的狀態等。
二,存在的問題——日志不輸出
但是使用supervisor,Django運行的日志就不會在Docker里輸出了,默認的輸出如下:
2018-03-28 06:48:20,292 CRIT Supervisor running as root (no user in config file)
2018-03-28 06:48:20,308 INFO supervisord started with pid 1
2018-03-28 06:48:21,310 INFO spawned: 'celery_beat' with pid 7
2018-03-28 06:48:21,312 INFO spawned: 'celery_worker' with pid 8
2018-03-28 06:48:21,313 INFO spawned: 'django' with pid 9
2018-03-28 06:48:22,315 INFO success: celery_beat entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2018-03-28 06:48:22,315 INFO success: celery_worker entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2018-03-28 06:48:22,315 INFO success: django entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
上面的Docker中supervisor配置如下:
[supervisord] nodaemon=true [program:django] command=python manage.py runserver 0.0.0.0:8080 [program:celery_worker] command=python manage.py celery worker -c 4 -l info [program:celery_beat] command=python manage.py celery beat