Skip to content

Instantly share code, notes, and snippets.

@anonoz
Created March 26, 2018 03:13
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save anonoz/b56e4e32b8c9252a3085fae74b78a7c8 to your computer and use it in GitHub Desktop.
Save anonoz/b56e4e32b8c9252a3085fae74b78a7c8 to your computer and use it in GitHub Desktop.
Sample of multistage Dockerfile for Rails app in production
FROM madnight/docker-alpine-wkhtmltopdf as wkhtmltopdf_savior
# STAGE for bundle & yarn install
FROM ruby:2.4.3-alpine3.7 as builder
ENV CA_CERTS_PATH /etc/ssl/certs/
ENV RAILS_ENV production
ENV RAILS_LOG_TO_STDOUT true
ENV RAILS_SERVE_STATIC_FILES true
RUN apk add --no-cache \
build-base \
busybox \
ca-certificates \
curl \
git \
gnupg1 \
graphicsmagick \
libffi-dev \
libsodium-dev \
nodejs=8.9.3-r0 \
openssh-client \
postgresql-dev \
rsync \
yarn
RUN mkdir -p /app
WORKDIR /app
COPY Gemfile Gemfile.lock /app/
ARG SSH_CHECKOUT_KEY
RUN mkdir /root/.ssh/ \
&& echo "${SSH_CHECKOUT_KEY}" > /root/.ssh/id_rsa \
&& chmod 400 /root/.ssh/id_rsa \
&& touch /root/.ssh/known_hosts \
&& echo $'\
github.com,192.30.255.112 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ== \n\
bitbucket.org,104.192.143.1 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAubiN81eDcafrgMeLzaFPsw2kNvEcqTKl/VqLat/MaB33pZy0y3rJZtnqwR2qOOvbwKZYKiEO1O6VqNEBxKvJJelCq0dTXWT5pbO2gDXC6h6QDXCaHo6pOHGPUy+YBaGQRGuSusMEASYiWunYN0vCAI8QaXnWMXNMdFP3jHAJH0eDsoiGnLPBlBp4TNm6rYI74nMzgz3B9IikW4WVK+dc8KZJZWYjAuORU3jc1c/NPskD2ASinf8v3xnfXeukU0sJ5N6m5E8VLjObPEO+mN2t/FZTMZLiFqPWc/ALSqnMnnhwrNi2rbfg/rd/IpL8Le3pSBne8+seeFVBoGqzHM9yXw== \n\
' >> /root/.ssh/known_hosts \
&& bundle config --global frozen 1 \
&& bundle install --without development test -j4 --retry 3 \
&& rm -rf /root/.ssh/id_rsa \
/usr/local/bundle/bundler/gems/*/.git \
/usr/local/bundle/cache/
COPY package.json yarn.lock /app/
RUN yarn install
COPY . /app/
RUN bundle exec rails \
AWS_ACCESS_KEY_ID=dontneed \
AWS_SECRET_ACCESS_KEY=dontneed \
DATABASE_URL=postgresql:does_not_exist \
SECRET_KEY_BASE=nein \
assets:precompile
# Packaging final app w/o node_modules & the development tools
FROM ruby:2.4.3-alpine3.7
ENV CA_CERTS_PATH /etc/ssl/certs/
ENV RAILS_ENV production
ENV RAILS_SERVE_STATIC_FILES true
ENV RAILS_LOG_TO_STDOUT true
RUN apk add --no-cache \
busybox \
ca-certificates \
curl \
gnupg1 \
graphicsmagick \
libsodium-dev \
nodejs=8.9.3-r0 \
postgresql-dev \
rsync
# Patch for wkhtmltopdf
RUN apk add --update --no-cache \
libgcc libstdc++ libx11 glib libxrender libxext libintl \
libcrypto1.0 libssl1.0 \
ttf-dejavu ttf-droid ttf-freefont ttf-liberation ttf-ubuntu-font-family
COPY --from=wkhtmltopdf_savior /bin/wkhtmltopdf /bin/
RUN mkdir -p /app
WORKDIR /app
COPY --from=builder /usr/local/bundle/ /usr/local/bundle/
COPY --from=builder /app/ /app/
COPY --from=builder /app/config/gpg/ /root/.gnupg/
EXPOSE 3000
CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0"]
@srgl
Copy link

srgl commented Apr 1, 2018

I see you have a bunch of environment variables with dummy values in the assets precompilation line. Are you using ENV.fetch in production.rb? If so, you can create custom environment, name it like building.rb, copy production.rb in it with no lines using ENV.fetch.
Then use it like RUN RAILS_ENV=building bundle exec rails assets:precompile

@pgaertig
Copy link

Thanks for this. I would add rm -rf node_modules tmp/* log/* just after assets precompile - that saves some space and copy time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment