1.supervisor的dockerfile文件如下:
FROM ubuntu
RUN apt-get update && apt-get install -y supervisor
RUN mkdir -p /var/log/supervisor
COPY supervisord.conf /etc/supervisor/supervisord.conf
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisord.conf"]
# 可以使用echo_supervisor_conf命令拿到supervisord.conf配置文件
2.supervisord.conf文件
; 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 ;这一句很重要,将supervisord进程交给docker管理(service运行变更为前台运行,否则supervisord可能一直无法启动,也没有报错日志)
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
3.docker-compose.yaml文件如下【只展示supervisor部分,实际中还有PHP,Nginx等容器】
因为我主要使用supervisor监控一个laravel进程;
version: '3.8'
services:
supervisor:
container_name: supervisor
build: ./supervisor
volumes:
- ./supervisor/conf.d:/etc/supervisor/conf.d
- ./supervisor/log:/home/root
#下面两句,主要是把docker的unix套接字和执行文件映射到docker中
- /var/run/docker.sock:/var/run/docker.sock:rw
- /usr/bin/docker:/usr/bin/docker:rw
#如果开启了WSL2,并且有报错“exec: “com.docker.cli”: executable file not found in %PATH%”,则需要使用下面这个映射
- /mnt/wsl/docker-desktop/cli-tools/usr/bin/com.docker.cli:/usr/bin/com.docker.cli
command: /usr/bin/supervisord -c /etc/supervisor/supervisord.conf
networks:
- backend
networks:
backend:
driver: bridge
4.现在就可以在supervisor镜像内使用docker了,最后还有一个conf.d里面创建一个laravel.conf文件,内容如下(使用docker命令,执行了PHP容器的PHP命令):
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=docker exec php php ./laravel/artisan queue:work redis --sleep=3 --tries=3 --max-time=3600
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=root
numprocs=8
redirect_stderr=true
stdout_logfile=/home/root/worker.log
stopwaitsecs=3600
5.打开supervisor的日志,发现如下,则说明已经正常启动了
image.png
总结:整个过程中,最难得还是supervisor容器的开启(主要是nodaemon=true的配置,配置不正确的时候,日志连报错都没有),和将docker执行文件等映射到supervisor容器。