Skip to content

Instantly share code, notes, and snippets.

@dserodio dserodio/Dockerfile
Last active Apr 12, 2019

Embed
What would you like to do?
Dockerfile for multi-stage build of a Ruby app which needs Node at build time (credits: https://github.com/gomex)
# Dockerfile for a multi-stage build of a Ruby app which needs Node at build time
#
# Thanks to https://github.com/gomex for sharing
FROM ruby:2.5.1 as builder
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - &&\
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - &&\
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
ARG NODE_DATE_INSTALL=20180710
RUN apt-get update \
&& apt-get install -y locales \
graphviz \
imagemagick \
postgresql-client-9.6 \
yarn \
nodejs \
&& echo "en_US.UTF-8 UTF-8" > /etc/locale.gen && /usr/sbin/locale-gen &&\
rm -rf /var/lib/apt/lists/*
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US.UTF-8
ENV LC_ALL en_US.UTF-8
ARG BUNDLE_GITHUB__COM
ENV BUNDLE_GITHUB__COM $BUNDLE_GITHUB__COM
ENV GEM_HOME /gems/vendor
ENV GEM_PATH /gems/vendor
ENV GEM_SPEC_CACHE /gems/specs
ENV BUNDLE_APP_CONFIG /gems/vendor
ENV BUNDLE_PATH /gems/vendor
ENV BUNDLE_BIN /gems/vendor/bin
ENV PATH /app/bin:/gems/vendor/bin:$PATH
ARG RAILS_ENV
ENV RAILS_ENV=$RAILS_ENV
ENV APP_ROOT /app
WORKDIR $APP_ROOT/
RUN mkdir -p /gems
RUN groupadd -r app \
&& groupmod -g 1000 app \
&& useradd -g app -ms /bin/bash app \
&& chown app $APP_ROOT \
&& chown -R app /gems
USER app
FROM builder as install
USER app
COPY Gemfile.lock $APP_ROOT/
COPY Gemfile $APP_ROOT/
COPY package-lock.json $APP_ROOT/
COPY package.json $APP_ROOT/
RUN bundle install
RUN yarn install
FROM install as preprod
USER root
ARG SECRET_KEY_BASE
ENV SECRET_KEY_BASE=$SECRET_KEY_BASE
COPY . /$APP_ROOT/
RUN bundle exec rails assets:precompile
# Packaging final app w/o node_modules & the development tools
FROM paycertify/ruby:2.5.1-slim as prod
LABEL maintainer="Rafael Gomes <rafael.gomes@paycertify.com>"
LABEL name="app"
ENV GEM_HOME /gems/vendor
ENV GEM_PATH /gems/vendor
ENV GEM_SPEC_CACHE /gems/specs
ENV BUNDLE_APP_CONFIG /gems/vendor
ENV BUNDLE_PATH /gems/vendor
ENV BUNDLE_BIN /gems/vendor/bin
ENV PATH /app/bin:/gems/vendor/bin:$PATH
WORKDIR /app/
RUN groupadd -r app \
&& groupmod -g 1000 app \
&& useradd -g app -ms /bin/bash app
COPY --from=preprod /usr/local/bundle/ /usr/local/bundle/
COPY --from=preprod /app/ /app/
COPY --from=preprod /gems/ /gems/
RUN chown -R app /gems
RUN chown -R app /app/
USER app
EXPOSE 3000
CMD ["bundle","exec","rails","server","-b","0.0.0.0"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.