0

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.

6
  • Can you docker run -d your-image php /var/www/app/artisan queue:work --sleep=3 --tries=3 in a separate container, without involving supervisord? Commented Jan 18, 2024 at 20:38
  • I think supervisor might be running in your app service, which is why it still runs the task. In your supervisor service, it doesn't look like you are mounting any volumes or copying any files which may also be causing the issues Commented Jan 18, 2024 at 22:28
  • @DavidMaze I'm just implementing a solution so what you suggest could be a possibility. Would you ming expanding more? I mean, in my docker-compose file, how should I add a new container to run only php /var/www/app/artisan queue:work? One question that arises, this container would be a copy of app right? which would take more space and resources than simply running supervisor, right? I don't know, i'm just asking Commented Jan 19, 2024 at 1:15
  • @Second2None but I'm copying over supervisor files in Dockerfile, shouldn't that work? Commented Jan 19, 2024 at 1:15
  • @MrCujo It needs all the app files to run - you could also mount them to the supervisor service, or if you were copying the project files in the Dockerfile, I'm pretty sure it would work as well. The image you are pulling is the same, but it's not the same instance - so when you mount the volumes to the app service, the supervisor service doesn't have access to those. Atleast that's my understanding, I could be wrong. Commented Jan 19, 2024 at 1:35

1 Answer 1

0

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"]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.