Skip to content

Instantly share code, notes, and snippets.

@vitobotta
Last active July 5, 2021 10:25
Show Gist options
  • Save vitobotta/a5f26666ac14547f1505e48cba797a36 to your computer and use it in GitHub Desktop.
Save vitobotta/a5f26666ac14547f1505e48cba797a36 to your computer and use it in GitHub Desktop.
Dockerfile
######################
# Stage: Builder
FROM ruby:2.6.5-alpine as Builder
ARG FOLDERS_TO_REMOVE
ARG BUNDLE_WITHOUT
ARG RAILS_ENV
ENV BUNDLE_WITHOUT ${BUNDLE_WITHOUT}
ENV RAILS_ENV ${RAILS_ENV}
ENV HOME=/home/rails
ENV LANG=C.UTF-8
ENV RAILS_ROOT=$HOME/app
ENV SECRET_KEY_BASE=foo
ENV RAILS_SERVE_STATIC_FILES=true
ENV RAILS_LOG_TO_STDOUT=true
RUN apk add --update --no-cache \
build-base \
postgresql-dev \
git \
imagemagick \
nodejs \
yarn \
tzdata \
mysql-client \
mariadb-dev \
chromium \
chromium-chromedriver \
curl
RUN mkdir $HOME
RUN wget https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz -O $HOME/GeoLite2-City.tar.gz \
&& tar xvfz $HOME/GeoLite2-City.tar.gz -C $HOME \
&& rm $HOME/GeoLite2-City.tar.gz \
&& mv $HOME/GeoLite2-City_*/GeoLite2-City.mmdb $HOME/ \
&& rm -rf $HOME/GeoLite2-City_*
WORKDIR $RAILS_ROOT
# Install gems
ADD Gemfile* $RAILS_ROOT/
RUN gem install bundler \
&& bundle install -j "$(getconf _NPROCESSORS_ONLN)" --without $BUNDLE_WITHOUT --retry 3 \
# Remove unneeded files (cached *.gem, *.o, *.c)
&& rm -rf /usr/local/bundle/cache/*.gem \
&& find /usr/local/bundle/gems/ -name "*.c" -delete \
&& find /usr/local/bundle/gems/ -name "*.o" -delete
# Install yarn packages
COPY package.json yarn.lock $RAILS_ROOT/
RUN yarn install
# Add the Rails app
ADD . $RAILS_ROOT
# Precompile assets
RUN rake webpacker:compile RAILS_ENV=production NODE_ENV=production
RUN yarn cache clean
# Remove folders not needed in resulting image
RUN rm -rf $FOLDERS_TO_REMOVE
###############################
# Stage Final
FROM ruby:2.6.5-alpine
ARG ADDITIONAL_PACKAGES
# Add Alpine packages
RUN apk add --update --no-cache \
mysql-client \
mariadb-dev \
imagemagick \
tzdata \
bash \
curl \
nodejs \
$ADDITIONAL_PACKAGES
ENV BUNDLE_WITHOUT ${BUNDLE_WITHOUT}
ENV RAILS_ENV ${RAILS_ENV}
ENV HOME=/home/rails
ENV LANG=C.UTF-8
ENV RAILS_ROOT=$HOME/app
ENV SECRET_KEY_BASE=foo
ENV RAILS_SERVE_STATIC_FILES=true
ENV RAILS_LOG_TO_STDOUT=true
WORKDIR $RAILS_ROOT
# Copy app with gems from former build stage
COPY --from=Builder /usr/local/bundle/ /usr/local/bundle/
COPY --from=Builder $RAILS_ROOT $RAILS_ROOT
COPY --from=Builder $HOME/GeoLite2-City.mmdb $HOME/
RUN mkdir -p ./lib/mariadb && ln -s /usr/lib/mariadb/plugin $RAILS_ROOT/lib/mariadb/plugin \
&& mkdir -p /usr/usr/lib/mariadb/plugin/ \
&& ln -s /usr/lib/mariadb/plugin/caching_sha2_password.so /usr/usr/lib/mariadb/plugin/caching_sha2_password.so
# RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl \
# && mv kubectl /usr/local/bin/ \
# && chmod +x /usr/local/bin/kubectl
RUN mkdir -p ./tmp/pids
RUN echo "alias rails='bundle exec rails'" > $HOME/.bashrc
# Expose Puma port
EXPOSE 3000
# Start up
CMD ["bundle", "exec", "puma", "-Cconfig/puma.rb"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment