Hi guys,
We are currently moving out of Heroku so we need to migrate from buildpack to Docker.
We created a Dockerfile for our Rails app, based on that one: docker-rails-example/Dockerfile at main · nickjj/docker-rails-example · GitHub
Do you have any feedback to improve our build time?
Current build time: ~40min
Here’s our Dockerfile:
FROM ruby:3.1.2-slim-bullseye AS assets
WORKDIR /app
ARG UID=1000
ARG GID=1000
RUN bash -c "set -o pipefail && apt-get update \
&& apt-get install -y --no-install-recommends build-essential curl git libpq-dev cmake pkg-config \
&& curl -sSL https://deb.nodesource.com/setup_19.x | bash - \
&& curl -sSL 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 \
&& apt-get update && apt-get install -y --no-install-recommends nodejs yarn \
&& rm -rf /var/lib/apt/lists/* /usr/share/doc /usr/share/man \
&& apt-get clean \
&& groupadd -g \"${GID}\" ruby \
&& useradd --create-home --no-log-init -u \"${UID}\" -g \"${GID}\" ruby \
&& mkdir /node_modules && chown ruby:ruby -R /node_modules /app"
USER ruby
ARG RACK_ENV
ARG RAILS_ENV
ARG NODE_ENV
ARG NODE_OPTIONS
ARG APPLICATION_NAME
ARG ASSET_HOST
ARG CUSTOM_DOMAINS
ARG DOMAIN
ARG DOMAIN_PORT
ARG RAILS_MASTER_KEY
ARG REACT_APP_HOST_WWW
ARG SUBDOMAIN_ADMIN
ARG SUBDOMAIN_WWW
ARG TZ
ENV RACK_ENV="${RACK_ENV}" \
RAILS_ENV="${RAILS_ENV}" \
NODE_ENV="${NODE_ENV}" \
NODE_OPTIONS="${NODE_OPTIONS}" \
APPLICATION_NAME="${APPLICATION_NAME}" \
ASSET_HOST="${ASSET_HOST}" \
CUSTOM_DOMAINS="${CUSTOM_DOMAINS}" \
DOMAIN="${DOMAIN}" \
DOMAIN_PORT="${DOMAIN_PORT}" \
RAILS_MASTER_KEY="${RAILS_MASTER_KEY}" \
REACT_APP_HOST_WWW="${REACT_APP_HOST_WWW}" \
SUBDOMAIN_ADMIN="${SUBDOMAIN_ADMIN}" \
SUBDOMAIN_WWW="${SUBDOMAIN_WWW}" \
TZ="${TZ}" \
PATH="${PATH}:/home/ruby/.local/bin:/node_modules/.bin" \
USER="ruby"
COPY --chown=ruby:ruby Gemfile* ./
RUN bundle install
COPY --chown=ruby:ruby package.json *yarn* ./
RUN yarn install
COPY --chown=ruby:ruby . .
ENV RACK_ENV=production
ENV NODE_OPTIONS=--max-old-space-size=4096
RUN SECRET_KEY_BASE=`bin/rake secret` bundle exec rails assets:precompile --trace && \
yarn cache clean && \
rm -rf node_modules tmp/cache vendor/assets test
###############################################################################
FROM ruby:3.1.2-slim-bullseye
WORKDIR /app
ARG UID=1000
ARG GID=1000
RUN apt-get update \
&& apt-get install -y --no-install-recommends build-essential curl libpq-dev nodejs npm \
&& rm -rf /var/lib/apt/lists/* /usr/share/doc /usr/share/man \
&& apt-get clean \
&& groupadd -g "${GID}" ruby \
&& useradd --create-home --no-log-init -u "${UID}" -g "${GID}" ruby \
&& chown ruby:ruby -R /app
RUN npm install -g mjml
RUN which mjml
USER ruby
COPY --chown=ruby:ruby bin/ ./bin
RUN chmod 0755 bin/*
ARG RACK_ENV
ARG RAILS_ENV
ARG NODE_ENV
ARG APPLICATION_NAME
ARG ASSET_HOST
ARG CORS_ALLOW_ORIGINS
ARG CUSTOM_DOMAINS
ARG DOMAIN
ARG DOMAIN_PORT
ARG RAILS_MASTER_KEY
ARG REACT_APP_HOST_WWW
ARG REACT_FEATURE_TOKEN_BUDGET_SPACE_SLUG
ARG SUBDOMAIN_ADMIN
ARG SUBDOMAIN_WWW
ARG TZ
ARG WEB_CONCURRENCY
ENV RACK_ENV="${RACK_ENV}" \
RAILS_ENV="${RAILS_ENV}" \
NODE_ENV="${NODE_ENV}" \
APPLICATION_NAME="${APPLICATION_NAME}" \
ASSET_HOST="${ASSET_HOST}" \
CORS_ALLOW_ORIGINS="${CORS_ALLOW_ORIGINS}" \
CUSTOM_DOMAINS="${CUSTOM_DOMAINS}" \
DOMAIN="${DOMAIN}" \
DOMAIN_PORT="${DOMAIN_PORT}" \
RAILS_MASTER_KEY="${RAILS_MASTER_KEY}" \
REACT_APP_HOST_WWW="${REACT_APP_HOST_WWW}" \
REACT_FEATURE_TOKEN_BUDGET_SPACE_SLUG="${REACT_FEATURE_TOKEN_BUDGET_SPACE_SLUG}" \
SUBDOMAIN_ADMIN="${SUBDOMAIN_ADMIN}" \
SUBDOMAIN_WWW="${SUBDOMAIN_WWW}" \
WEB_CONCURRENCY="${WEB_CONCURRENCY}" \
TZ="${TZ}" \
PATH="${PATH}:/home/ruby/.local/bin:/node_modules/.bin" \
USER="ruby"
COPY --chown=ruby:ruby --from=assets /usr/local/bundle /usr/local/bundle
COPY --chown=ruby:ruby --from=assets /app/public /public
COPY --chown=ruby:ruby . .
ENTRYPOINT ["/app/bin/docker-entrypoint-web"]
EXPOSE 3000
CMD ["rails", "s"]
Thanks a lot for your help