ARG ALPINE_VERSION=3.16
FROM alpine:${ALPINE_VERSION}
#LABEL Maintainer="Ahmed hifny <ahmed.hifny@waseet.net>"
#LABEL Description="Lightweight container with Nginx 1.22 & PHP 8.1 based on Alpine Linux."

# Install packages and remove default server definition
RUN apk add --no-cache \
  curl \
  git \
  nginx \
  npm \
  php81 \
  php81-ctype \
  php81-curl \
  php81-dom \
  php81-fpm \
  php81-gd \
  php81-intl \
  php81-mbstring \
  php81-mysqli \
  php81-opcache \
  php81-openssl \
  php81-phar \
  php81-session \
  php81-xml \
  php81-xmlreader \
  php81-simplexml \
  php81-zlib \
  php81-fileinfo \
  php81-sodium \
  php81-tokenizer \
  php81-exif \
  php81-xmlwriter \
  php81-pdo \
  php81-pdo_mysql \
  php81-redis \
  php81-pear \
  php81-dev \
  php81-zip \
  php81-sockets \
  php81-mongodb \
  php81-iconv \
  # php81-pecl-couchbase\
  supervisor \
  busybox-extras
# Create symlink so programs depending on `php` still function
RUN ln -s /usr/bin/php81 /usr/bin/php
#install composer
COPY --from=composer /usr/bin/composer /usr/bin/composer

# Setup document root
WORKDIR /var/www/html
COPY . .
RUN cp .env.example .env
#Configure nginx
COPY ./nginx.conf /etc/nginx/nginx.conf
#COPY ./default /etc/nginx/sites-available/default
# Configure PHP-FPM
COPY ./fpm-pool.conf /etc/php81/php-fpm.d/www.conf
COPY ./php.ini /etc/php81/conf.d/custom.ini
# Configure supervisord
COPY ./supervisord.conf /etc/supervisor/conf.d/supervisord.conf

RUN mkdir /.conf \
  && mkdir /.conf/psysh \
  && mkdir -p /var/log/supervisor \
  && chmod -R 700 /.conf/psysh \
  && chmod -R 777 /var/www/html/storage \
  && chown -R nobody.nobody /var/www/html/public  /run /var/lib/nginx /var/log/nginx /.conf/psysh

#RUN composer install
RUN composer install --optimize-autoloader --no-interaction --no-progress --ignore-platform-req=ext-couchbase --ignore-platform-req=ext-mongodb
# Make sure files/folders needed by the processes are accessable when they run under the nobody user
RUN set -x ; \
  addgroup -g 82 -S www-data ; \
  adduser -u 82 -D -S -G www-data www-data && exit 0 ; exit 1

# Switch to use a non-root user from here on
USER nobody
# Add application
COPY --chown=nobody . /var/www/html/
# Expose the port nginx is reachable on
EXPOSE 8030
# Let supervisord start nginx & php-fpm
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
# Configure a healthcheck to validate that everything is up&running
#HEALTHCHECK --timeout=10s CMD curl --silent --fail http://127.0.0.1:8080/fpm-ping
