laraveldockerdockerfileaws-fargatelaravel-horizon

Php artisan horizon error when build container


i have an application written in laravel and i should manage queues through laravel HORIZON this application i should put it in a container and then deploy on AWS Fargate. But strange things happen to me like for example:

1.This is my dockerfile

FROM php:8.1-fpm
RUN apt-get update && apt-get install -y \
 git \
 curl \
 libpng-dev \
 libonig-dev \
 libxml2-dev \
 zip \
 unzip \
 supervisor

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

RUN pecl install -o -f redis \
 &&  rm -rf /tmp/pear \
 &&  docker-php-ext-enable redis

WORKDIR /app
COPY horizon.conf /etc/supervisor/conf.d/horizon.conf
COPY composer.json .
RUN composer install --no-scripts
COPY .  .
RUN php artisan horizon
CMD php artisan serve --host=0.0.0.0 --port 80

If in the Dockerfile there is RUN php artisan horizon command then during the build of the image I get this error error

2.if instead of php artisan horizon I put php artisan horizon:start then the build of the image is successful and the container starts but horizon is not active horizon dashboard

3.I tried to put php artisan horizon command under the CMD command of the Dockerfile CMD bash -c "php artisan horizon && php artisan serve --host 0.0.0.0 --port 80" it goes in a loop the service I see the logs from AWS cloudwatch aws cloudwatch

The container goes into an active state but keeps restarting... anyone have any idea?


Solution

  • I finally resolved with laravel octane by taking the code from this GitHub account https://github.com/exaco/laravel-octane-dockerfile/blob/main/Dockerfile ... I hope I have been useful to you

    # Accepted values: 8.1 - 8.0
    ARG PHP_VERSION=8.1
    
    ARG COMPOSER_VERSION=latest
    
    ###########################################
    # PHP dependencies
    ###########################################
    
    FROM composer:${COMPOSER_VERSION} AS vendor
    WORKDIR /var/www/html
    COPY composer* ./
    RUN composer install \
      --no-dev \
      --no-interaction \
      --prefer-dist \
      --ignore-platform-reqs \
      --optimize-autoloader \
      --apcu-autoloader \
      --ansi \
      --no-scripts \
      --audit
    
    ###########################################
    
    FROM php:${PHP_VERSION}-cli-buster
    
    LABEL maintainer="Seyed Morteza Ebadi <seyed.me720@gmail.com>"
    
    ARG WWWUSER=1000
    ARG WWWGROUP=1000
    ARG TZ=UTC
    
    # Accepted values: app - horizon - scheduler
    ARG CONTAINER_MODE=app
    
    ARG APP_WITH_HORIZON=false
    ARG APP_WITH_SCHEDULER=false
    
    ENV DEBIAN_FRONTEND=noninteractive \
        TERM=xterm-color \
        CONTAINER_MODE=${CONTAINER_MODE} \
        APP_WITH_HORIZON=${APP_WITH_HORIZON} \
        APP_WITH_SCHEDULER=${APP_WITH_SCHEDULER}
    
    ENV ROOT=/var/www/html
    WORKDIR $ROOT
    
    SHELL ["/bin/bash", "-eou", "pipefail", "-c"]
    
    RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime \
        && echo $TZ > /etc/timezone
    
    RUN apt-get update; \
        apt-get upgrade -yqq; \
        pecl -q channel-update pecl.php.net; \
        apt-get install -yqq --no-install-recommends --show-progress \
              apt-utils \
              gnupg \
              gosu \
              git \
              curl \
              wget \
              libcurl4-openssl-dev \
              ca-certificates \
              supervisor \
              libmemcached-dev \
              libz-dev \
              libbrotli-dev \
              libpq-dev \
              libjpeg-dev \
              libpng-dev \
              libfreetype6-dev \
              libssl-dev \
              libwebp-dev \
              libmcrypt-dev \
              libonig-dev \
              libzip-dev zip unzip \
              libargon2-1 \
              libidn2-0 \
              libpcre2-8-0 \
              libpcre3 \
              libxml2 \
              libzstd1 \
              procps
    
    ###########################################
    # pdo_mysql
    ###########################################
    
    RUN docker-php-ext-install pdo_mysql;
    
    ###########################################
    # zip
    ###########################################
    
    RUN docker-php-ext-configure zip && docker-php-ext-install zip;
    
    ###########################################
    # mbstring
    ###########################################
    
    RUN docker-php-ext-install mbstring;
    
    ###########################################
    # GD
    ###########################################
    
    RUN docker-php-ext-configure gd \
                --prefix=/usr \
                --with-jpeg \
                --with-webp \
                --with-freetype \
        && docker-php-ext-install gd;
    
    ###########################################
    # OPcache
    ###########################################
    
    ARG INSTALL_OPCACHE=true
    
    RUN if [ ${INSTALL_OPCACHE} = true ]; then \
          docker-php-ext-install opcache; \
      fi
    
    ###########################################
    # PHP Redis
    ###########################################
    
    ARG INSTALL_PHPREDIS=true
    
    RUN if [ ${INSTALL_PHPREDIS} = true ]; then \
          pecl -q install -o -f redis \
          && rm -rf /tmp/pear \
          && docker-php-ext-enable redis; \
      fi
    
    ###########################################
    # PCNTL
    ###########################################
    
    ARG INSTALL_PCNTL=true
    
    RUN if [ ${INSTALL_PCNTL} = true ]; then \
          docker-php-ext-install pcntl; \
      fi
    
    ###########################################
    # BCMath
    ###########################################
    
    ARG INSTALL_BCMATH=true
    
    RUN if [ ${INSTALL_BCMATH} = true ]; then \
          docker-php-ext-install bcmath; \
      fi
    
    ###########################################
    # RDKAFKA
    ###########################################
    
    ARG INSTALL_RDKAFKA=true
    
    RUN if [ ${INSTALL_RDKAFKA} = true ]; then \
          apt-get install -yqq --no-install-recommends --show-progress librdkafka-dev \
          && pecl -q install -o -f rdkafka \
          && docker-php-ext-enable rdkafka; \
      fi
    
    ###########################################
    # OpenSwoole/Swoole extension
    ###########################################
    
    ARG INSTALL_SWOOLE=true
    ARG SERVER=openswoole
    
    RUN if [ ${INSTALL_SWOOLE} = true ]; then \
          apt-get install -yqq --no-install-recommends --show-progress libc-ares-dev \
          && pecl -q install -o -f -D 'enable-openssl="yes" enable-http2="yes" enable-swoole-curl="yes" enable-mysqlnd="yes" enable-cares="yes"' ${SERVER} \
          && docker-php-ext-enable ${SERVER}; \
        fi
    
    ###########################################################################
    # Human Language and Character Encoding Support
    ###########################################################################
    
    ARG INSTALL_INTL=true
    
    RUN if [ ${INSTALL_INTL} = true ]; then \
          apt-get install -yqq --no-install-recommends --show-progress zlib1g-dev libicu-dev g++ \
          && docker-php-ext-configure intl \
          && docker-php-ext-install intl; \
      fi
    
    ###########################################
    # Memcached
    ###########################################
    
    ARG INSTALL_MEMCACHED=false
    
    RUN if [ ${INSTALL_MEMCACHED} = true ]; then \
          pecl -q install -o -f memcached && docker-php-ext-enable memcached; \
      fi
    
    ###########################################
    # MySQL Client
    ###########################################
    
    ARG INSTALL_MYSQL_CLIENT=true
    
    RUN if [ ${INSTALL_MYSQL_CLIENT} = true ]; then \
          apt-get install -yqq --no-install-recommends --show-progress default-mysql-client; \
      fi
    
    ###########################################
    # pdo_pgsql
    ###########################################
    
    ARG INSTALL_PDO_PGSQL=false
    
    RUN if [ ${INSTALL_PDO_PGSQL} = true ]; then \
          docker-php-ext-install pdo_pgsql; \
      fi
    
    ###########################################
    # pgsql
    ###########################################
    
    ARG INSTALL_PGSQL=false
    
    RUN if [ ${INSTALL_PGSQL} = true ]; then \
          docker-php-ext-install pgsql; \
      fi
    
    ###########################################
    # pgsql client and postgis
    ###########################################
    
    ARG INSTALL_PG_CLIENT=false
    ARG INSTALL_POSTGIS=false
    
    RUN if [ ${INSTALL_PG_CLIENT} = true ]; then \
          . /etc/os-release \
          && echo "deb http://apt.postgresql.org/pub/repos/apt $VERSION_CODENAME-pgdg main" > /etc/apt/sources.list.d/pgdg.list \
          && curl -sL https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \
          && apt-get install -yqq --no-install-recommends --show-progress postgresql-client-12 postgis; \
          if [ ${INSTALL_POSTGIS} = true ]; then \
            apt-get install -yqq --no-install-recommends --show-progress postgis; \
          fi; \
      fi
    
    ###########################################
    # Laravel scheduler
    ###########################################
    
    RUN if [ ${CONTAINER_MODE} = 'scheduler' ] || [ ${APP_WITH_SCHEDULER} = true ]; then \
          wget -q "https://github.com/aptible/supercronic/releases/download/v0.2.1/supercronic-linux-amd64" \
               -O /usr/bin/supercronic \
          && chmod +x /usr/bin/supercronic \
          && mkdir -p /etc/supercronic \
          && echo "*/1 * * * * php ${ROOT}/artisan schedule:run --verbose --no-interaction" > /etc/supercronic/laravel; \
      fi
    
    ###########################################
    
    RUN groupadd --force -g $WWWGROUP octane \
        && useradd -ms /bin/bash --no-log-init --no-user-group -g $WWWGROUP -u $WWWUSER octane
    
    RUN apt-get clean \
        && docker-php-source delete \
        && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
        && rm /var/log/lastlog /var/log/faillog
    
    COPY . .
    COPY --from=vendor ${ROOT}/vendor vendor
    
    RUN mkdir -p \
      storage/framework/{sessions,views,cache} \
      storage/logs \
      bootstrap/cache \
      && chown -R octane:octane \
      storage \
      bootstrap/cache \
      && chmod -R ug+rwx storage bootstrap/cache
    
    COPY deployment/octane/supervisord* /etc/supervisor/conf.d/
    COPY deployment/octane/php.ini /usr/local/etc/php/conf.d/octane.ini
    COPY deployment/octane/opcache.ini /usr/local/etc/php/conf.d/opcache.ini
    
    RUN chmod +x deployment/octane/entrypoint.sh
    RUN cat deployment/octane/utilities.sh >> ~/.bashrc
    
    EXPOSE 9000
    
    ENTRYPOINT ["deployment/octane/entrypoint.sh"]
    
    HEALTHCHECK --start-period=5s --interval=2s --timeout=5s --retries=8 CMD php artisan octane:status || exit 1