在 Laravel dockerized 应用程序上运行主管的问题

问题描述 投票:0回答:1

我有一个运行良好的 dockerized Laravel 应用程序。最近,应用程序中添加了一项工作,为了管理这项工作,我们将使用

supervisor

运行应用程序时

docker-compose --build
应用程序按预期构建,但我在与主管相关的std输出中看到错误:

dmc-supervisor  | Error: The directory named as part of the path /var/www/storage/logs/worker.log does not exist in section 'program:app-worker' (file: '/etc/supervisor/conf.d/worker.conf')
dmc-supervisor  | For help, use /usr/bin/supervisord -h
dmc-supervisor exited with code 2

据此,我预计

supervisor
不会运行,但奇怪的是,当我运行一个将触发作业的操作时,我看到了正确的结果,这意味着作业正在以某种方式运行。

如果我登录容器

docker exec -it a4b21cb5b474 bash
并运行
supervisorctl status
我得到:

unix:///var/run/supervisor.sock 没有这样的文件

如果我跑步

supervisord
我得到:

Traceback (most recent call last):
  File "/usr/bin/supervisord", line 33, in <module>
    sys.exit(load_entry_point('supervisor==4.2.2', 'console_scripts', 'supervisord')())
  File "/usr/lib/python3/dist-packages/supervisor/supervisord.py", line 359, in main
    go(options)
  File "/usr/lib/python3/dist-packages/supervisor/supervisord.py", line 369, in go
    d.main()
  File "/usr/lib/python3/dist-packages/supervisor/supervisord.py", line 72, in main
    self.options.make_logger()
  File "/usr/lib/python3/dist-packages/supervisor/options.py", line 1494, in make_logger
    loggers.handle_file(
  File "/usr/lib/python3/dist-packages/supervisor/loggers.py", line 419, in handle_file
    handler = RotatingFileHandler(filename, 'a', maxbytes, backups)
  File "/usr/lib/python3/dist-packages/supervisor/loggers.py", line 213, in __init__
    FileHandler.__init__(self, filename, mode)
  File "/usr/lib/python3/dist-packages/supervisor/loggers.py", line 160, in __init__
    self.stream = open(filename, mode)
PermissionError: [Errno 13] Permission denied: '/etc/supervisor/logs/supervisord.log'

如果我访问它在构建中抱怨的文件夹(

/var/www/storage/logs/
),我可以验证它确实存在:

$ pwd
/var/www/storage/logs

知道发生了什么事吗?

这是我的

Dockerfile

FROM php:8.1.12-fpm

ARG uid=1000
ARG user=inigomontoya

RUN apt-get update && apt-get install -y \
    libpng-dev \
    libonig-dev \
    libxml2-dev \
    libzip-dev \
    git \
    curl \
    zip \
    unzip \
    supervisor

# Install and enable xDebug
RUN pecl install xdebug \
    && docker-php-ext-enable xdebug

RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install php modules required by laravel.
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd zip

# Create system user to run Composer and Artisan commands.
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
    chown -R $user:$user /home/$user

# Install composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Set working directory
WORKDIR /var/www

# Create directory for supervisor logs
RUN mkdir -p "/etc/supervisor/logs"

# Copy supervisor config files
ADD ./docker/config/supervisor/supervisord.conf /etc/supervisor/conf.d/worker.conf

USER $user

这是我的

docker-compose.yaml

version: "3.9"
services:
  app:
    build:
      context: ./
      dockerfile: Dockerfile
    image: dmc
    container_name: dmc-app
    restart: unless-stopped
    working_dir: /var/www/
    depends_on:
      - db
      - nginx
    volumes:
      - ./:/var/www/
      - ./docker/php/conf.d/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
      - ./docker/php/conf.d/error_reporting.ini:/usr/local/etc/php/conf.d/error_reporting.ini
      - ./images:/public/images
    expose:
      - "9003"
    networks:
      - dmc-net

  nginx:
    image: nginx:1.23.2-alpine
    container_name: dmc-nginx
    restart: unless-stopped
    ports:
      - "8000:80"
    volumes:
      - ./:/var/www
      - ./docker-compose/nginx:/etc/nginx/conf.d
    networks:
      - dmc-net

  supervisor:
    image: dmc
    container_name: dmc-supervisor
    networks:
      - dmc-net
    depends_on:
      - app
      - nginx
    command:
      - supervisord

  db:
    image: mysql:8.0.31
    container_name: dmc-db
    restart: unless-stopped
    # using 3307 on the host machine to avoid collisions in case there's a local MySQL instance installed already.
    ports:
      - "3307:3306"
    # use the variables declared in .env file
    environment:
      MYSQL_HOST: ${DB_HOST}
      MYSQL_DATABASE: ${DB_DATABASE}
      MYSQL_PASSWORD: ${DB_PASSWORD}
      MYSQL_ROOT_PASSWORD: abcd1234
      MYSQL_USER: ${DB_USERNAME}
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes:
      - ./docker-compose/mysql:/docker-entrypoint-initdb.d
      - mysql-data:/var/lib/mysql
    networks:
      - dmc-net

networks:
  dmc-net:
    driver: bridge

volumes:
  mysql-data:

这是我的

supervisord.conf
文件:

[supervisord]
logfile=/etc/supervisor/logs/supervisord.log ; main log file; default $CWD/supervisord.log
logfile_maxbytes=5MB         ; max main logfile bytes b4 rotation; default 50MB
logfile_backups=10           ; # of main logfile backups; 0 means none, default 10
loglevel=info                ; log level; default info; others: debug,warn,trace
pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid
nodaemon=false               ; start in foreground if true; default false
minfds=1024                  ; min. avail startup file descriptors; default 1024
minprocs=200                 ; min. avail process descriptors;default 200

[supervisorctl]
;serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket
serverurl=unix:///var/run/supervisord.sock

[program:app-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/app/artisan queue:work --sleep=3 --tries=3
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
numprocs=1
redirect_stderr=true
user=root
stdout_logfile=/var/www/storage/logs/worker.log

注意: 使用此配置,容器已构建并运行,但我遇到了上面刚刚指出的问题,但如果我添加:

CMD ["/usr/bin/supervisord", "-n", "-c",  "/etc/supervisor/conf.d/worker.conf"]

CMD ["/usr/bin/supervisord"]

在我的

Dockerfile
结束时,构建进入一个循环,其中出现错误:

PermissionError: [Errno 13] Permission denied: '/etc/supervisor/logs/supervisord.log'

不断抛出并且永远不会构建。

谢谢。

laravel docker docker-compose supervisord laravel-jobs
1个回答
0
投票

只是一些建议

您可以将其添加到 Dockerfile 的最后一行,然后 您可能想检查 /var/www/storage/logs/

的文件权限
RUN mkdir -p /var/www/storage/logs/

在你的Docker文件中,你可以简化从Add到copy的复制操作 建议使用 as copy 将本地文件复制到 Docker 镜像中。 并且只处理本地文件到容器中的基本复制

ADD ./docker/config/supervisor/supervisord.conf /etc/supervisor/conf.d/worker.conf


to 

 COPY ./docker/config/supervisor/supervisord.conf /etc/supervisor/conf.d/supervisord.conf

还要修改supervisor配置并设置为no deamon

[supervisord]
nodaemon=true


[supervisorctl]
;serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket
serverurl=unix:///var/run/supervisord.sock

[program:app-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/artisan queue:work database --sleep=3 --tries=3
autostart=true
autorestart=true
numprocs=8
user=root
redirect_stderr=true
stdout_logfile=//var/www/html/storage/logs/worker.log
stopwaitsecs=3600



and 
you might need to change 
CMD ["/bin/bash", "-c", "/usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf"]
© www.soinside.com 2019 - 2024. All rights reserved.