FROM docker.io/bitnami/php-fpm:8.4 AS database
WORKDIR /app
COPY ./composer.* ./symfony.* ./
COPY ./bin ./bin
COPY ./.env.prod ./.env
COPY ./config ./config
COPY ./migrations ./migrations
COPY ./src ./src
RUN mkdir public \
    && composer install --no-cache --no-progress \
    && php bin/console doctrine:migrations:migrate latest \
    && rm -rf composer.* symfony.* bin .env config migrations src vendor

FROM database AS apt-install
RUN set -eux \
    && apt-get update && apt-get install -y --no-install-recommends \
        acl \
    && rm -rf /var/lib/apt/lists/*

FROM apt-install AS composer-install
COPY --from=database /app/var /app/var
WORKDIR /app
COPY ./composer.* ./symfony.* ./
COPY ./.env.prod ./.env
RUN composer install \
    --no-cache \
    --no-dev \
    --no-progress \
    --no-scripts \
    --optimize-autoloader

FROM composer-install AS copy-src
COPY --from=composer-install /app /app
WORKDIR /app
COPY . .
RUN mkdir -p var/log var/cache && \
    chown -R www-data:www-data var && \
    chmod -R 775 var
