FROM node:16-alpine AS node-builder
WORKDIR /build
COPY package.json package-lock.json tailwind.config.js webpack.mix.js ./
RUN npm install
COPY resources/ resources/
RUN mkdir -p public
RUN npm run production

FROM php:8.1-fpm-alpine

RUN apk add --no-cache \
        git unzip libpng libwebp libjpeg libxpm libxml2 \
    && apk add --no-cache --virtual .build-deps \
        libpng-dev libjpeg-turbo-dev libwebp-dev libxpm-dev libxml2-dev freetype-dev oniguruma-dev \
    && docker-php-ext-install bcmath gd mbstring pdo_mysql xml \
    && apk del .build-deps

COPY --from=composer:2 /usr/bin/composer /usr/bin/composer

WORKDIR /var/www

COPY . .

COPY --from=node-builder /build/public/css /var/www/public/css
COPY --from=node-builder /build/public/js /var/www/public/js

RUN composer install --no-dev --optimize-autoloader && \
    chmod -R 775 storage bootstrap/cache && \
    chown -R www-data:www-data storage bootstrap/cache public/img

COPY .docker/php/entrypoint.sh /entrypoint.sh
COPY .docker/php/php.ini /usr/local/etc/php/conf.d/custom.ini
RUN chmod +x /entrypoint.sh

EXPOSE 9000
ENTRYPOINT ["/entrypoint.sh"]
