Last active
March 10, 2025 21:21
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM ruby:2.7.1-slim AS base | |
LABEL maintainer="Yann Defretin <[email protected]" | |
# 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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.