Skip to content

Instantly share code, notes, and snippets.

@palkan
Last active May 14, 2020 06:09
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save palkan/c2bc39146730fbe381c03f43e6ad7b05 to your computer and use it in GitHub Desktop.
Save palkan/c2bc39146730fbe381c03f43e6ad7b05 to your computer and use it in GitHub Desktop.
hybrid dockerfile
ARG RUBY_VERSION
# === Base image ===
FROM ruby:${RUBY_VERSION}-slim-buster as base
ARG NODE_MAJOR
ARG POSTGRES_VERSION
ARG BUNDLER_VERSION
ARG YARN_VERSION
# Common dependencies
RUN apt-get update -qq \
&& apt-get dist-upgrade -y \
&& DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
build-essential \
gnupg2 \
curl \
less \
git \
&& apt-get clean \
&& rm -rf /var/cache/apt/archives/* \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
&& truncate -s 0 /var/log/*log
# Node
RUN curl -sL https://deb.nodesource.com/setup_${NODE_MAJOR}.x | bash -
# Postgres client
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' ${POSTGRES_VERSION} > /etc/apt/sources.list.d/pgdg.list
# Yarn
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
# App's dependencies
RUN apt-get update -qq \
&& DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
postgresql-client-${POSTGRES_VERSION} \
libpq-dev \
nodejs \
yarn=${YARN_VERSION}-1 \
&& apt-get clean \
&& rm -rf /var/cache/apt/archives/* \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
&& truncate -s 0 /var/log/*log
RUN mkdir /app
# Bundler
ENV LANG=C.UTF-8 \
BUNDLE_JOBS=4 \
BUNDLE_RETRY=3
RUN gem update --system \
&& gem install bundler:${BUNDLER_VERSION}
EXPOSE 3000
# === Development image ===
FROM base as development
ENV RAILS_ENV=development
RUN apt-get update -qq \
&& DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
vim \
&& apt-get clean \
&& rm -rf /var/cache/apt/archives/* \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
&& truncate -s 0 /var/log/*log
WORKDIR /app
CMD ["/usr/bin/bash"]
# === Image that installs libraries and compiles assets for production image ===
FROM base as production-builder
ENV RAILS_ENV=production
# Container user
RUN groupadd --gid 1005 my_user \
&& useradd --uid 1005 --gid my_user --shell /bin/bash --create-home my_user
USER my_user
RUN mkdir /home/my_user/app
WORKDIR /home/my_user/app
# JS packages
COPY --chown=my_user:my_user package.json yarn.lock ./
RUN yarn install --check-files
# Ruby gems
COPY --chown=my_user:my_user Gemfile* .ruby-version ./
ENV BUNDLE_APP_CONFIG=/home/my_user/bundle \
BUNDLE_PATH=/home/my_user/bundle \
GEM_HOME=/home/my_user/bundle
RUN mkdir $BUNDLE_PATH \
&& bundle install --without development test --deployment --no-cache --path $BUNDLE_PATH \
&& rm -rf $BUNDLE_PATH/ruby/2.6.0/cache/* \
&& rm -rf /home/my_user/.bundle/cache/*
# Code
COPY --chown=my_user:my_user . .
# Precompile assets
ARG SECRET_KEY_BASE=qwerty
ARG DATABASE_URL=postgres://postgres:postgres@postgres:5432
ARG REDIS_URL=redis://redis:6379/0
ARG CACHE_REDIS_URL=redis://redis-cache:6379/0
ARG IMGPROXY_ENDPOINT=http://localhost:3080
RUN bundle exec rake assets:precompile
# == Production image ==
FROM ruby:${RUBY_VERSION}-slim-buster AS production
# Common dependencies
RUN apt-get update -qq \
&& apt-get dist-upgrade -y \
&& DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
curl \
gnupg2 \
less \
tzdata \
time \
locales \
&& apt-get clean \
&& rm -rf /var/cache/apt/archives/* \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
&& truncate -s 0 /var/log/*log \
&& update-locale LANG=C.UTF-8 LC_ALL=C.UTF-8
# Postgres client for rails dbconsole -p
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' ${POSTGRES_VERSION} > /etc/apt/sources.list.d/pgdg.list \
&& apt-get update -qq \
&& DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
postgresql-client-${POSTGRES_VERSION} \
libpq5 \
&& apt-get clean \
&& rm -rf /var/cache/apt/archives/* \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
&& truncate -s 0 /var/log/*log
RUN gem update --system
ENV RAILS_ENV=production \
MALLOC_ARENA_MAX=2 \
BUNDLE_APP_CONFIG=/home/my_user/bundle \
BUNDLE_PATH=/home/my_user/bundle \
GEM_HOME=/home/my_user/bundle \
LANG=C.UTF-8 \
LC_ALL=C.UTF-8
# Container user
RUN groupadd --gid 1005 my_user \
&& useradd --uid 1005 --gid my_user --shell /bin/bash --create-home my_user
RUN mkdir /home/my_user/app
WORKDIR /home/my_user/app
USER my_user
EXPOSE 3000
RUN gem install bundler -v "${BUNDLER_VERSION}"
COPY --from=production-builder $BUNDLE_PATH $BUNDLE_PATH
COPY --from=production-builder /home/my_user/app/public/packs /home/my_user/app/public/packs
# Code
COPY --chown=my_user:my_user . .
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