FROM ruby:3.0.4-slim

#HEALTHCHECK --start-period=60s CMD curl http://127.0.0.1/

ENV TINI_VERSION v0.18.0
ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini /tini
RUN chmod +x /tini

RUN apt-get -qq update \
  && DEBIAN_FRONTEND=noninteractive apt-get -qq install -y \
    build-essential \
    openssl \
    curl \
    sqlite3 \
    libsqlite3-dev \
    wget \
    default-libmysqlclient-dev \
    nodejs \
    tzdata \
    git \
  && apt-get -qq clean \
  && apt-get autoremove -y \
  && rm -rf \
    /var/lib/apt/lists/* \
    /tmp/* \
    /var/tmp/*

RUN echo 'gem: --no-rdoc --no-ri' > /root/.gemrc
RUN gem install bundler
RUN gem update --system
#Cache gems so they don't install on every code change
RUN rm -rf /rails && mkdir /rails
WORKDIR /rails
COPY Gemfile Gemfile
COPY Gemfile.lock Gemfile.lock
RUN bundle install --jobs 4

# Copy site into place.
ADD . /rails/

#Need temp secret keys to precompile assets
#RUN RAILS_ENV=production bundle exec rake assets:precompile

#SSL
RUN mkdir /ssl \
  && openssl req -sha256 -subj '/CN=home/O=home LTD./C=US' \
    -x509 -nodes -days 365 -newkey rsa:4096 -keyout /ssl/server.key -out /ssl/server.crt

EXPOSE 80
EXPOSE 443

# Tini solves the zombie PID problem
ENTRYPOINT ["/tini", "--"]

CMD bundle exec passenger start --max-pool-size ${PASSENGER_POOL_SIZE} --min-instances ${PASSENGER_POOL_SIZE} --environment production -p 80
# Higher max pool uses more ram
# Followed recommendation from: https://blog.phusion.nl/2015/11/10/heroku-and-passenger-focus-on-the-app-performance/

#CMD bundle exec puma -w 3 -t 5:5 -b 'tcp://0.0.0.0:80' -e production
#CMD bundle exec puma -w 3 -t 5:5 -b 'ssl://0.0.0.0:443?key=/ssl/server.key&verify_mode=none&cert=/ssl/server.crt' -e production
#CMD bundle exec passenger start --max-pool-size 3 --environment production --ssl --ssl-certificate /ssl/server.crt --ssl-certificate-key /ssl/server.key
