docker构建php74+supervisor+cron

作者: 分类: php 时间: 2025-05-27 评论: 暂无评论

Dockerfile

FROM php:7.4.33-fpm

COPY sources.list /etc/apt/sources.list
### 安装编译 Redis 扩展所需的依赖
RUN apt-get update && apt-get install -y \
autoconf \
g++ \
make \
libssl-dev \
libzip-dev \
libpng-dev \
libjpeg-dev \
libfreetype6-dev \
# 补充运行时依赖库
libfreetype6 \
libjpeg62-turbo \
libpng16-16 \
cron \
#添加工具ps 用于查看进程
lsof \
procps \
vim \
supervisor \
# 配置 GD 扩展(明确指定路径)
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) gd \
&& docker-php-ext-install pdo_mysql zip \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# 下载并解压 Redis 扩展源码(版本 5.3.7)
# 注意:需提前将源码包放在构建上下文的 `redis/` 目录下
COPY redis/redis-5.3.7.tgz /tmp/
RUN mkdir -p /usr/src/php/ext/redis \
&& tar xfz /tmp/redis-5.3.7.tgz -C /usr/src/php/ext/redis --strip-components=1 \
&& rm /tmp/redis-5.3.7.tgz
# 编译安装 Redis 扩展
RUN docker-php-ext-install redis
# 复制配置
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY cronjobs /etc/cron.d/cronjobs

# 设置权限
RUN chmod 0644 /etc/cron.d/cronjobs && \
touch /var/log/cron.log

# 启动 Supervisor  supervisor 也是9000端口
CMD ["/usr/bin/supervisord", "-n", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
# /usr/bin/supervisord -n -c /etc/supervisor/conf.d/supervisord.conf

WORKDIR /www/monitor_plat

EXPOSE 9000

sources.list

deb http://mirrors.aliyun.com/debian/ bullseye main contrib non-free
eb http://mirrors.aliyun.com/debian-security/ bullseye-security main
deb http://mirrors.aliyun.com/debian/ bullseye-updates main contrib non-free

default.conf

server {
listen 80;
index index.php index.html;
server_name smart.u3733.com;
root /www/monitor_plat/public;

location / {
  if (!-e $request_filename){
      rewrite  ^(.*)$  /index.php?s=$1  last; break;
  }
}

location ~ \.php$ {
    fastcgi_pass  php74:9000;
    fastcgi_index index.php;
    include       fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

}

cronjobs

# 每隔2小时重启队列进程(注意使用绝对路径)
0 */2 * * * root /usr/bin/supervisorctl restart queue

10 * * * * root /usr/bin/supervisorctl restart queue

# 每天8:03执行定时脚本(设置工作目录)
3 8 * * * root cd /www/monitor_plat && /usr/local/bin/php think day

docker-compose.yml

version: '3'

services:
  php74:
#    image: registry.cn-hangzhou.aliyuncs.com/gcpu/lnmp:php-fpm-7.4-supervisord
    build:
      context: .
      dockerfile: Dockerfile
    container_name: php74
    ports:
      - "9001:9000"
    volumes:
      - ./../:/www/monitor_plat
      - ./php74/php.ini:/usr/local/etc/php/php.ini
    networks:
      - smart-network


  nginx:
    image: registry.cn-hangzhou.aliyuncs.com/gcpu/lnmp:nginx_1.27.5
    container_name: nginx
    ports:
      - "9090:80"
    volumes:
      - ./../:/www/monitor_plat
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    networks:
      - smart-network
    depends_on:
      - php74

networks:
  smart-network:

  ####
  ##  docker-compose build --no-cache  # 强制完全重建
  ##  docker-compose up -d --force-recreate  # 重启容器

supervisord 有两种模式,一种是端口的占用9000,一种是unix套接字
supervisord.conf

[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/supervisord.log
pidfile=/var/run/supervisord.pid

[program:php-fpm]
daemonize = no  ; 禁止后台守护进程模式
command=/usr/local/sbin/php-fpm
autostart=true
autorestart=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

[program:queue]
command=php /www/monitor_plat/think queue:listen --queue=default
directory=/www/monitor_plat  # 设置工作目录
autostart=true
autorestart=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

[program:cron]
command=cron -f  # 保持前台运行
autostart=true
autorestart=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0