FROM nginx:1.21-alpine as base

COPY ./docker/nginx/nginx.conf /etc/nginx/

RUN adduser -D -H -u 1000 -s /bin/bash -G www-data www-data

ARG PHP_UPSTREAM_CONTAINER=php-fpm
ARG PHP_UPSTREAM_PORT=9000

# Set upstream conf and remove the default conf
RUN echo "upstream php-upstream { server ${PHP_UPSTREAM_CONTAINER}:${PHP_UPSTREAM_PORT}; }" > /etc/nginx/conf.d/upstream.conf \
    && rm /etc/nginx/conf.d/default.conf

CMD ["nginx"]

EXPOSE 8000

FROM composer:2.2.9 as build

COPY cms/composer.json /var/www/composer.json
COPY cms/composer.lock /var/www/composer.lock

WORKDIR /var/www

RUN composer install --prefer-dist --no-dev --ignore-platform-reqs --no-scripts -o -q

COPY cms /var/www

FROM base as run

RUN rm /etc/nginx/conf.d/upstream.conf

COPY --from=build /var/www /var/www

COPY ./docker/nginx/sites/default.conf /etc/nginx/sites-available/default.conf
