Skip to content

Commit

Permalink
Combine RUN directives and optimize Dockerfile for smaller build (#2)
Browse files Browse the repository at this point in the history
* Combine RUN directives and optimize Dockerfile for smaller build

* Remove duplicate WORKDIR directives

* Update commented package names for alpine
  • Loading branch information
gabe565 authored and brotandgames committed Jul 18, 2019
1 parent a6217d2 commit 071b6b1
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 38 deletions.
7 changes: 5 additions & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
.env.staging

# Ignore node_modules
/node_modules/*
/node_modules

# Ignore bundler config.
/.bundle
Expand All @@ -26,4 +26,7 @@ tags
.cache

# Ignore master key for decrypting credentials and more.
/config/master.key
/config/master.key

# Ignore Dockerfile
/Dockerfile
80 changes: 50 additions & 30 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,36 +1,56 @@
FROM ruby:2.5.5

RUN apt-get update -qq && apt-get install -y build-essential apt-transport-https

RUN curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list

RUN curl -sL https://deb.nodesource.com/setup_10.x | bash

RUN apt-get update -qq && apt-get install -y libxml2-dev libxslt1-dev nodejs yarn

# for postgres: libpq-dev
# for capybara-webkit: libqt4-webkit libqt4-dev xvfb

ENV APP_HOME=/app
ENV RACK_ENV=production
ENV RAILS_LOG_TO_STDOUT=true

RUN mkdir $APP_HOME
WORKDIR $APP_HOME

RUN gem update --system && gem install bundler

ADD Gemfile* package.json yarn.lock $APP_HOME/
# RUN bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment
RUN bundle install --without development:test --jobs 20

ADD . $APP_HOME
FROM ruby:2.5.5-alpine

# for postgres: postgresql-dev
RUN apk add --no-cache \
sqlite-dev \
tzdata \
yarn

WORKDIR /app

ARG RACK_ENV=production
ENV RACK_ENV=$RACK_ENV

ADD Gemfile* /app/
RUN set -x \
&& apk add --no-cache --virtual .build-deps \
build-base \
libxml2-dev \
libxslt-dev \
&& gem install bundler \
&& bundle install --without development:test --jobs 20 -j"$(nproc)" --retry 3 \
# Remove unneeded files (cached *.gem, *.o, *.c)
&& rm -rf \
/usr/local/bundle/cache/*.gem \
/usr/local/bundle/gems/**/*.c \
/usr/local/bundle/gems/**/*.o \
&& apk del .build-deps

COPY package.json yarn.lock /app/
RUN set -x \
&& yarn install \
&& rm -rf /tmp/*

COPY . ./

# The command '/bin/sh -c rake assets:precompile' needs the RAILS_MASTER_KEY to be set!?
# https://github.com/rails/rails/issues/32947
RUN SECRET_KEY_BASE=`bin/rake secret` rake assets:precompile
RUN set -x \
&& SECRET_KEY_BASE=foo bundle exec rake assets:precompile \
# Remove folders not needed in resulting image
&& rm -rf \
/tmp/* \
app/assets \
lib/assets \
node_modules \
spec \
tmp/cache \
vendor/assets

ENV RAILS_LOG_TO_STDOUT=true
ENV RAILS_SERVE_STATIC_FILES=true
ENV EXECJS_RUNTIME=Disabled

EXPOSE 3000

CMD ["./start.sh"]
CMD ["./start.sh"]
13 changes: 7 additions & 6 deletions start.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#!/usr/bin/env bash
#!/bin/sh

set -e
self=${0##*/}
set -euo pipefail

if [[ -z "$SECRET_KEY_BASE" ]]; then
self="$(basename "$0")"

if [ -z "${SECRET_KEY_BASE:-}" ]; then
echo "== $self WARNING: SECRET_KEY_BASE not set"
echo "== $self It will be set to a random value using \`rake secret\`"
export SECRET_KEY_BASE=`rake secret`
export SECRET_KEY_BASE="$(rake secret)"
fi

rake db:migrate

exec rails server -b 0.0.0.0 -p 3000
exec rails server -b 0.0.0.0 -p 3000

0 comments on commit 071b6b1

Please sign in to comment.