Issues running supervisor on a Laravel dockerized app

149 Views Asked by At

I've got a dockerized Laravel app which is working just fine. Recently, a job was added to the app, to manage this job we'll use supervisor.

When running the app docker-compose --build the app is build as expected, but I'm seeing an error in the std output related to supervisor:

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

According to this, I'd expect supervisor not to be running but weirdly enough when I run an action that will trigger the job I'm seeing the correct results, meaning that somehow the job is running.

If I log into the container docker exec -it a4b21cb5b474 bash and run supervisorctl status I'm getting:

unix:///var/run/supervisor.sock no such file

if I run supervisord I'm getting:

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'

If I visit the folder that it is complaining about in the build (/var/www/storage/logs/), I can verify that it does exist:

$ pwd
/var/www/storage/logs

Any idea what's going on?

Here's my 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

Here's my 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:

And here's my supervisord.conf file:

[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

NOTE: With this configuration the containers are built and running, but I'm getting the issues I just pointed above, but if I add either:

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

or

CMD ["/usr/bin/supervisord"]

at the end of my Dockerfile then the build gets in a loop where the error:

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

is constantly thrown and it never builds.

Thanks.

1

There are 1 best solutions below

0
elemes On

just a few suggestions

you can add this to the last line of your Dockerfile and you might want to check the file permission of /var/www/storage/logs/

RUN mkdir -p /var/www/storage/logs/

or

in your Docker file, you can simplify the copy operation from Add to copy as copy is recommended for copying local files into the Docker image. and only handles the basic copying of local files into the container

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

also modify the supervisor config and set to 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"]