dnmp 中supervisor使用教程
supervisor是用python 实现的,所以安装supervisor容器就要消耗更多空间.
supervisor 单独一个容器用法:
git clone https://github.com/yeszao/dnmp.git
cp docker-compose.sample.yml docker-compose.yml
cp env.sample .env
然后把docker-compose.yml中最下面的supervisor部分的注释解开
supervisor:(注意顶到头前要空两格)
build:
context: ./services/supervisor //DockerFile目录
args:
ALPINE_VERSION: alpine:${ALPINE_VERSION} //容器镜像
TZ: "$TZ"
CONTAINER_PACKAGE_URL: ${CONTAINER_PACKAGE_URL} //映射ENV文件中的镜像地址
container_name: supervisor
ports:
- "${SUPERVISOR_HOST_PORT_C}:9001" //映射Web Supervisor管理的端口
volumes: //映射目录
- ${SOURCE_DIR}:/www/:rw
- ${SUPERVISOR_LOG}:/var/log/supervisor/:rw
- ${SUPERVISOR_CONFIG}:/etc/supervisor/conf.d/:rw
- ${SUPERVISOR_CONF_FILE}:/etc/supervisor/supervisord.conf:ro
command: //启动时执行命令
- /bin/sh
- -c
- |
supervisord -n -c /etc/supervisor/supervisord.conf
restart: always
cap_add:
- SYS_PTRACE
networks:
- default
docker-compose up -d
然后就启动成功了,再访问 9001 端口就会出现supervisor的web管理界面了 默认账号user 密码 123 (可在主配置中修改)
在PHP容器中使用supervisor
注意是Php-Cli 如果是 Php-Fpm 则容器启动的时候不能将两个进程全部启动,可在supervisor中添加php-fpm的守护进程
修改过后,增加端口,文件,启动命令
php:
build:
context: ./services/php
args:
PHP_VERSION: php:${PHP_VERSION}-alpine
CONTAINER_PACKAGE_URL: ${CONTAINER_PACKAGE_URL}
PHP_EXTENSIONS: ${PHP_EXTENSIONS}
TZ: "$TZ"
container_name: php
expose:
- 9501
ports:
- "${SUPERVISOR_HOST_PORT_C}:9001"
extra_hosts:
- "www.site1.com:172.17.0.1"
volumes:
- ${SOURCE_DIR}:/www/:rw
- ${PHP_PHP_CONF_FILE}:/usr/local/etc/php/php.ini:ro
- ${PHP_FPM_CONF_FILE}:/usr/local/etc/php-fpm.d/www.conf:rw
- ${PHP_LOG_DIR}:/var/log/php
- ${DATA_DIR}/composer:/tmp/composer
- ${SUPERVISOR_LOG}:/var/log/supervisor/:rw
- ${SUPERVISOR_CONFIG}:/etc/supervisor/conf.d/:rw
- ${SUPERVISOR_CONF_FILE}:/etc/supervisor/supervisord.conf:ro
restart: always
command:
- /bin/sh
- -c
- |
supervisord -n -c /etc/supervisor/supervisord.conf
cap_add:
- SYS_PTRACE
networks:
- default
php DockerFile 的修改
dnmp/services/php/Dockerfile
ARG PHP_VERSION
FROM ${PHP_VERSION}
ARG TZ
ARG PHP_EXTENSIONS
ARG CONTAINER_PACKAGE_URL
RUN if [ $CONTAINER_PACKAGE_URL ] ; then sed -i "s/dl-cdn.alpinelinux.org/${CONTAINER_PACKAGE_URL}/g" /etc/apk/repositories ; fi
RUN apk update
&& apk upgrade
&& apk add supervisor
&& apk --no-cache add tzdata
&& cp "/usr/share/zoneinfo/$TZ" /etc/localtime
&& echo "$TZ" > /etc/timezone
&& rm -rf /var/cache/apk/*
COPY ./extensions /tmp/extensions
WORKDIR /tmp/extensions
RUN chmod +x install.sh
&& sh install.sh
&& rm -rf /tmp/extensions
ADD ./extensions/install-php-extensions /usr/local/bin/
增加了这一段安装的命令
RUN apk update
&& apk upgrade
&& apk add supervisor
&& apk --no-cache add tzdata
&& cp "/usr/share/zoneinfo/$TZ" /etc/localtime
&& echo "$TZ" > /etc/timezone
&& rm -rf /var/cache/apk/*
然后在supervisor子配置文件中测试
[program:php]
command=/usr/local/bin/php -S 0.0.0.0:80
directory=/www/localhost/
priority=999 ; the relative start priority (default 999)
autostart=true ; start at supervisord start (default: true)
autorestart=true ; retstart at unexpected quit (default: true)
startsecs=10 ; number of secs prog must stay running (def. 10)
startretries=3 ; max # of serial start failures (default 3)
exitcodes=0,2 ; 'expected' exit codes for process (default 0,2)
stopsignal=QUIT ; signal used to kill process (default TERM)
stopwaitsecs=10 ; max num secs to wait before SIGKILL (default 10)
user=root ; setuid to this UNIX account to run the program
log_stdout=true
log_stderr=true ; if true, log program stderr (def false)
logfile=/var/log/supervisor/php.log
logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
logfile_backups=10 ; # of logfile backups (default 10)
stdout_logfile_maxbytes=20MB ; stdout 日志文件大小,默认 50MB
stdout_logfile_backups=20 ; stdout 日志文件备份数
stdout_logfile=/var/log/supervisor/php.stdout.log
supervisor web端的进程列表就显示了
OK
当然可以直接用我改好的
链接