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 \
    #添加工具ps 用于查看进程
    lsof \
    procps \
    vim \
    supervisor \
    && apt-get install -y --no-install-recommends cron \
   # 配置 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

#用文件会出错 deepseek说是少了换行导致的
RUN echo "* * * * * root echo 'cron is running' > /proc/1/fd/1 2>&1" >> /etc/cron.d/custom-cron
RUN echo "0 */2 * * * root /usr/bin/supervisorctl restart queue" >> /etc/cron.d/custom-cron
RUN echo "*/30 * * * * root /usr/bin/supervisorctl update" >> /etc/cron.d/custom-cron
RUN echo "3 8 * * * root cd /www/monitor_plat && /usr/local/bin/php think day" >> /etc/cron.d/custom-cron




# 设置权限
RUN chmod 0644 /etc/cron.d/custom-cron && \
    touch /var/log/cron.log &&\
    chmod 0777 /var/log/cron.log && \
    chgrp crontab /etc/cron.d/custom-cron  # 添加crontab组权限


# 启动 Supervisor  supervisor 也是9000端口  主进程 supervisord 不建议重启
# /usr/bin/supervisord -n -c /etc/supervisor/conf.d/supervisord.conf
# CMD ["/usr/bin/supervisord", "-n", "-c", "/etc/supervisor/conf.d/supervisord.conf"]


# 复制docker-entrypoint.sh到镜像中,并赋予可执行权限
COPY docker-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/docker-entrypoint.sh

CMD ["/usr/local/bin/docker-entrypoint.sh"]

WORKDIR /www/monitor_plat

EXPOSE 9000

sources.list

deb http://mirrors.aliyun.com/debian/ bullseye main contrib non-free
deb 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;
}

}

docker-compose.yml

version: '3'

services:
  php74:
    image: my-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
      - ./supervisord.conf:/etc/supervisor/conf.d/supervisord.conf
    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

[inet_http_server]
port=0.0.0.0:9096

[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

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock

[program:php-fpm]
daemonize = no  ; 禁止后台守护进程模式
command=bash -c "fuser -k 9000/tcp || true; /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
user=www-data
numprocs= 4 ; 启动8个进程
process_name=%(program_name)s_%(process_num)02d ; 动态生成进程名

docker-entrypoint.sh

#!/bin/sh
set -e
# 停止 cron 服务(如果正在运行)
pkill cron || true

# 创建运行时目录并设置适当权限
mkdir -p /www/monitor_plat/runtime
chmod -R 775 /www/monitor_plat/runtime

# 重启 cron 服务
service cron restart

# 后台输出 cron 日志
tail -F /var/log/cron.log &

# 启动主进程
exec /usr/bin/supervisord -n -c /etc/supervisor/conf.d/supervisord.conf