FROM phusion/passenger-ruby23:latest

# Use baseimage-docker's init process.
CMD ["/sbin/my_init"]

# Enable Nginx / Passenger
RUN rm -f /etc/service/nginx/down

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

# Add DB Migration Script
RUN mkdir -p /etc/my_init.d
ADD docker/db_migrate.sh /etc/my_init.d/db_migrate.sh
RUN chmod +x /etc/my_init.d/db_migrate.sh

# Set Default RAILS_ENV
ENV RAILS_ENV docker
ENV PASSENGER_APP_ENV docker

# Setup directory and install gems
RUN mkdir -p /home/app/parsley/
COPY Gemfile /home/app/parsley/
COPY Gemfile.lock /home/app/parsley/
RUN gem install bundler
RUN cd /home/app/parsley/ && bundle install --jobs 4

# Copy the app into the image
COPY . /home/app/parsley/
WORKDIR /home/app/parsley/

# Set log permissions
RUN mkdir -p /home/app/parsley/log
RUN chmod 0777 /home/app/parsley/log

# Compile assets
RUN env RAILS_ENV=production bundle exec rake assets:clobber assets:precompile

# Set ownership of the tmp folder
RUN chown -R app:app /home/app/parsley/tmp

# Clean up APT when done.
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
