FROM ruby:3.3.5-bookworm

RUN curl -sL https://deb.nodesource.com/setup_lts.x | bash - && \
    apt-get update && apt-get dist-upgrade -y && \
    apt-get install -y \
        nodejs \
        nginx && \
    apt-get autoremove -y && apt-get clean && rm -rf /var/lib/apt/lists/*

RUN gem update --no-document --system && gem install bundler --no-document && corepack enable

# Install nginx config files
RUN rm /etc/nginx/sites-enabled/default
ADD docker/nginx_server.conf /etc/nginx/sites-enabled/parsley.conf

# Add scripts
ADD docker/bin/* /usr/local/bin/
RUN chmod +x /usr/local/bin/entrypoint.sh /usr/local/bin/nginx_service.sh /usr/local/bin/rails_service.sh

# Set Default RAILS_ENV
ENV RAILS_ENV docker

# Setup directory and install gems
RUN mkdir -p /parsley
WORKDIR /parsley

COPY Gemfile* ./
RUN bundle install

COPY package.json yarn.lock ./
RUN yarn install --immutable

COPY . .

# Compile assets
RUN env RAILS_ENV=production bundle exec rails shakapacker:clobber shakapacker:compile

ENV PORT=80
EXPOSE 80
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
