1 介紹
記錄使用docker 構建包含 supervior
的映象,
supervisor
: 是一個管理和監控程序的程式,可以方便的透過配置檔案來管理我們的任務指令碼
將supervisor構建到系統映象中,啟動時預設啟動 supervisor程序,容器可以正常執行,然後透過 ,supervisor 去啟動,停止,重啟我們的 任務指令碼,例如, flask, fastapi 等服務啟動指令碼
1.1 背景
構建一個 fastapi web
服務映象,方便業務上線.基礎需求:
- 容器中沒有
fastapi web
服務也可以正常啟動執行 web 服務
透過配置檔案可插拔載入和分離
2 具體流程
2.1 準備supervisor預設啟動的配置檔案
使用預設的配置檔案可以 預設開啟 supervisor 的 http 伺服器管理訪問功能,即配置塊 inet_http_server
對應的配置如下:
; supervisor config file
[unix_http_server]
file=/var/run/supervisor.sock ; (the path to the socket file)
chmod=0700 ; sockef file mode (default 0700)
[supervisord]
nodaemon=true
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP)
; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
# [supervisorctl]
# serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket
; The [include] section can just contain the "files" setting. This
; setting can list multiple files (separated by whitespace or
; newlines). It can also contain wildcards. The filenames are
; interpreted as relative to this file. Included files *cannot*
; include files themselves.
[include]
files = /etc/supervisor/conf.d/*.conf
[inet_http_server] ; inet (TCP) server disabled by default
port=*:9001 ; (ip_address:port specifier, *:port for all iface)
username=admin ; (default is no username (open server))
password=admin.123 ; (default is no password (open server))
2.2 web服務的pip包安裝配置檔案
準備 requirements.txt
, 使用時根據自己實際情況.
fastapi==0.115.0
2.3 Dockerfile構建配置
這裡我使用 python:3.10-slim
作為基礎映象構建, supervisor的啟動命令作為入口 CMD
FROM python:3.10-slim
MAINTAINER faron
WORKDIR /usr/src/app
RUN mkdir -p /var/log/supervisor \
&& apt-get update && apt-get install -y cron autoconf automake libtool vim procps supervisor \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
COPY requirements.txt ./
COPY supervisord.conf /etc/supervisor/supervisord.conf
RUN pip install -i https://mirrors.aliyun.com/pypi/simple --no-cache-dir -r requirements.txt \
&& rm -rf requirements.txt
CMD ["/usr/bin/supervisord","-c","/etc/supervisor/supervisord.conf" ]
構建命令:
docker build -t web:1.0 .
2.4 啟動測試
使用 docker-compose
進行測試
services:
web: # web服務
image: web:1.0
container_name: web_test
ports:
- 9001:9001
volumes:
- /etc/localtime:/etc/localtime
restart: always
啟動命令:
docker-compose up -d
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
web_test web:1.0 "/usr/bin/supervisor…" web 8 seconds ago Up 7 seconds 0.0.0.0:9001->9001/tcp
檢視效果: 訪問 IP:9001 ,賬戶名密碼: admin admin.123
賬戶名密碼在 supervisord.conf
檔案的 inet_http_server
下;
3 使用supervisor配置啟動服務
這裡以一個 實時檢視文字檔案的需求服務為示例,介紹如何透過 supervisor 管理服務;
3.1 建立 服務配置檔案
實時檢視某個檔案的服務管理指令碼: test.conf
[program:test]
command=tail -n 20 -f /var/log/supervisor/supervisord.log
autostart=true
startsecs=3
autorestart=true
調整docker-compose.yml
檔案,將啟動配置檔案對映進去
services:
web: # web服務
image: web:1.0
container_name: web_test
ports:
- 9001:9001
volumes:
- /etc/localtime:/etc/localtime
- ./test.conf:/etc/supervisor/conf.d/test.conf # 對映啟動配置檔案
restart: always
啟動新的容器服務,注意歷史的關閉
docker-compose down
docker-compose up -d
訪問 IP:9001
4 參考文件
- supervisor configuration
- supervisor program config