Skip to content

Instantly share code, notes, and snippets.

@kinoute
Last active March 28, 2024 04:44
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kinoute/b7d54042d3ed3e516a5b01b4c176dd12 to your computer and use it in GitHub Desktop.
Save kinoute/b7d54042d3ed3e516a5b01b4c176dd12 to your computer and use it in GitHub Desktop.
Example of Ruby on Rails 6 Multi-stage builds with Docker. Development/production environments share the same Dockerfile.
FROM ruby:2.7.1-slim AS base
LABEL maintainer="Yann Defretin <yann@defret.in"
# Common dependencies
RUN apt-get update -qq \
&& DEBIAN_FRONTEND=noninteractive apt-get install -yq \
--no-install-recommends \
build-essential=12.6 \
gnupg2=2.2.12-1+deb10u1 \
curl=7.64.0-4+deb10u1 \
less=487-0.1+b1 \
git=1:2.20.1-2+deb10u3 \
vim=2:8.1.0875-5 \
ssh=1:7.9p1-10+deb10u2 \
&& apt-get clean \
&& rm -rf /var/cache/apt/archives/* \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
&& truncate -s 0 /var/log/*log
# Add PostgreSQL to sources list
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN curl -sSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \
&& echo 'deb http://apt.postgresql.org/pub/repos/apt/ buster-pgdg main' 11 > /etc/apt/sources.list.d/pgdg.list
# Add NodeJS to sources list
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -
# Add Yarn to the sources list
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
&& echo 'deb http://dl.yarnpkg.com/debian/ stable main' > /etc/apt/sources.list.d/yarn.list
RUN apt-get update -qq && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq \
--no-install-recommends \
libpq-dev=12.3-1.pgdg100+1 \
postgresql-client-11=11.7-0+deb10u1 \
nodejs=12.16.3-1nodesource1 \
yarn=1.22.4-1 \
cron=3.0pl1-134+deb10u1 && \
apt-get clean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \
truncate -s 0 /var/log/*log
# Configure bundler
ENV LANG=C.UTF-8 \
BUNDLE_JOBS=4 \
BUNDLE_RETRY=3
RUN mkdir -p /mc-crm
COPY Gemfile* /mc-crm/
WORKDIR /mc-crm
RUN bundle install --jobs=10
RUN yarn install --check-files && yarn cache clean
# development stage
FROM base AS development
ENV RAILS_ENV development
COPY . /mc-crm
CMD ["bin/dev-start"]
# production stage
FROM base AS production
COPY . /mc-crm
ENV RAILS_ENV production
ENV NODE_ENV production
SHELL ["/bin/bash", "-c"]
RUN ["bundle", "exec", "rake", "assets:precompile"]
CMD ["bin/prod-start"]
#!/bin/bash
# stop on errors
set -e
# setup cronjobs
bundle exec whenever --update-crontab
# start cron service
service cron restart
# Remove a potentially pre-existing server.pid for Rails.
rm -f tmp/pids/server.pid
# setup database if it doesn't exist, otherwise run migrations if any
bundle exec rake db:prepare
# start webpack dev server
./bin/webpack-dev-server &
# start server
bundle exec rails server -b 0.0.0.0
#!/bin/bash
# stop on errors
set -e
# setup cronjobs
bundle exec whenever --update-crontab
# start cron service
service cron restart
# Remove a potentially pre-existing server.pid for Rails.
rm -f tmp/pids/server.pid
# setup database if it doesn't exist, otherwise run migrations if any
bundle exec rake db:prepare
# start server
bundle exec puma -C config/puma.rb
@brendonrapp
Copy link

BUNDLE_JOBS=4 env var on line 49 and the --jobs=10 bundle install flag on line 58 seem to be at odds with each other.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment