C
C
Crash2021-02-08 18:41:22
Docker
Crash, 2021-02-08 18:41:22

How to start a background process (worker) inside a docker container so that it runs reliably and restarts when necessary?

Hello! I make a worker on Symphony, all work goes through the docker. Accordingly, the worker is launched in a separate container. RabbitMQ is used as a transport. Now there is a problem: in order for the worker to work normally, I go to the console of the running container and run the command manually:

php bin/console messenger:consume amqp_image_caching -vv


Everything works great. Messages travel from the main container through the RabbitMQ container to the worker container. But there is one thing - this is a so-so solution and you need to run this command automatically immediately after starting the worker container. And then I ran into a problem. If you add this line to the end of the Dockerfile:

CMD ["php bin/console messenger:consume amqp_image_caching -vv"]


Then the worker container will always be in the Restarting state and the worker itself will not work (I checked that messages from the queue do not reach it).

Full Dockerfile for worker container:

FROM php:7.4-fpm

RUN apt-get update && apt-get install -y zlib1g-dev g++ git libicu-dev zip libzip-dev zip \
    && docker-php-ext-install intl opcache pdo pdo_mysql \
    && pecl install apcu \
    && docker-php-ext-enable apcu \
    && docker-php-ext-configure zip \
    && docker-php-ext-install zip

# AMQP
RUN docker-php-ext-install sockets
RUN apt-get install -y librabbitmq-dev \
    && pecl install amqp \
    && docker-php-ext-enable amqp

RUN apt-get install -y inetutils-ping

# GD
RUN apt-get update -y && apt-get install -y libwebp-dev libjpeg62-turbo-dev libpng-dev libxpm-dev \
    libfreetype6-dev
RUN docker-php-ext-configure gd --with-freetype --with-jpeg \
    --with-webp
RUN docker-php-ext-install gd

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

RUN curl -sS https://get.symfony.com/cli/installer | bash

RUN mv /root/.symfony/bin/symfony /usr/local/bin/symfony

WORKDIR /var/www/project

CMD ["php bin/console messenger:consume amqp_image_caching -vv"]


The worker is launched along with other containers via docker-compose if anything, they work fine. The problem is with running the background command in a separate worker container. Please tell me how to run it correctly.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2021-02-08
@Bandicoot

Try to specify the full path to the php binary and separately give parameters:

CMD ["/usr/local/bin/php", "bin/console", "messenger:consume", "amqp_image_caching", "-vv"]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question